Command-line argument parsing
Command-line argument parsing refers to methods used in a programming language to parse command-line arguments.
Command-line options
Parsing methods
Many languages offer functionality for argument parsing. For example, the C POSIX library provides getopt|, Python offers a module calledargparse, while C# provides a namespace System.CommandLine. 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
main method. For example, in C and C++, the main method has signature argc is the number of arguments plus the name of the program, and argv is an array of C-strings where argv is the name of the program. In Java and C#, the main method instead takes one parameter args of type String. Meanwhile, in some other languages, such as Rust, command-line arguments are accessed by a method std::env::args, allowing a global point of access rather than having to be obtained from main.In different programming languages
AWK
usesARGV also.BEGIN
C
C usesargv to process command-line arguments.An example of C argument parsing would be:
- include
C POSIX library also has functions called and.
C++
accesses arguments the same way as C.import std;
using std::string;
using std::vector;
int main
The POCO C++ Libraries offer a class
Poco::Util::OptionProcessor for parsing command-line arguments. Boost provides a class boost::program_options::command_line_parser. Meanwhile, Google has a library called gflags. There is also a argparse library for C++17+ offers a similar API for argument parsing to Python argparse.C#
An example of C# argument parsing would be:class ReadArgs
C# provides the
System.CommandLine namespace for advanced command-line argument parsing.D
The D programming language provides a modulestd.getopt.Go
Go provides theflag package for argument parsing.Haskell
Haskell provides the librarySystem.Console.GetOpt.Java
An example of Java argument parsing would be:public class ReadArgs
The Apache Commons library
org.apache.commons.cli provides command-line argument parsing capabilities. There is also the gnu.getopt library, ported from GNU getopt.Kotlin
Here are some possible ways to print arguments in Kotlin:fun main = println)
fun main = println)
fun main
Perl
uses@ARGV.foreach $arg
or
foreach $argnum
There is also
Getopt::Long and Getopt::Std for argument parsing.PHP
usesargc 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:$args = parseArgs;
echo getArg;
function parseArgs: array
function getArg: string | bool
PHP can also use
getopt.Python
usessys.argv, e.g.:import sys
if __name__ "__main__":
for arg in sys.argv:
Python also has a module called
argparse in the standard library for parsing command-line arguments.Racket
uses acurrent-command-line-arguments parameter, and provides a racket/cmdline library for parsing these arguments. Example:- lang racket
#:once-each
char "use
" ")
The library parses long and short flags, [handles arguments, allows combining short flags, and handles
-h and --help automatically:$ racket /tmp/c -nfe 8
8-(
Rexx
usesarg, e.g.:do i=1 to words
say word
end
Rust
Rather than being part of the parameters ofmain, in Rust the args are in std::env::args, which returns a std::env::Args and is converted to a Vec with .collect.use std::env;
fn main
A popular Rust library for command-line argument parsing is
clap.JavaScript
Node.js
programs written for Node.js use theprocess.argv global variable.// argv.js
console.log;
$ node argv.js one two three four five
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.// process-args.js
console.log;
$ node process-args.js one two=three four
Bun
JavaScript written for Bun useBun.argv and the util.parseArgs function.console.log;
Deno
JavaScript written for Deno useDeno.args and the parseArgs function.console.log;