p-ranav / argparse

Argument Parser for Modern C++
MIT License
2.72k stars 250 forks source link

Support prefix style arguments #320

Open MinekPo1 opened 11 months ago

MinekPo1 commented 11 months ago

GCC allows for providing arguments to sub-processes via a syntax which I would like to replicate: it uses a prefix as send this to a sub process. As an example -D is used to define a macro (ex -DDEBUG defines a macro named DEBUG).

Currently, at least to my knowledge, such usage is not supported (aside from inheriting from ArgumentParser and implementing it), which is a shame in my opinion.

There are three main ways, that I see, that this feature could be designed as:

Modified Argument approach

Example code:

// ...
program.add_argument("--p")
  .make_prefix();
// ...
auto input = program.get<std::string>("--p");
// ...

This would however likely be harder to implement, or would, under the hood, use the second approach anyway.

Separate trait approach

Example code:

// ...
program.add_prefix("--p");
// ...
auto input = program.get<std::string>("--p");
// ...

Slightly more straight-forward to implement, likely would better represent what is happening under the hood, though it seems off to me. It also opens the door for more option related to the prefix, like not using the normal prefix chars.

Hook approach

Example code:

// ...
std::string value;
program.set_unknown_argument_hook([](std::string argument) -> bool {
    if (!argument.substr(0,3) == "--p")
        return false;
    value = argument.substr(3);
    return true;
});
// ...

Likely easiest to implement, and gives room for other creativity, but very hacky feeling and more code needed on the side of the user.