Parsing command line arguments using LibCapy

In this article I'll show how to parse arguments from the command line using LibCapy's CapyArgParser object.

Lets imagine you are developping a program to be executed from the command line. One way to allow interaction between the user and your program is to add arguments after the command executing your program. For example, a program to add numbers called add, could be executed with some arguments like this: add 1 2, and add the given arguments, in this example 1 2. The CapyArgParser object allows you program that kind of program easily:

The result of add 1 2 is:

You get the corrected answer, but also an error message. CapyArgParser uses the argument definition to check if the arguments given by the user are as expected. As we have left empty the definition, the parser expect by default no argument, hence the error message. Let's fill in the definition of the arguments to indicates that the program expect two arguments.

The array of CapyArg contains the definition for each arguments, including the program itself. Here we say that the argument corresponding to the program (.lbl=programName) takes 2 values in argument (.nbVal=2). To identify your program, you should always use argv[0], not a constant string. The reason is that the user may execute your program with a command different from what you initially thought (renamed binary, alias, including path, ...), which would break your argument definition. Using argv[0] you'll be forever safe.

Now, the result of add 1 2 is:

And if we give the wrong number of arguments, add 1 2 3:

If you wish to execute some code in case of invalid arguments, CapyArgParser raise an exception which you could catch and process:

Now if we give the wrong number of arguments, add 1 2 3:

Of course, you'll soon want to add some other optional arguments. There is one that's automatically defined by CapyArgParser, it's --help which, as you would expect, print the help. add 1 2 3:

There is not much for the moment as we haven't filled in much in the argument definition. Lets add some information about the program:

.help="..." sets the help message for an argument (here the program itself). The help now shows:

Until now we've restricted the program to take only two values. We could let it take any number of integers and just add them up all. This would be done as follow:

add 1 2 3:

Finally, more options can be defined, each with a label and shortcut which can be used indifferently, a number of arguments, and a help message. The shortcut and label do not need to be both given, but at least one is necessary. If none is given, CapyArgParser will raise an exception.

The help now looks like this:

The use of optional arguments can be check with isSet(argument_name). And there value can be retrieved with getAsInt(argument_name, idx_value), getAsDouble(argument_name, idx_value) and getAsStr(argument_name, idx_value) as necessary. For example:

add -b 10.0 1 2 3:

To conclude, one word about arguments with variable number of values. The number of values being undefined, all values after one of these argument until the next argument or the end of the command line are associated with this argument. Values associated to the program makes exception to this rule. Any value which is not associated with and optional argument is considered to be associated with the program. Some examples:

2021-10-25
in All, C programming, LibCapy examples,
97 views
Copyright 2021-2024 Baillehache Pascal