p-ranav / argparse

Argument Parser for Modern C++
MIT License
2.59k stars 244 forks source link

Add Argument::store_into() functions #331

Closed rouault closed 5 months ago

rouault commented 5 months ago

It is possible to bind arguments to a variable storing their value, as an alternative to explicitly calling program.get<T>(arg_name) or program[arg_name]

This is currently implementeted for variables of type bool (this also implicitly calls flag()), int, double, std::string and std::vector<std::string>. If the argument is not specified in the command line, the default value (if set) is set into the variable.

bool flagvar = false;
program.add_argument("--flagvar").store_into(flagvar);

int intvar = 0;
program.add_argument("--intvar").store_into(intvar);

double doublevar = 0;
program.add_argument("--doublevar").store_into(doublevar);

std::string strvar;
program.add_argument("--strvar").store_into(strvar);

std::vector<std::string> strvar_repeated;
program.add_argument("--strvar-repeated").append().store_into(strvar_repeated);

std::vector<std::string> strvar_multi_valued;
program.add_argument("--strvar-multi-valued").nargs(2).store_into(strvar_multi_valued);