Argument Parsing for Console Applications

  Parsing console application arguments is a basic task for system programmer.

  First, let's clarify some confusing terms:

  Suppose the syntax of a tool is: "tool_name[-a][-b][-c option_parameter] [-d-e][-foption_parameter][operand...]"

  The tool in the example is named as "tool_name". It is followed by options, option-arguments, and operands. The arguments that consist of hyphens and single letters or digits, such as 'a', are known as "options" (or, historically, "flags", "switches"). Certain options are followed by an "option-argument", as shown with [ -c option_argument]. The arguments following the last options and option-arguments are named "operands".

  In general:
  • Argument is parameter that is used to tell applications some specific information in order to accomplish some specific tasks. It can be divided into 3 categories: Option, Option-Parameter and Operand.
  • Option (also named as: flag, switch) is used to customize some default behaviours of program.
  • Option-Parameter is used to provide extra information for the option.
  • Operand is usually source or destination file name, directory name. It will not affect the program's behaviour, but just help program to identify the data to be processed.

  Then, let's see command argument syntax or convention in Winows and *nix world.

POXIS Command Argument Syntax/Convention
  • Arguments are options if they begin with a hyphen delimiter (`-').
  • Multiple options may follow a hyphen delimiter in a single token if the options do not take arguments. Thus, `-abc' is equivalent to `-a -b -c'.
  • Option names are single alphanumeric characters.
  • Certain options require an parameter.
  • An option and its parameter may or may not appear as separate tokens. (In other words, the whitespace separating them is optional.) Thus, `-o foo' and `-ofoo' are equivalent.
  • Options typically precede other non-option arguments.
  • The argument `--' terminates all options; any following arguments are treated as non-option arguments, even if they begin with a hyphen.
  • A token consisting of a single hyphen character is interpreted as an ordinary non-option argument. By convention, it is used to specify input from or output to the standard input and output streams.
  • GNU introduced so called long option style. It starts with '--', the option name consists of letter and '-'. If there is any parameter, it is separated with name by whitespace or '='.
POSIX Option
GNU Long Option
Argument Parsing in TAOUP

Windows Command Argument Syntax/Convention
  • Option consists of an option specifier, either a dash (–) or a forward slash (/), followed by the name of the option.
  • Option names cannot be abbreviated.
  • Some options take an argument, specified after a colon (:) or whitespace.
  • No spaces or tabs are allowed within an option specification, except within a quoted string.
  • Option names and their keyword or filename arguments are not case sensitive, but identifiers as arguments are case sensitive.
  • Use suffix/prefix "+/-" to represent order attribute or addition/removal operation of Options.
  • @fileName.ext style argument specifies that the filename is not used as argument, but the content of the file is used as command argument, plus other normal arguments.
  • To see more concrete examples, run "dir /?", "robocopy /?", "runas /?" and watch the screen output.
MSVS Link.exe Option Syntax
Sqlcmd.exe Option Syntax

Yet Another C++ Command Line Argument Parser - ApCxx
- It supports both Windows convention and Posix/GNU convention
- It supports both ANSI version and Unicode version
- It is implemented in portable C++ code
- It can Get all/specific options/operands
- It can Get all/specific arguments for specific option
- It can check whether some option is give

Source Code download


Option Parsing for C++:

Option Parsing for Other Languages:
C# OptParse
ASM OptParse

No comments: