Skip to content

Module: Argument Parser

Hannes Hauswedell edited this page Mar 6, 2017 · 19 revisions

Links:

Possible interface:

// positional, in order and required parameter
addArg(value_type & save_place,      // myOptions.outputFile (std::string)
       std::function<bool(value_type const&)> && validator = [] (value_type const&) { return true; } );

// optional, non-valued parameter (existance is true/false)
addFlag(char const short_id,           // -f
        std::string const & long_id,   // --fast
        bool & save_place,             // myOptions.fastMode (bool)
        );

// optional, valued parameter
addParameter(char const short_id,          // -o
             std::string const & long_id,  // --output
             value_type & save_place,      // myOptions.outputFile (std::string)
             std::function<bool(value_type const&)> && validator = [] (value_type const&) { return true; } );

savePlace would be an existing variable that also defines the type of the argument. savePlace can be set to a default value upon definition/creation / outside of the addArgument function. validator is a callable that verifies the argument. It can be user-specified, but there shall be pre-defined validators for integral ranges (takes a pair of integral) or file extensions (takes a container of std::string).

Wishlist from h-2:

  • also support enums as TValue and offer a validator that accepts the enum labels as valid strings. This will be a little tricky to implement, but one could check how e.g. Cereal get string values from enum labels.
  • the input_file_validator (and output_file_validator and directory...) shall check via http://en.cppreference.com/w/cpp/filesystem that the file/directory exists / is readable / writeable et cetera.
Clone this wiki locally