Command line is probably the most universal way of passing information from caller to the program. Concept of a command line is part of most operating systems and programming languages including C and C++. However model and terminology for command line processing vary greatly among different systems.
Single UNIX Specification contains Utility Argument Syntax Conventions and Guidelines which document basic terminology for command line processing. Single UNIX Specification model is a "common denominator" for different UNIX implementations. It is somewhat minimal and targets system utilities rather than a wide spectrum of applications. Another de-facto command line processing model is GNU Standard for Command Line Interfaces which generally encourages conformance to the Single UNIX Specification but adds few extensions and uses different terminology.
The idea behind this document is to establish terminology and complete model for command line processing. Terms translation between this document, Single UNIX Specification and GNU Standard for Command Line Interfaces is provided in Appendix A.
Command line is an array of character strings and not just a string with spaces between words as some people tend to think.
Each string in a command line array is referred to as argument . First argument usually contains a string that refers to an executable.
Interpretation of arguments is completely up to a program logic however conventions exist that vary among different systems. Usually groups of arguments are translated into a higher-level objects such as commands, options, and operands. These objects form a model for command line processing. All of them are defined below.
Command is usually a word, or a single letter that represents a command to the program logic. Neither Single UNIX Specification nor GNU Standard for Command Line Interfaces has the notion of a command. Other terms for command include action and function. Command is usually (but not necessarily) the first argument after executable name. Here are few examples:
tar x
Here we have a one letter command 'x'
(extract). In GNU tar
manual it is called functional letter.
tar xvf
Here we have three commands encoded as a single letter each. Actually
semantically only 'x'
is a command while 'v'
(verbose) and 'f'
(read from a file) are options.
openssl req
Here we have a word command 'req'
(operations with certificate
requests).
cvs checkout foo
Here we have a word command 'checkout'
and command operand
foo
.
tar --help
Even though '--help'
is usually considered to be an option
semantically it is a command.
Option consists of option name and optionally one or more option values. Options are usually optional. Non-optional options are usually better represented by commands or operands.
Option name usually takes up one argument. Option names usually start with
a prefix (e.g. '--compile-only'
, '-c'
or '/c'
). This helps distinguish them from commands and operands. Option name
may have aliases (e.g. for option name '--output-dir'
there could
be an '-o'
alias).
Option without a value is alway optional and represents an option with implied binary value (e.g. {0, 1} or {false, true} etc.). Such option is sometimes called flag.
Option can be associated with a program or a command. Thus the concept of option can be further refined to program option and command option. Program option alters behavior of the program as a whole while command option is only affecting particular command.
Following are some examples:
g++ -o hello.o hello.cpp
Here we have an option with name '-o'
which has a value
'hello.o'
. 'hello.cpp'
is an operand.
ls -l
Here we have a flag with name '-l'
.
foo --bar=a,b,c
foo -b "a,b,c"
foo /baz a b c
Here we have a more elaborate example of a multi-format option. It has
a name '--bar'
and two aliases: '-b'
and
'/baz'
. It also has three values (in our case they are
'a'
, 'b'
, and 'c'
).
cvs -z 6 checkout -P foo
Here we have a program option with name '-z'
and value
'6'
(set compression level to be 6). 'checkout'
is a command. -P
is a command flag (prune empty directories).
'foo'
is a command operand.
operand usually represents an input value or a parameter. Operands can be mandatory or optional. Interpretation of operands is usually application-specific.
Same as with option the concept of operand can be further refined to program operand and command operand.
Term | Single UNIX Specification | GNU |
---|---|---|
command line | command line | command line |
argument | argument | argument |
command | -- | -- |
option | -- | option |
option name | option | name |
option value | option-argument | -- |
program option | -- | -- |
command option | -- | -- |
operand | operand | argument |
program operand | -- | -- |
command operand | -- | -- |