catchorg / Clara

A simple to use, composable, command line parser for C++ 11 and beyond
Boost Software License 1.0
650 stars 67 forks source link

Why does CommandLine need to be a template? #10

Closed vadz closed 7 years ago

vadz commented 8 years ago

This is not really an issue, more of a question, but as there doesn't seem to be any better place to discuss it, let me start it here: why do we need to use ConfigT as template parameter everywhere? It is indeed good practice to collect all the command line options values in one struct, but looking at my existing projects (which I am thinking of migrating to Clara), I see that quite a few of them don't actually do it like this and just use local variables. And there doesn't seem to be any reason not support it in Clara too, i.e. it seems to me that the following should be possible:

int main(int argc, char const* argv[])
{
    struct Options {
        bool foo = false;
    };

    Clara::CommandLine cl; // not a template!

    // It looks like this could still be made to work:
    Options opts;
    cl["-f"].bind(&Options::foo, opts);

    // But we could also have this:
    bool bar = false;
    cl["-b"].bind(&bar);
    cl.parse(argc, argv);
}

Granted, implementing #1 would make this less critical as I could then do

  cl["-b"].bind([&](bool value) { bar = value; });

but it would still be nice to have direct support for this IMHO.

philsquared commented 7 years ago

I'm going to close this as the rework in Clara 1.0 makes this a moot point (the CommandLine template no longer exists - each option and argument is a separate parser - non-templated - and you don't need to supply a config object type - you just bind individual variables or lambdas - which give you your final "nice have point" request :-) )