Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ambiguous help, short name duplicates #16

Open
rysson opened this issue Nov 30, 2020 · 4 comments
Open

Ambiguous help, short name duplicates #16

rysson opened this issue Nov 30, 2020 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@rysson
Copy link

rysson commented Nov 30, 2020

I seems that first character of long-name is used in a short-name.

In case:

struct Options
{
  // oositional flags
  // ./main [--bar] [--baz]
  std::optional<bool> bar = false;
  std::optional<bool> baz = false;
};
STRUCTOPT(Options, bar, baz);

Help prints:

USAGE: my_app [FLAGS] [OPTIONS] 

FLAGS:
    -b, --bar
    -b, --baz

OPTIONS:
    -h, --help <help>
    -v, --version <version>

There are -b twice. Ii can be confusing, specially with many another options between duplicates.

BTW, it's possible (or is planned) to set flag name (short name)?

Maybe something like

std::optional<bool> baz = false;
...
STRUCTOPT(Options, bar, structopt::short(baz, "z"));
@p-ranav
Copy link
Owner

p-ranav commented Nov 30, 2020

I've been wondering for awhile how to solve this - being able to provide short names, help descriptions etc.

Using it as part of the STRUCTOPT macro makes sense. Thanks for the suggestion!

@p-ranav p-ranav self-assigned this Nov 30, 2020
@p-ranav p-ranav added the enhancement New feature or request label Nov 30, 2020
@luigifcruz
Copy link

I like this idea. Custom help strings can be added to individual arguments this way too.

@nathan-b
Copy link

I would like to +1 this idea. Any idea if it will get implemented?

@sixprime
Copy link

You should check out my fork of visit_struct : https://github.com/sixprime/visit_struct.

I added the capability of adding metadata to any struct member being visited. This can technically allow things such as :

struct my_type {
  int a = 666;
  float b = 42.0f;
  std::string c;
};

struct arg_opt {
    std::optional<std::string> help;
    int priority = 0;
};

struct arg_special {
    bool is_valid = false;
};

VISITABLE_STRUCT_METADATA(my_type,
  METADATA(a, arg_opt { .help = "help message for A", .priority = 1 }),
  METADATA(b, arg_opt { .help = "help message for B", .priority = 2 }),
  METADATA(c, arg_special { .is_valid = true }));

struct debug_printer {
  template <typename T>
    void operator()(const char* name, const T& value, const arg_opt& ao) {
        std::cerr << name << ": " << value << " [ desc=" << ao.help.value_or("<none>")
            << ", priority=" << ao.priority << "]" << std::endl;
    }

    template <typename T, typename M>
    void operator()(const char* name, const T& value, const M& /*metadata*/) {
        std::cerr << name << ": " << value << std::endl;
    }
};

void debug_print(const my_type & my_struct) {
  visit_struct::for_each(my_struct, debug_printer{});
}

Now, in theory, this can open the door to custom help messages, argument groups, custom short names, etc. And there could be many other improvements as well.

Only major difference is that I really wanted to use designated initializers, and that's only available for C++20 and above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants