Skip to content

Module: Argument Parser

smehringer edited this page Mar 14, 2017 · 19 revisions

Links:

Possible interface:

// positional option
// in order, required (except if last one is a list?)
add_positional_option(value_type & save_place,      // myOptions.outputFile (std::string)
                      std::function<bool(value_type const&)> && validator = [] (value_type const&) { return true; },
                      std::string const & description
                      );

// non-valued option (existance is true/false)
add_flag(char const short_id,            // -f
         std::string const & long_id,    // --fast
         bool & save_place,              // myOptions.fastMode (bool)
         std::string const & description // "fast mode is switched on"
         );

// valued option
add_option(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; },
           std::string const & description // "give the name of an output file."
           );

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