Skip to content

Latest commit

 

History

History
364 lines (239 loc) · 6.9 KB

argument.md

File metadata and controls

364 lines (239 loc) · 6.9 KB

Argument

flag

Argument& flag(const char* flag__);

Add flag in argument object.

Example:

Args args;
args.addArgument("-f").flag("--foo");

action

Argument& action(enum Action::eAction action__);

Add a action when this argument is encountered at the command line.

Action list:

NONE

This just stores the argument’s value. This is the default action.
Example at examples.md/none.

APPEND

This stores a list, and appends each argument value to the list. It is useful to allow an option to be specified multiple times. If the default value is non-empty, the default elements will be present in the parsed value for the option, with any values from the command line appended after those default values.
Example at examples.md/append.

EXTEND

This stores a list, and extends each argument value to the list.
Example at examples.md/extend.

HELP

This case used for create the help flag.
⚠ Can only use this action after constructor with false parameter or remove the last help flags.

blet::Args args(false);
// or
// blet::Args args;
// args.removeArgument(args.vector("-h", "--help"));

Example at examples.md/help.

INFINITE

This stores a list.
Example at examples.md/infinite.

STORE_FALSE

This case used for storing the values false respectively.
Example at examples.md/storefalse.

STORE_TRUE

This case used for storing the values true respectively.
Example at examples.md/storetrue.

VERSION

This case used for define the version flag.
Example at examples.md/version.

help

Argument& help(const char* help__);

Set the help description massge for this argument.

required

Argument& required(bool required__);

Whether or not the command-line argument may be omitted.

metavar

Argument& metavar(const char* metavar__);

A name for the argument in usage messages.

nargs

Argument& nargs(std::size_t nargs__);

The number of command-line arguments that should be consumed by this object.

defaults

Argument& defaults(const Vector& defaults__);

Define the defaults string values.

valid

Argument& valid(IValid* pValid, bool isDeletable = true);

You can check format of argument with IValid interface.
Example of Custom Valid at examples.md/custom-valid-transform.

ValidNumber

args.addArgument("--arg").valid(new blet::Args::ValidNumber());

Check if arguments are number.

ValidMinMax

args.addArgument("--arg").valid(new blet::Args::ValidMinMax(0, 100));

Check if arguments are number and if in range of min-max.

ValidChoise

args.addArgument("--arg").valid(new blet::Args::ValidChoise(args.vector("foo", "bar")));

Check if arguments are in choise.

ValidPath

args.addArgument("--arg").valid(new blet::Args::ValidPath());
args.addArgument("--arg").valid(new blet::Args::ValidPath(blet::Args::ValidPath::IS_DIR));
args.addArgument("--arg").valid(new blet::Args::ValidPath(blet::Args::ValidPath::IS_FILE));

Check if arguments are valid path/dir/file.

dest

template<typename T>
Argument& dest(std::vector<std::vector<T> >& dest, void (*toDest)(std::vector<std::vector<T> >& dest, bool isExist, const std::vector<std::vector<std::string> >& arguments) = NULL);
template<typename T>
Argument& dest(std::vector<T>& dest, void (*toDest)(std::vector<T>& dest, bool isExist, const std::vector<std::string>& arguments) = NULL);
template<typename T>
Argument& dest(T& dest, void (*toDest)(T& dest, bool isExist, const std::string& argument) = NULL);

Define a reference of object for insert the value after parseArguments method.
Action can be changed by toDest parameter with your function.

unsigned long value;
args.addArgument("--arg").dest(value);

Examples at dest.md.

getString

std::string getString() const;

Get the string format of this argument.

getDefault

const std::string& getDefault() const;

Get the default value of argument.

isNumber

bool isNumber() const;

Check if this argument is a number ("1234aaa" is true with 1234 like number).

getNumber

double getNumber() const;

Get Number if isNumber.

isExists

bool isExists() const;

After parseArguments method check if this argument is present in argv.

isRequired

bool isRequired() const;

Get required option.

count

std::size_t count() const;

After parseArguments method check if number of this argument in argv.

getNargs

std::size_t getNargs() const;

Get nargs option.

getHelp

const std::string& getHelp() const;

Get help option.

getMetavar

const std::string& getMetavar() const;

Get metavar option.

getNameOrFlags

const std::vector<std::string>& getNameOrFlags() const;

Get the name or flag(s) of this argument.

getDefaults

const std::vector<std::string>& getDefaults() const

Get the default(s) value(s) of this argument.

getAction

Action::eAction getAction() const;

Get action option.

operator bool()

operator bool() const;

Operator constructor bool, check if argument object isExists.
For action STORE_FALSE the effect is inversed.

Example:

if (args["--option"]) { // call operator bool()
    std::cout << args["--option"] << std::endl;
}

operator std::string()

operator std::string() const;

Call getString method.

Example:

std::string option = args["--option"];

operator std::vector<std::string>()

operator std::vector<std::string>() const;

If the argument object contains a lot of one argument, you can get a std::vector of std::string.

Example:

std::vector<std::string> options = args["--options"];

operator std::vector<std::vector<std::string> >()

operator std::vector<std::vector<std::string> >() const;

If the argument object contains a lot of one argument with number of argument (nargs) bigger than one, you can get a std::vector of std::vector of std::string.

Example:

std::vector<std::vector<std::string> > options = args["--options"];

operator T()

template<typename T>
operator T() const;

Call the getNumber method with your custom type.

Example:

double option = args["--option"];