-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Display order of subcommands' pre-existing args is not merged/updated #4920
Comments
I'm wondering what would be the right way to solve this. We are setting the display order when calling A workaround for this would be calling |
To me the interspersed A/D/B/E/C/F order looks wrong, and I can't imagine users actually wanting that behavior. It is a logical consequence of the implementation, but not a feature to desire. Adding all of subcommand's args (in their subcommand's order) after args existing in the parent seems sensible to me. It'd be analogous to adding these args directly to the parent without subcommand's indirection. I think the fix should be, when adding a subcommand to a parent:
This would work well for a typical usage with This leaves |
Tweak build help to clarify role of --bin From [user feedback](https://internals.rust-lang.org/t/pre-issue-feature-request-give-me-the-option-to-not-build-a-target/18852/5) the `--bin`'s description "Build only the specified binary" could be understood as "Don't build lib" rather than "Build this bin (with lib) and not other bins". I don't know if a better wording explaining subtelty of lib+bin crates would fit in the small space of CLI help. However, reordering the args to show `--bin` after `--bins` rather than after `--lib` gives it a more accurate context. So I've merely put the `--bin` (and test/bench/example) after their plural counterpart. I've also noticed an issue with clap [inserting global args between subcommand's args](clap-rs/clap#4920), and `--locked` definitely doesn't belong right between the two bin args. I've added a workaround for that issue. The workaround is otherwise harmless, and shouldn't cause problems if clap decides to update sort order of subcommands. Changes this: ```text --lib Build only this package's library --bin [<NAME>] Build only the specified binary --locked Require Cargo.lock is up to date --bins Build all binaries --offline Run without accessing the network --config <KEY=VALUE> Override a configuration value --example [<NAME>] Build only the specified example --examples Build all examples -Z <FLAG> Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details --test [<NAME>] Build only the specified test target --tests Build all tests --bench [<NAME>] Build only the specified bench target --benches Build all benches ``` to this: ```text --lib Build only this package's library --bins Build all binaries --bin [<NAME>] Build only the specified binary --examples Build all examples --example [<NAME>] Build only the specified example --tests Build all tests --test [<NAME>] Build only the specified test target --benches Build all benches --bench [<NAME>] Build only the specified bench target […] --locked Require Cargo.lock is up to date --offline Run without accessing the network --config <KEY=VALUE> Override a configuration value -Z <FLAG> Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details ```
To be clear, I never said it does look right. I also disagree that this is a consequence of implementation but is instead a matter of trade offs.
FYI I would consider this a breaking change so this would not be able to go in until clap v5, if we go this route. The reason is what I had hinted at before as to why I was not considering this route: a user can be setting the display order, explicitly with We could try to track intent (what was done explicitly) but that is adding a lot of extra complexity to try to out-guess the user.
Globals are passed down while building the |
Please complete the following tasks
Rust Version
rustc 1.69.0 (84c898d65 2023-04-16)
Clap Version
4.2.1
Minimal reproducible code
Steps to reproduce the bug with the above code
cargo run -- sub --help
Actual Behaviour
Expected Behaviour
Additional Context
Cargo is affected by this.
cargo build -h
displays:Note that
--locked
inserted between--bin
and--bins
. Cargo defines lib, bin, bins, example, examples one after another. But global args happen to be numbered the same and get interspersed with unrelated commands.I believe it's because each command has its own
disp_order
numbering starting from zero, andsubcommand_internal
resets the number for future args, but doesn't update order of existing args to give them their unique number range. This means that global args from the parent numbered from zero are sorted together with subcommands' args numbered from zero.Debug Output
No response
The text was updated successfully, but these errors were encountered: