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

Repeated group help when sharing common args in an adjacent enum #369

Open
ozwaldorf opened this issue Jul 2, 2024 · 3 comments
Open

Repeated group help when sharing common args in an adjacent enum #369

ozwaldorf opened this issue Jul 2, 2024 · 3 comments

Comments

@ozwaldorf
Copy link
Contributor

Group help is not deduplicated when using an enum variants with adjacent args that share a common struct.

Example

use bpaf::Bpaf;

/// Example common args
#[derive(Bpaf, Clone, Debug)]
struct Common {
    /// Enable foo
    #[bpaf(short, long)]
    foo: bool,
    /// Enable bar
    #[bpaf(short, long)]
    bar: bool,
}

#[derive(Bpaf, Clone, Debug)]
#[bpaf(options)]
enum Args {
    #[bpaf(adjacent)]
    VariantOne {
        /// Use the first group
        #[bpaf(short('o'), long("one"), req_flag(()))]
        _one: (),
        #[bpaf(external)]
        common: Common,
        /// Enable baz
        #[bpaf(short('B'), long)]
        baz: bool,
    },
    #[bpaf(adjacent)]
    VariantTwo {
        /// Use the second group
        #[bpaf(short('t'), long("two"), req_flag(()))]
        _two: (),
        #[bpaf(external)]
        common: Common,
        /// Enable bau
        #[bpaf(short('B'), long)]
        bau: bool,
    },
}

fn main() {
    let _ = args().run();
}

Output

Usage: example (-o [-f] [-b] [-B] | -t [-f] [-b] [-B])

Example common args
    -f, --foo   Enable foo
    -b, --bar   Enable bar

Example common args
    -f, --foo   Enable foo
    -b, --bar   Enable bar

Available options:
  -o [-f] [-b] [-B]
    -o, --one   Use the first group
    -B, --baz   Enable baz

  -t [-f] [-b] [-B]
    -t, --two   Use the second group
    -B, --bau   Enable bau

    -h, --help  Prints help information
@pacak
Copy link
Owner

pacak commented Jul 2, 2024

Yea, I'm still thinking about a better way to represent it. common args should probably be inlined inside adjacent groups, but this messes up the formatting. One way would be just to hide one of the groups...

        #[bpaf(external, hide)]
        common: Common,

Or rearrange the structure into something like (-o [-B] | -t [-B]) [-f] [b]

I think eventually I'll give more control to render things like that.

@ozwaldorf
Copy link
Contributor Author

ozwaldorf commented Jul 2, 2024

I think eventually I'll give more control to render things like that.

This would be amazing!


Unfortunately my usecase actually has 2 common structs, one of them that's used inside every variant, and another that's used in only 2 out of 4 variants, so rearranging like that isn't really possible as it would be unclear which ones don't use those args (and I would lose type safety around arg conflicts). The other option of hiding works but it might again be unclear that that adjacent group contains those arguments, which is crucial to my usecase since some variants don't use the shared args

I ended up not using group help at all, and just relied on the adjacent group summaries to show possible fields. I also ended up needing the default variant to work without a req_flag, which contains both structs. I made this variant non-adjacent to allow it to parse as the default without any of the specific flags needed, and it inlines the common args into the top level help, but still deduplicates the other adjacent helps, which is a nice compromise.

image

@pacak
Copy link
Owner

pacak commented Jul 2, 2024 via email

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

No branches or pull requests

2 participants