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

Help text: flattening Args containing both positional arguments and named flags results in incorrect "usage" order #5625

Open
2 tasks done
jrose-signal opened this issue Aug 5, 2024 · 1 comment
Labels
A-help Area: documentation, including docs.rs, readme, examples, etc... C-bug Category: Updating dependencies E-medium Call for participation: Experience needed to fix: Medium / intermediate

Comments

@jrose-signal
Copy link

Please complete the following tasks

Rust Version

1.80.0

Clap Version

4.5.11

Minimal reproducible code

use clap::*; // 4.5.11

#[derive(Parser)]
struct TopLevel {
    first: String,
    #[command(flatten)]
    second: Inner,
}

// *** Note: the user can specify *either* a positional argument *or* a named argument
#[derive(Args)]
#[group(required = true, multiple = false)]
struct Inner {
    value: Option<String>,
    #[arg(long)]
    path: Option<String>,
}

fn main() {
    let mut cmd = TopLevel::command();
    _ = cmd.print_long_help();

    let parsed = TopLevel::parse_from(["test", "a", "b"]);
    assert_eq!(parsed.first, "a");
    assert_eq!(parsed.second.value, "b");
}

Steps to reproduce the bug with the above code

Run on play.rust-lang.org

Actual Behaviour

Usage: playground <VALUE|--path <PATH>> <FIRST>

even though the assertions show that the FIRST argument is in fact getting parsed first, as desired.

Expected Behaviour

Usage: playground <FIRST> <VALUE|--path <PATH>>

It does work to put the flag argument after the first positional argument, so this isn't even wrong, even if it's a little unconventional.

Additional Context

I tried manually specifying index on the arguments, but it did not affect the help output.

Debug Output

No response

@jrose-signal jrose-signal added the C-bug Category: Updating dependencies label Aug 5, 2024
@epage epage added A-help Area: documentation, including docs.rs, readme, examples, etc... E-medium Call for participation: Experience needed to fix: Medium / intermediate labels Aug 5, 2024
@epage
Copy link
Member

epage commented Aug 5, 2024

The usage code generally assumes the mutually exclusive arguments are flags. It'll be a bit messy to try to integrate positional support.

In the mean time, override_usage can be used to manually specify something

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-help Area: documentation, including docs.rs, readme, examples, etc... C-bug Category: Updating dependencies E-medium Call for participation: Experience needed to fix: Medium / intermediate
Projects
None yet
Development

No branches or pull requests

2 participants