Command-line argument parsing refers to methods used in a programming language to parse command-line arguments.
A command-line option or simply option (also known as a flag or switch) modifies the operation of a command; the effect is determined by the command's program. Options follow the command name on the command line, separated by, e.g., commas, spaces. Separators are not always required, such as Dir/? and DIR /? in DOS, which have the same effect [1] of listing the DIR command's available options, whereas dir --help (in many versions of Unix) does require the option to be preceded by at least one space (and is case-sensitive).
The format of options varies widely between operating systems. In most cases, the syntax is by convention rather than an operating system requirement; the entire command line is simply a string passed to a program, which can process it in any way the programmer wants, so long as the interpreter can tell where the command name ends and its arguments and options begin.
A few representative samples of command-line options, most relating to listing files in a directory, to illustrate some conventions:
| Operating system | Command | Valid alternative | Notes |
|---|---|---|---|
| OpenVMS | directory/owner | Dir /Owner | instruct the directory command to also display the ownership of the files. Note the Directory command name is not case sensitive, and can be abbreviated to as few letters as required to remain unique. |
| Windows | DIR/Q/O:S d* | dir /q d* /o:s | display ownership of files whose names begin with d (or D), sorted by size, smallest first. Note spaces around argument d* are required. |
| Unix-like systems | ls -lS D* | ls -S -l D* | display in long format files and directories whose names begin with D (but not d), sorted by size (largest first). Note spaces are required around all arguments and options, but some can be run together, e.g. -lS is the same as -l -S. |
| Data General RDOS CLI | list/e/s 04-26-80/b | List /S/E 4-26-80/B | list every attribute for files created before 26 April 1980. Note the /B at the end of the date argument is a local switch, that modifies the meaning of that argument, while /S and /E are global switches, i.e. apply to the whole command. |
| VM/CMS CLI | LISTFILE (FULLDATE) | l(ful | includes the date the file was last written in the list. Note the LISTFILE command name is not case sensitive, and can be abbreviated to as few letters as required to remain unique. |
| OS/360 operator commands | START TAPERDR,DSNAME=FOO.BAR | S TAPERDR,DSN=FOO.BAR | Start the procedure named TAPERDR with the supplied data set name |
| TSO | LISTCAT LEVEL(FOO) MEMBERS | LISTC L(FOO) M | List datasets at specified index level and list members for each PDS. |
SEND 'text' USER(FOO) | SE 'text' U(FOO) | Send message to specified user |
Many languages offer functionality for argument parsing. For example, the C POSIX library provides getopt(), Python offers a module called argparse [2] , while C# provides a namespace System.CommandLine [3] . 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 intmain(intargc,char*argv[]);, where argc is the number of arguments plus the name of the program, and argv is an array of C-strings where argv[0] is the name of the program. In Java and C#, the main() method instead takes one parameter args of type String[] (an array of strings). 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().
AWK uses ARGV also.
BEGIN{for(i=0;i<ARGC;i++){printARGV[i]}}C uses argv to process command-line arguments. [4] [5]
An example of C argument parsing would be:
#include<stdio.h>intmain(intargc,char*argv[]){for(inti=0;i<argc;++i){printf("%s\n",argv[count]);}} C POSIX library also has functions called getopt() and getopt_long().
C++ accesses arguments the same way as C.
importstd;usingstd::string;usingstd::vector;intmain(intargc,char*argv[]){vector<string>args(argv,argv+argc);for(conststring&s:args){std::println("{}",s);}}The POCO C++ Libraries offer a class Poco::Util::OptionProcessor for parsing command-line arguments. [6] Boost provides a class boost::program_options::command_line_parser. [7] 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. [8]
An example of C# argument parsing would be:
classReadArgs{staticvoidMain(string[]args){foreach(stringarginargs){Console.WriteLine(arg);}}}C# provides the System.CommandLine namespace for advanced command-line argument parsing. [9]
The D programming language provides a module std.getopt.
Go provides the flag package for argument parsing.
Haskell provides the library System.Console.GetOpt.
An example of Java argument parsing would be:
publicclassReadArgs{publicstaticvoidmain(String[]args){for(Strings:args){System.out.println(s);}}}The Apache Commons library org.apache.commons.cli provides command-line argument parsing capabilities. [10] There is also the gnu.getopt library, ported from GNU getopt.
Here are some possible ways to print arguments in Kotlin: [11]
funmain(args:Array<String>)=println(args.joinToString())funmain(args:Array<String>)=println(args.contentToString())funmain(args:Array<String>){for(arginargs){println(arg)}} Perl uses @ARGV.
foreach$arg(@ARGV){print$arg;}or
foreach$argnum(0..$#ARGV){print$ARGV[$argnum];}There is also Getopt::Long and Getopt::Std for argument parsing.
PHP uses argc as a count of arguments and argv as an array containing the values of the arguments. [12] [13] To create an array from command-line arguments in the -foo:bar format, the following might be used:
$args=parseArgs($argv);echogetArg($args,"foo");functionparseArgs(array$args):array{foreach($argsas$arg){$tmp=explode(":",$arg,2);if($arg[0]==="-"){$args[substr($tmp[0],1)]=$tmp[1];}}return$args;}functiongetArg(array$args,string$arg):string|bool{if(isset($args[$arg])){return$args[$arg];}returnfalse;}PHP can also use getopt(). [14]
Python uses sys.argv, e.g.:
importsysif__name__=="__main__":forarginsys.argv:print(arg)Python also has a module called argparse in the standard library for parsing command-line arguments. [2]
Racket uses a current-command-line-arguments parameter, and provides a racket/cmdline [15] library for parsing these arguments. Example:
#lang racket(requireracket/cmdline)(definesmile?(make-parameter#t))(definenose?(make-parameter#false))(defineeyes(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"(eyeschar)])(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:
$ racket/tmp/c-nfe88-( Rexx uses arg, e.g.:
doi=1towords(arg(1))sayword(arg(1),i)endRather than being part of the parameters of main() (like other C-style languages), in Rust the args are in std::env::args(), which returns a std::env::Args and is converted to a Vec<String> with .collect(). [16]
usestd::env;fnmain(){letargs:Vec<String>=env::args().collect();letquery:&String=&args[1];letfile_path:&String=&args[2];println!("Searching for {}",query);println!("In file {}",file_path);}A popular Rust library for command-line argument parsing is clap. [17]
JavaScript programs written for Node.js use the process.argv global variable. [18]
// argv.jsconsole.log(process.argv);$nodeargv.jsonetwothreefourfive ['node', '/home/avian/argvdemo/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. [19]
// process-args.jsconsole.log(process.argv.slice(2));$nodeprocess-args.jsonetwo=threefour ['one', 'two=three', 'four']JavaScript written for Bun use Bun.argv and the util.parseArgs function. [20]
console.log(Bun.argv);JavaScript written for Deno use Deno.args [21] and the parseArgs function. [22]
console.log(Deno.args);