A nice and clean C++ class for handling the work of parsing input arguments.
Sorts the arguments correctly and binds them together with flags.
This allows the programmer to quickly identify and implement different command-line input.
At the same time; all dirty code is abstracted away.
Maybe this'll be useful if you're not very known with command-line commands.
http://publib.boulder.ibm.com/tividd/td/tec/SC32-1232-00/en_US/HTML/ecormst15.htm
We read the argument string from left to right.
The path that the program is being run from is always fetched and stored separately.
It is returned by the "getPath()" method, and is always the first string of char *argv[].
A flag is a program option. Options are defined by:
-
- one dash; single-character option
- -- two dashes; multiple-character option
An argument is a piece of data that belongs to a flag.
Method 1: "-o a" would make "a" the argument to option "-o". This method uses a whitespace as separator.
Method 2: "-o=a" would make "a" the argument to option "-o". This method uses a '=' as separator.
An argument will always be bound to the last unset, non-inert flag.
(We get back to that later)
A single-characted flag can be chained:
"-a -b -c" becomes "-abc"
Arguments will be applied to the last unset, non-inert flag such that in:
"-q -abc uvw dre xyz nmr"
uvw will be bound to "-c"
dre will be bound to "-b"
xyz will be bound to "-a"
and nmr will be bound to "-q"
Flags that are inert will not be filled in by arguments:
Let flags "-a", "-b", "-c" exist.
Let flag "-b" be inert.
"-abc some argument"
Will bind "some" to "-c" and "argument" to "-a".
Operands are flagless arguments. They are created when:
- There are no more flags to bind to
or - We have given anything behind "--" (Flag Terminator)
- Operands are sorted by order and can be retrieved using getOperand(std::size_t).
- getOperandCount() returns the amount of operands. getOperand does not check bounds.
All flag-starters ('-' and "--") will be ignored after issueing a "--" followed by nothing:
Let flags "-a", "-b", "-c" exist.
Let flag "-b" be inert.
"-ab data -- -c dr"
Will bind "data" to "-a"
Will add "-c" and "dr" to a collection of operands.
C++ argument parsing utility class.
Written in pure C++.
Conforms with the GNU Utility Syntax guidelines. One exception: "-o foo" is NOT "-ofoo" due to "option-argument mirroring".
More info: http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
See the folder ./src/main.cpp
This file will contain a complete test