ArgParser

Synopsis

  1. #include <ts/ArgParser.h>

Description

ArgParser is a powerful and easy-to-use command line Parsing library for ATS. The program defines what it requires by adding commands and options. Then ArgParser will figure out the related information from the command line. All parsed arguments and function will be put in a key-value pairs structure Arguments available for users to use.

Usage

The usage of the ArgParser is straightforward. The user is expected to create an ArgParser for the program. Commands and options can be added to the parser with details including ENV variable, arguments expected, etc. After a single method parse(argv) is called, An object containing all information and parsed arguments available to use will be returned.

Create a parser

The first step to use ArgParser is to create a parser. The parser can be created simply by calling:

  1. ts::ArgParser parser;

or initialize with the following arguments: name, help description, environment variable, argument number expected, function

  1. ts::ArgParser parser("name", "description", "ENV_VAR", 0, &function);

To add the usage for the help message of this program:

  1. parser.add_global_usage("traffic_blabla [--SWITCH]");

Add commands and options

We can perform all kinds of operations on the parser which is the ArgParser or command which is a Command.

To add commands to the program or current command:

  1. ts::ArgParser &command1 = parser.add_command("command1", "description");
  2. command1.add_command("command2", "description");
  3. command1.add_command("command3", "description", "ENV_VAR", 0);
  4. command1.add_command("command4", "description", "ENV_VAR", 0, &function, "lookup_key");

This function call returns the new Command instance added.

Note

The 0 here is the number of arguments we expected. It can be also set to MORE_THAN_ZERO_ARG_N or MORE_THAN_ONE_ARG_N to specify that this command expects all the arguments come later (more than zero or more than one).

To add options to the parser or current command:

  1. parser.add_option("--switch", "-s", "switch description");
  2. command1.add_option("--switch", "-s", "switch description", "", 0);
  3. command1.add_option("--switch", "-s", "switch description", "ENV_VAR", 1, "default", "lookup_key");

This function call returns the new Option instance. (0 is also number of arguments expected)

We can also use the following chained way to add subcommand or option:

  1. command.add_command("init", "description").add_command("subinit", "description");
  2. command.add_command("remove", "description").add_option("-i", "--initoption");

which is equivalent to

  1. ts::ArgParser::Command &init_command = command.add_command("init", "description");
  2. ts::ArgParser::Command &remove_command = command.add_command("remove", "description");
  3. init_command.add_command("subinit", "description");
  4. remove_command.add_option("-i", "--initoption");

In this case, subinit is the subcommand of init and –initoption is a switch of command remove.

Parsing Arguments

ArgParser parses arguments through the parse(argv) method. This will inspect the command line and walk through it. A Arguments object will be built up from attributes parsed out of the command line holding key-value pairs all the parsed data and the function.

  1. Arguments args = parser.parse(argv);

Invoke functions

To invoke the function associated with certain commands, we can perform this by simply calling invoke() from the Arguments object returned from the parsing. The function can be a lambda.

  1. args.invoke();

Help message

  • Help message will be outputted when a wrong usage of the program is detected or –help option found.

Classes

class ArgParser

  • Option &add_option(std::string const &long_option, std::string const &short_option, std::string const &description, std::string const &envvar = “”, unsigned arg_num = 0, std::string const &default_value = “”, std::string const &key = “”)

    Add an option to current command with long name, short name, help description, environment variable, arguments expected, default value and lookup key. Return The Option object itself.

  • Command &add_command(std::string const &cmd_name, std::string const &cmd_description, std::function<void()> const &f = nullptrstd::string const &key = “”, )

    Add a command with only name and description, function to invoke and lookup key. Return the new Command object.

  • Command &add_command(std::string const &cmd_name, std::string const &cmd_description, std::string const &cmd_envvar, unsigned cmd_arg_num, std::function<void()> const &f = nullptrstd::string const &key = “”, )

    Add a command with name, description, environment variable, number of arguments expected, function to invoke and lookup key. The function can be passed by reference or be a lambda. It returns the new Command object.

  • void parse(const char **argv)

    Parse the command line by calling parser.parse(argv). Return the new Arguments instance.

  • void help_message() const

    Output usage to the console.

  • void add_global_usage(std::string const &usage)

    Add a global_usage for help_message(). Example: traffic_blabla [–SWITCH [ARG]].

  • void set_default_command(std::string const &cmd);

    Set a default command to the parser. This method should be called after the adding of the commands.

  • Command &require_commands()

    Make the parser require commands. If no command is found, output help message. Return The Command instance for chained calls.

  • void set_error(std::string e)

    Set the user customized error message for the parser.

  • std::string get_error() const

    Return the error message of the parser.

