Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Command Line Options

Classes

class  EnumOption< T >
 
class  ListOption< T >
 
class  OptionException
 
class  Manager
 
class  Option
 
class  StringList
 
class  SwitchOption
 
class  ValueOption< T >
 

Macros

#define ELM_RUN(C)   int main(int argc, char **argv) { return C().manage(argc, argv); }
 

Detailed Description

#include <elm/options.h>

ELM provides several classes to manage efficiently and easily command line options. There are currently two ways to use these classes, the old approach that will become quickly deprecated (please no more use it) and the new one.

To perform the parsing of command line arguments, ELM provides two basic classes. elm::option::Manager represents the command as a base of documentation and as a list of options items. The manager is initialized as in the following example:

#include <elm/options.h>
...
option::Manager manager = Make("my command", Version(1, 0, 0))
.description("This is my command !")
.license("GPL V")
.author("me <me@here.there>");

According to your needs, as many configurations as needed can be passed to the Make() object while maintaining type checking. Refer to elm::option::Manager::Make class documentation for the list of available configurations.

The options works in the same way:

option::SwitchOption opt1 = Make(manager).cmd("-o").cmd("--com").help("option 1");

As many ".cmd()" method calls can be added and other configurations can be passed using the same syntax. Refer to the documentations of the different option classes to get details about the configuration items (elm::option::Option::Make, ...).

Using both configuration system for the manager and the option, a command is usually defined as in the following example:

class MyCommand: public option::Manager {
public:
MyCommand(void): option::Manager(Make("my-command", Version(1, 0, 0))
.description("This is my command !")
.license("GPL V")
.author("me <me@here.there>")),
opt(option::SwitchOption::Make(*this).cmd("-o").cmd("--com").help("option 1")),
val(option::ValueOption::Make(*this).cmd("-v").cmd("--val").help("my value")) { }
...
private:
option::SwitchOption opt;
option::ValueOption val;
};

At this point, the options may be used as normal values in the program as below:

if(opt)
doSomething(val);

Free arguments, not processed by an option item, are usually handled by overriding the method

class MyCommand: public option::Manager {
public:
...
protected:
virtual void process(String arg) {
if(!my_arg)
my_arg = arg;
else
throw OptionException("only one argument supported !");
}
...
private:
string my_arg;
};

The example above supports only one argument.In this example, if the argument is not defined, the argument arg is copied to the field my_arg. If it is already defined, an OptionException is raised. In this case, the passed message will be displayed to the user and the syntax of the command will also automatically be displayed then.

To use such a command, the main parameters, argc and argv, must be passed to the parse() method:

int main(int argc, char *argv[]) {
MyCommand cmd;
return cmd.parse(argc, argv);
}

This may be replaced by the short-cut:

ELM_RUN(MyCommand)

The classes allowed are:

Macro Definition Documentation

◆ ELM_RUN

#define ELM_RUN (   C)    int main(int argc, char **argv) { return C().manage(argc, argv); }

#include <include/elm/option/Manager.h>

This macro is a short to a main function running an application class. The parameter must be the name of a class extending option::Manager. It replaces the mandatory main() function of a C++ application.

Parameters
CName of the application class.
Make
ELM_RUN
#define ELM_RUN(C)