Command-line argument parsing refers to methods used in a programming language to parse command-line arguments.
Many languages offer functionality for argument parsing. For example, the C POSIX library provides , Python offers a module called <code>argparse</code>, while C# provides a namespace <code>System.CommandLine</code>. In others, they are not bundled in the standard library, but rather must be used through third-party libraries.
In many languages, particularly C-derived languages, arguments are accessed through the parameters of the <code>main()</code> method. For example, in C and C++, the <code>main</code> method has signature , where <code>argc</code> is the number of arguments plus the name of the program, and <code>argv</code> is an array of C-strings where <code>argv[0]</code> is the name of the program. In Java and C#, the <code>main()</code> method instead takes one parameter <code>args</code> of type <code>String[]</code> (an array of strings). Meanwhile, in some other languages, such as Rust, command-line arguments are accessed by a method <code>std::env::args()</code>, allowing a global point of access rather than having to be obtained from <code>main()</code>.
AWK uses <code>ARGV</code> also.
C uses <code>argv</code> to process command-line arguments.
An example of C argument parsing would be:
C POSIX library also has functions called and .
C++ accesses arguments the same way as C.
The POCO C++ Libraries offer a class <code>Poco::Util::OptionProcessor</code> for parsing command-line arguments. Boost provides a class <code>boost::program_options::command_line_parser</code>. Meanwhile, Google has a library called <code>gflags</code>. There is also a <code>argparse</code> library for C++17+ offers a similar API for argument parsing to Python <code>argparse</code>.
An example of C# argument parsing would be:
C# provides the <code>System.CommandLine</code> namespace for advanced command-line argument parsing.
The D programming language provides a module <code>std.getopt</code>.
Go provides the <code>flag</code> package for argument parsing.
Haskell provides the library <code>System.Console.GetOpt</code>.
An example of Java argument parsing would be:
The Apache Commons library <code>org.apache.commons.cli</code> provides command-line argument parsing capabilities. There is also the <code>gnu.getopt</code> library, ported from GNU getopt.
Here are some possible ways to print arguments in Kotlin:
Perl uses <code>@ARGV</code>.
or
There is also <code>Getopt::Long</code> and <code>Getopt::Std</code> for argument parsing.
PHP uses <code>argc</code> as a count of arguments and <code>argv</code> as an array containing the values of the arguments. To create an array from command-line arguments in the <code>-foo:bar</code> format, the following might be used:
PHP can also use <code>getopt()</code>.
Python uses <code>sys.argv</code>, e.g.:
Python also has a module called <code>argparse</code> in the standard library for parsing command-line arguments.
Racket uses a <code>current-command-line-arguments</code> parameter, and provides a <code>racket/cmdline</code> library for parsing these arguments. Example:
The library parses long and short flags, handles arguments, allows combining short flags, and handles <code>-h</code> and <code>--help</code> automatically:
Rexx uses <code>arg</code>, e.g.:
Rather than being part of the parameters of <code>main()</code> (like other C-style languages), in Rust the args are in <code>std::env::args()</code>, which returns a <code>std::env::Args</code> and is converted to a <code>Vec<String></code> with <code>.collect()</code>.
A popular Rust library for command-line argument parsing is <code>clap</code>.
JavaScript programs written for Node.js use the <code>process.argv</code> global variable.
Node.js programs are invoked by running the interpreter node interpreter with a given file, so the first two arguments will be <code>node</code> 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 <code>process.argv</code>.
JavaScript written for Bun use <code>Bun.argv</code> and the <code>util.parseArgs</code> function.
JavaScript written for Deno use <code>Deno.args</code> and the <code>parseArgs</code> function.