class Option

Option is a data struct containing information about an option.

  1. struct Option {
  2. std::string long_option; // long option: --arg
  3. std::string short_option; // short option: -a
  4. std::string description; // help description
  5. std::string envvar; // stored ENV variable
  6. unsigned arg_num; // number of argument expected
  7. std::string default_value; // default value of option
  8. std::string key; // look-up key
  9. };

class Command

Command is a nested structure of command for ArgParser. The add_option(), add_command() and require_command() are the same with those in ArgParser. When add_command() is called under certain command, it will be added as a subcommand for the current command. For Example, command1.add_command("command2", "description") will make command2 a subcommand of command1. require_commands() is also available within Command.

  • void add_example_usage(std::string const &usage)

    Add an example usage for the command to output in help_message. For Example: command.add_example_usage("traffic_blabla init --path=/path/to/file").

  • Command &set_default()

    set the current command as default

class Arguments

Arguments holds the parsed arguments and function to invoke. It basically contains a function to invoke and a private map holding key-value pairs. The key is the command or option name string and the value is the Parsed data object which contains the environment variable and arguments that belong to this certain command or option.

  • std::string get(std::string const &name)

    Return the ArgumentData object related to the name.

  • std::string set_env(std::string const &key, std::string const &value)

    Set the environment variable given key.

  • void append(std::string const &key, ArgumentData const &value)

    Append key-value pairs to the map in Arguments.

  • void append_arg(std::string const &key, std::string const &value)

    Append value to the data of key.

  • void show_all_configuration() const

    Show all the called commands, options, and associated arguments.

  • void invoke()

    Invoke the function associated with the parsed command.

  • void has_action() const

    return true if there is any function to invoke.

class ArgumentData

ArgumentData is a struct containing the parsed Environment variable and command line arguments. There are methods to get the data out of it.

Note: More methods can be added later to get specific types from the arguments.

  • operator bool() const noexcept

    bool for checking if certain command or option is called.

  • std::string const &operator[](int x) const

    Index accessing operator.

  • std::string const &env() const noexcept

    Return the environment variable associated with the argument.

  • std::vector<std::string>::const_iterator begin() const noexcept

    Begin iterator for iterating the arguments data.

  • std::vector<std::string>::const_iterator end() const noexcept

    End iterator for iterating the arguments data.

  • std::string const &at(unsigned index) const

    Index accessing method.

  • std::string const &value() const noexcept

    Return the first element of the arguments data.

  • size_t size() const noexcept

    Return the size of the arguments vector

  • size_t empty() const noexcept

    Return true if the arguments vector and env variable are both empty.

Example

ArgParser

Below is a short example of using the ArgParser. We add some options and some commands to it using different ways. This program will have such functionality:

  • --switch, -s as a global switch.
  • Command func will call function() and this command takes 2 argument.
  • Command func2 will call function2(int num).
  • Command init has subcommand subinit and option --path which take 1 argument.
  • Command remove has option --path which takes 1 argument and has HOME as the environment variable.
  1. #include "ts/ArgParser.h"
  2. void function() {
  3. ...
  4. }
  5. void function2(int num) {
  6. ...
  7. }
  8. int main (int, const char **argv) {
  9. ts::ArgParser parser;
  10. parser.add_global_usage("traffic_blabla [some stuff]");
  11. parser.add_option("--switch", "-s", "top level switch");
  12. parser.add_command("func", "some function", "ENV_VAR", 2, &function);
  13. parser.add_command("func2", "some function2", [&]() { return function2(100); });
  14. auto &init_command = parser.add_command("init", "initialize");
  15. init_command.add_option("--path", "-p", "specify the path", "", 1);
  16. init_command.add_command("subinit", "sub initialize");
  17. parser.add_command("remove", "remove things").add_option("--path", "-p", "specify the path", "HOME", 1);
  18. ts::Arguments parsed_data = parser.parse(argv);
  19. parsed_data.invoke();
  20. ...
  21. }

Arguments

To get the values from the arguments data, please refer to the methods in Arguments and ArgumentData