| .. Licensed to the Apache Software Foundation (ASF) under one | |
| or more contributor license agreements. See the NOTICE file | |
| distributed with this work for additional information | |
| regarding copyright ownership. The ASF licenses this file | |
| to you under the Apache License, Version 2.0 (the | |
| "License"); you may not use this file except in compliance | |
| with the License. You may obtain a copy of the License at | |
| http://www.apache.org/licenses/LICENSE-2.0 | |
| Unless required by applicable law or agreed to in writing, | |
| software distributed under the License is distributed on an | |
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| KIND, either express or implied. See the License for the | |
| specific language governing permissions and limitations | |
| under the License. | |
| .. include:: ../../common.defs | |
| .. highlight:: cpp | |
| .. default-domain:: cpp | |
| .. _ArgParser: | |
| ArgParser | |
| ********* | |
| Synopsis | |
| ++++++++ | |
| .. code-block:: cpp | |
| #include <ts/ArgParser.h> | |
| Description | |
| +++++++++++ | |
| :class:`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 :class:`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 | |
| :class:`Arguments` available for users to use. | |
| Usage | |
| +++++ | |
| The usage of the ArgParser is straightforward. An :code:`ArgParser` instance is | |
| created. Commands and options are then added to this instance, along with | |
| details including ENV variable, arguments expected, etc. The method | |
| :code:`parse(argv)` is called, passing the :code:`argv` from the command line | |
| (:code:`main` function arguments). An object containing the parsed command line | |
| information 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: | |
| .. code-block:: cpp | |
| ts::ArgParser parser; | |
| or initialize with the following arguments: | |
| *name, help description, environment variable, argument number expected, function* | |
| .. code-block:: cpp | |
| ts::ArgParser parser("name", "description", "ENV_VAR", 0, &function); | |
| To add the usage for the help message of this program: | |
| .. code-block:: cpp | |
| parser.add_global_usage("traffic_blabla [--SWITCH]"); | |
| Add commands and options | |
| ------------------------ | |
| We can perform all kinds of operations on the parser which is the :class:`ArgParser` or command which is a :class:`Command`. | |
| To add commands to the program or current command: | |
| .. code-block:: cpp | |
| ts::ArgParser &command1 = parser.add_command("command1", "description"); | |
| command1.add_command("command2", "description"); | |
| command1.add_command("command3", "description", "ENV_VAR", 0); | |
| command1.add_command("command4", "description", "ENV_VAR", 0, &function, "lookup_key"); | |
| This function call returns the new :class:`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: | |
| .. code-block:: cpp | |
| parser.add_option("--switch", "-s", "switch description"); | |
| command1.add_option("--switch", "-s", "switch description", "", 0); | |
| command1.add_option("--switch", "-s", "switch description", "ENV_VAR", 1, "default", "lookup_key"); | |
| This function call returns the new :class:`Option` instance. (0 is also number of arguments expected) | |
| .. Note:: | |
| For options, the number of arguments may also be one of the following, which mirror the | |
| ``nargs`` values of Python's ``argparse``: | |
| ================================ ======================================================= | |
| Value Meaning | |
| ================================ ======================================================= | |
| ``AT_MOST_ONE_ARG_N`` Zero or one value (``argparse`` ``nargs='?'``) | |
| ``MORE_THAN_ZERO_ARG_N`` Zero or more values (``argparse`` ``nargs='*'``) | |
| ``MORE_THAN_ONE_ARG_N`` One or more values (``argparse`` ``nargs='+'``) | |
| ================================ ======================================================= | |
| An option taking a variable number of values stops collecting when it reaches a token | |
| naming another option of the same command, so options written afterwards keep their own | |
| arguments. Use ``AT_MOST_ONE_ARG_N`` rather than ``MORE_THAN_ZERO_ARG_N`` for an option | |
| whose value is optional, otherwise it also consumes the positional arguments of its | |
| command. | |
| A token naming another option is not a value for a fixed number of arguments either. An | |
| option written where a value is expected leaves the value missing, which is reported as a | |
| usage error rather than the option being consumed and applied as the value. | |
| Because collection stops at the following option, an option taking an unbounded number of | |
| values may be written more than once, and the occurrences accumulate. This matches the | |
| ``--option=value`` form, which has always appended. An option taking a fixed number of | |
| values keeps its last-one-wins behaviour instead, and ``AT_MOST_ONE_ARG_N`` reports a | |
| repetition as a usage error since it permits only one value in total. | |
| An empty token is not a value for ``AT_MOST_ONE_ARG_N``. It is reported as a missing | |
| argument rather than read as the option having been given without one, so a value taken | |
| from an unset variable cannot silently select the declared default. | |
| A ``--`` token stops option recognition for the values being collected, which is how a | |
| value beginning with ``-`` is passed. Note this differs from the POSIX ``--``: it does | |
| not end the value list nor force the remainder to be positional arguments. | |
| Option recognition stays off for the rest of that collection, so for a variable number of | |
| values every remaining token becomes a value and no later option is recognized. Use the | |
| ``--option=value`` form instead when options still have to follow a value that begins with | |
| ``-``. | |
| The escaped token is also left alone by the option scan of every command on the line, | |
| including the one that owns it. Each of those scans covers the whole remaining command line, | |
| so without this a value spelled like an option of another command would be taken as that | |
| option rather than reaching the command that asked for it. Only the single token right after | |
| the ``--`` is protected this way, whatever the arity of its owner. A second or later value | |
| after the ``--`` is still matched against the options of the enclosing commands, ``--help`` | |
| and ``--version`` included, and when it is a further positional argument of a command, | |
| against that command's own options as well. Put the ``--`` directly before the value that | |
| begins with ``-``. That works for one such value per option or command: a second ``--`` in | |
| the same collection is taken as a value, since the first one already turned option | |
| recognition off. Where more than one value begins with ``-``, use the ``--option=value`` form | |
| for the values that belong to an option. | |
| .. Note:: | |
| When a command or option is written on the command line but given no value at all, a | |
| declared environment variable fills it in, so a caller reading the value through | |
| :func:`ArgumentData::value` or :func:`ArgumentData::at` needs no special handling. This | |
| applies whether the value is missing because the option takes a fixed number of arguments, | |
| ``AT_MOST_ONE_ARG_N``, or ``MORE_THAN_ONE_ARG_N`` collected none. The resolution order is: | |
| #. the value typed on the command line, if any; | |
| #. the declared environment variable, if set; | |
| #. the declared default value. | |
| The variable is consulted only when the command or option is actually written, and only | |
| when it is given no value at all; it never activates one that was never typed, and it never | |
| joins a value that was typed. For a fixed number of arguments above one, this means the | |
| variable fills every slot only when none of them were given; a partial set is a usage error | |
| just as it would be without a variable declared. | |
| An option may be written more than once. The variable applies only when every occurrence is | |
| bare: if any occurrence is given a value, in either the ``--option value`` or | |
| ``--option=value`` spelling, a bare occurrence of the same option is the usage error it | |
| would be without a variable at all, regardless of which occurrence comes first. A line with | |
| no bare occurrence is untouched, including the last-one-wins behaviour a fixed-arity option | |
| already has when typed more than once. ``AT_MOST_ONE_ARG_N`` cannot reach this case, since it | |
| already refuses repetition outright. | |
| ``MORE_THAN_ZERO_ARG_N`` does not support this fallback: zero values is already valid input | |
| for it, so there is no usage error for a variable to replace. | |
| :func:`ArgumentData::env` keeps returning the variable's raw value regardless of whether it | |
| was used to fill a value. | |
| We can also use the following chained way to add subcommand or option: | |
| .. code-block:: cpp | |
| command.add_command("init", "description").add_command("subinit", "description"); | |
| command.add_command("remove", "description").add_option("-i", "--initoption"); | |
| which is equivalent to | |
| .. code-block:: cpp | |
| ts::ArgParser::Command &init_command = command.add_command("init", "description"); | |
| ts::ArgParser::Command &remove_command = command.add_command("remove", "description"); | |
| init_command.add_command("subinit", "description"); | |
| remove_command.add_option("-i", "--initoption"); | |
| In this case, `subinit` is the subcommand of `init` and `--initoption` is a switch of command `remove`. | |
| Mutually Exclusive Groups | |
| -------------------------- | |
| ArgParser supports mutually exclusive option groups, where only one option from the group can be used at a time. | |
| This is useful for options that conflict with each other, such as ``--verbose`` and ``--quiet``, or ``--enable`` and ``--disable``. | |
| To create a mutually exclusive group: | |
| .. code-block:: cpp | |
| // Create a mutex group (optional by default) | |
| parser.add_mutex_group("verbosity", false, "Verbosity level"); | |
| // Add options to the group | |
| parser.add_option_to_group("verbosity", "--verbose", "-v", "Enable verbose output"); | |
| parser.add_option_to_group("verbosity", "--quiet", "-q", "Suppress output"); | |
| The second parameter to ``add_mutex_group()`` specifies whether the group is required (``true``) or optional (``false``). | |
| If required, the user must specify exactly one option from the group. If optional, the user may specify zero or one option. | |
| When multiple options from the same group are used together, ArgParser will display an error message and exit. | |
| Example with a required group: | |
| .. code-block:: cpp | |
| // Create a required mutex group for output format | |
| parser.add_mutex_group("format", true, "Output format (required)"); | |
| parser.add_option_to_group("format", "--json", "-j", "JSON format"); | |
| parser.add_option_to_group("format", "--xml", "-x", "XML format"); | |
| // User must specify either --json or --xml, but not both | |
| Option Dependencies | |
| ------------------- | |
| ArgParser supports option dependencies, where one option requires another option to be present. | |
| This is useful when an option only makes sense in combination with another option. | |
| To specify that an option requires another option, use the ``with_required()`` method immediately after | |
| adding the option: | |
| .. code-block:: cpp | |
| command.add_option("--tags", "-t", "Debug tags", "", 1) | |
| command.add_option("--append", "-a", "Append tags to existing tags") | |
| .with_required("--tags"); // --append requires --tags to be present | |
| When ``--append`` is used without ``--tags``, ArgParser will display an error message and exit: | |
| .. code-block:: text | |
| Error: Option '--append' requires '--tags' to be specified | |
| Multiple dependencies can be specified by chaining ``with_required()`` calls: | |
| .. code-block:: cpp | |
| command.add_option("--verbose-append", "-V", "Verbose append mode") | |
| .with_required("--tags") | |
| .with_required("--append"); // requires both --tags and --append | |
| .. Note:: | |
| The ``with_required()`` method must be called immediately after ``add_option()`` or | |
| ``add_option_to_group()``. It applies to the most recently added option. | |
| Parsing Arguments | |
| ----------------- | |
| :class:`ArgParser` parses arguments through the :code:`parse(argv)` method. This will inspect the command line | |
| and walk through it. A :class:`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. | |
| .. code-block:: cpp | |
| Arguments args = parser.parse(argv); | |
| Invoke functions | |
| ---------------- | |
| To invoke the function associated with certain commands, we can perform this by simply calling :code:`invoke()` | |
| from the :class:`Arguments` object returned from the parsing. The function can be a lambda. | |
| .. code-block:: cpp | |
| args.invoke(); | |
| Help and Version messages | |
| ------------------------- | |
| - Help message will be outputted when a wrong usage of the program is detected or `--help` option found. | |
| - Version message is defined unified in :code:`ArgParser::version_message()`. | |
| Classes | |
| +++++++ | |
| .. class:: ArgParser | |
| .. function:: 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*. | |
| If no *lookup key* is provided, the *long name* will be used as lookup key without the prefix, for instance, for a long option ``--debug`` you should use ``debug`` as | |
| as lookup key. Return The Option object itself. | |
| .. function:: Command &add_command(std::string const &cmd_name, std::string const &cmd_description, std::function<void()> const &f = nullptr, std::string const &key = "") | |
| Add a command with only *name* and *description*, *function to invoke* and *lookup key*. Return the new :class:`Command` object. | |
| .. function:: 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 = nullptr, std::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 :class:`Command` object. | |
| .. function:: void parse(const char **argv) | |
| Parse the command line by calling :code:`parser.parse(argv)`. Return the new :class:`Arguments` instance. | |
| .. function:: void help_message() const | |
| Output usage to the console. | |
| .. function:: void version_message() const | |
| Output version string to the console. | |
| .. function:: void add_global_usage(std::string const &usage) | |
| Add a global_usage for :code:`help_message()`. Example: `traffic_blabla [--SWITCH [ARG]]`. | |
| .. function:: 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. | |
| .. function:: Command &require_commands() | |
| Make the parser require commands. If no command is found, output help message. Return The :class:`Command` instance for chained calls. | |
| .. function:: void set_error(std::string e) | |
| Set the user customized error message for the parser. | |
| .. function:: std::string get_error() const | |
| Return the error message of the parser. | |
| .. function:: void add_mutex_group(std::string const &group_name, bool required, std::string const &description) | |
| Create a mutually exclusive option group. Only one option from the group can be used at a time. | |
| If *required* is true, exactly one option from the group must be specified. | |
| .. function:: void add_option_to_group(std::string const &group_name, std::string const &long_option, std::string const &short_option, std::string const &description) | |
| Add an option to a mutually exclusive group. The option will be created and associated with the specified group. | |
| .. class:: Option | |
| :class:`Option` is a data struct containing information about an option. | |
| .. code-block:: cpp | |
| struct Option { | |
| std::string long_option; // long option: --arg | |
| std::string short_option; // short option: -a | |
| std::string description; // help description | |
| std::string envvar; // stored ENV variable | |
| unsigned arg_num; // number of argument expected | |
| std::string default_value; // default value of option | |
| std::string key; // look-up key | |
| }; | |
| .. class:: Command | |
| :class:`Command` is a nested structure of command for :class:`ArgParser`. The :code:`add_option()`, :code:`add_command()` and | |
| :code:`require_command()` are the same with those in :class:`ArgParser`. When :code:`add_command()` | |
| is called under certain command, it will be added as a subcommand for the current command. For Example, :code:`command1.add_command("command2", "description")` | |
| will make :code:`command2` a subcommand of :code:`command1`. :code:`require_commands()` is also available within :class:`Command`. | |
| .. function:: Command &add_example_usage(std::string const &usage) | |
| Add an example usage for the command to output in ``help_message``. This method can be | |
| called multiple times to add multiple examples. Returns the Command instance for chained calls. | |
| Example:: | |
| command.add_example_usage("traffic_ctl server debug enable -t my_tags") | |
| .add_example_usage("traffic_ctl server debug enable -t new_tag -a # append mode"); | |
| This will output in help: | |
| .. code-block:: text | |
| Example Usage: | |
| traffic_ctl server debug enable -t my_tags | |
| traffic_ctl server debug enable -t new_tag -a # append mode | |
| .. function:: Command &set_default() | |
| set the current command as default | |
| .. function:: void add_mutex_group(std::string const &group_name, bool required, std::string const &description) | |
| Create a mutually exclusive option group for this command. | |
| .. function:: void add_option_to_group(std::string const &group_name, std::string const &long_option, std::string const &short_option, std::string const &description) | |
| Add an option to a mutually exclusive group for this command. | |
| .. function:: Command &with_required(std::string const &required_option) | |
| Specify that the last added option requires another option to be present. | |
| Must be called immediately after ``add_option()`` or ``add_option_to_group()``. | |
| Returns the Command instance for chained calls. | |
| .. class:: Arguments | |
| :class:`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. | |
| .. function:: std::string get(std::string const &name) | |
| Return the :class:`ArgumentData` object related to the name. | |
| .. function:: std::string set_env(std::string const &key, std::string const &value) | |
| Set the environment variable given `key`. | |
| .. function:: void append(std::string const &key, ArgumentData const &value) | |
| Append key-value pairs to the map in :class:`Arguments`. | |
| .. function:: void append_arg(std::string const &key, std::string const &value) | |
| Append `value` to the data of `key`. | |
| .. function:: void show_all_configuration() const | |
| Show all the called commands, options, and associated arguments. | |
| .. function:: void invoke() | |
| Invoke the function associated with the parsed command. | |
| .. function:: void has_action() const | |
| return true if there is any function to invoke. | |
| .. class:: ArgumentData | |
| :class:`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. | |
| .. function:: operator bool() const noexcept | |
| `bool` for checking if certain command or option is called. | |
| .. function:: std::string const &operator[](int x) const | |
| Index accessing operator. | |
| .. function:: std::string const &env() const noexcept | |
| Return the environment variable associated with the argument. | |
| .. function:: std::vector<std::string>::const_iterator begin() const noexcept | |
| Begin iterator for iterating the arguments data. | |
| .. function:: std::vector<std::string>::const_iterator end() const noexcept | |
| End iterator for iterating the arguments data. | |
| .. function:: std::string const &at(unsigned index) const | |
| Index accessing method. | |
| .. function:: std::string const &value() const noexcept | |
| Return the first element of the arguments data. | |
| .. function:: size_t size() const noexcept | |
| Return the size of the arguments vector | |
| .. function:: 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. | |
| .. code-block:: cpp | |
| #include "ts/ArgParser.h" | |
| void function() { | |
| ... | |
| } | |
| void function2(int num) { | |
| ... | |
| } | |
| int main (int, const char **argv) { | |
| ts::ArgParser parser; | |
| parser.add_global_usage("traffic_blabla [some stuff]"); | |
| parser.add_option("--switch", "-s", "top level switch"); | |
| parser.add_command("func", "some function", "ENV_VAR", 2, &function); | |
| parser.add_command("func2", "some function2", [&]() { return function2(100); }); | |
| auto &init_command = parser.add_command("init", "initialize"); | |
| init_command.add_option("--path", "-p", "specify the path", "", 1); | |
| init_command.add_command("subinit", "sub initialize"); | |
| parser.add_command("remove", "remove things").add_option("--path", "-p", "specify the path", "HOME", 1); | |
| ts::Arguments parsed_data = parser.parse(argv); | |
| parsed_data.invoke(); | |
| ... | |
| } | |
| Arguments | |
| --------- | |
| To get the values from the arguments data, please refer to the methods in :class:`Arguments` and :class:`ArgumentData` |