Different command-line argument parsing methods are used by different programming languages to parse command-line arguments.
C uses argv
to process command-line arguments.[1] [2]
An example of C argument parsing would be:
int main (int argc, char *argv[])C also has functions called getopt and getopt_long.
An example of C# argument parsing would be:
An example of Java argument parsing would be:
Here are some possible ways to print arguments in Kotlin:[3]
Perl uses @ARGV
.
AWK uses ARGV
also.
PHP uses argc
as a count of arguments and argv
as an array containing the values of the arguments.[4] [5] To create an array from command-line arguments in the -foo:bar
format, the following might be used:
function parseArgs(array $args)
function getArg(array $args, string $arg)
PHP can also use getopt
.[6]
Python uses sys.argv
, e.g.:
Python also has a module called argparse
in the standard library for parsing command-line arguments.[7]
Racket uses a current-command-line-arguments
parameter, and provides a racket/cmdline
[8] library for parsing these arguments. Example:
(require racket/cmdline)
(define smile? (make-parameter #t))(define nose? (make-parameter #false))(define eyes (make-parameter ":"))
(command-line #:program "emoticon"
#:once-any ; the following two are mutually exclusive [("-s" "--smile") "smile mode" (smile? #true)] [("-f" "--frown") "frown mode" (smile? #false)]
#:once-each [("-n" "--nose") "add a nose" (nose? #true)] [("-e" "--eyes") char "use <char> for the eyes" (eyes char)])
(printf "~a~a~a\n" (eyes) (if (nose?) "-" "") (if (smile?) ")" "("))The library parses long and short flags, handles arguments, allows combining short flags, and handles -h
and --help
automatically:
Rexx uses arg
, e.g.:
The args are in env::args
.[9]
fn main
JavaScript programs written for Node.js use the process.argv
global variable.[10]
Node.js programs are invoked by running the interpreter node interpreter with a given file, so the first two arguments will be node
and the name of the JavaScript source file. It is often useful to extract the rest of the arguments by slicing a sub-array from process.argv
.[11]
JavaScript written for Bun use Bun.argv
and the util.parseArgs
function.[12]
JavaScript written for Deno use Deno.args
[13] and the parseArgs
function.[14]