Girish Mahajan (Editor)

Command line argument parsing

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

Different Command-line argument parsing methods are used by different programming languages to parse command-line arguments.

Contents

C

C uses argv to process command-line arguments.

An example of C argument parsing would be:

Java

An example of Java argument parsing would be:

Bash

Bash uses $1 $2 ... ($0 is the script filename).

or

Perl

Perl uses $ARGV.

or

AWK

AWK uses ARGV also.

PHP

PHP uses argc as a count of arguments and argv as an array containing the values of the arguments. To create an array from command-line arguments in the -foo:bar format, the following might be used:

PHP can also use getopt().

Python

Python uses sys.argv, e.g.:

Python also has a module called argparse in the standard library for parsing command-line arguments.

Racket

Racket uses a current-command-line-arguments parameter, and provides a racket/cmdline library for parsing these arguments. Example:

The library parses long and short flags, handles arguments, allows combining short flags, and handles -h and --help automatically:

References

Command-line argument parsing Wikipedia