firstworks Programming with Rudiments using the commandline class

Using the commandline class

The commandline class provides methods for parsing command line arguments.

Most programs take command line options in the option/value pair format:

> program -option value -option value ...

or

> program --option=value --option=value ...

The commandline class makes it easy to deal with command line options of either of these formats.

The found() method returns true if the option is found and false otherwise. The getValue() method returns the value of the option or an empty string if the option wasn't specified.

The following program greets the user and takes two arguments:

- -hello
- -name name

If -hello is specified then the user is greeted with "hello", rather than "hi". If -name is specified then the user is greeted by name.

> #include <rudiments/commandline.h>
> #include <rudiments/stdio.h>
> int main(int argc, const char **argv) {
> // initialize an instance of the commandline class
> commandline cmdl(argc,argv);
> // determine the greeting
> const char *greeting="hi";
> if (cmdl.found("hello")) {
> greeting="hello";
> }
> // get the name
> const char *name=cmdl.getValue("name");
> // print the greeting
> stdoutput.printf("%s %s\n",greeting,name);
> }