muellan / clipp

easy to use, powerful & expressive command line argument parsing for modern C++ / single header / usage & doc generation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Don't duplicate documentation for re-used options

brett-harrison opened this issue · comments

Is it possible to dedup args that are listed multiple times in different commands? Here's an example:

bool val1, val2, val3;
auto arg1 = required("--arg1").set(val1) % "doc for arg1";
auto arg2 = required("--arg2").set(val2) % "doc for arg2";
auto arg3 = required("--arg3").set(val3) % "doc for arg3";

auto cli = (command("cmd1"), arg1, arg2) | (command("cmd2"), arg2, arg3) | (command("cmd3"), arg1, arg3);
SYNOPSIS
        prog cmd1 --arg1 --arg2
        prog cmd2 --arg2 --arg3
        prog cmd3 --arg1 --arg3

OPTIONS
        --arg1      doc for arg1
        --arg2      doc for arg2
        --arg2      doc for arg2
        --arg3      doc for arg3
        --arg1      doc for arg1
        --arg3      doc for arg3

Do I understand it correctly that you want to avoid that the documentation is listed multiple times?
Since all parameters are copied, arg1 behind cmd1 really is a different object from arg1 behind cmd3.
Currently I don't check for redundancy while generating the option documentation but it is already on my todo list.

The only quick solution I can think of is to add the documentation only once.
So either:

bool val1, val2, val3;
auto arg1 = required("--arg1").set(val1) % "doc for arg1";
auto arg2 = required("--arg2").set(val2) % "doc for arg2";
auto arg3 = required("--arg3").set(val3) % "doc for arg3";

auto cli = (command("cmd1"), arg1, arg2) | (command("cmd2"), arg2 % "", arg3) | (command("cmd3"), arg1 % "", arg3 % "");

or

bool val1, val2, val3;
auto arg1 = required("--arg1").set(val1);
auto arg2 = required("--arg2").set(val2);
auto arg3 = required("--arg3").set(val3);
auto cli = 
    (command("cmd1"), arg1  % "doc for arg1", arg2 % "doc for arg2") | 
    (command("cmd2"), arg2, arg3) | 
    (command("cmd3"), arg1, arg3 % "doc for arg3");

Yes you understand correctly. Thanks for looking into this.
One alternative could be allowing a param_filter to be constructed from a lambda which takes the arg_string as an argument, and I could store my own set<string> that's captured by the lambda and returns false if I've already seen that arg string and stored it in my set.

Or perhaps make operator() virtual and derive uniq_param_filter from param_filter, adding unordered_set<string> to check on flags().