This repository has been archived by the owner on Jan 1, 2022. It is now read-only.
Easy to accidentally use setting
when global_setting
is intended
#240
Labels
Issue by epage
Monday Nov 15, 2021 at 18:08 GMT
Originally opened as clap-rs/clap#3028
Please complete the following tasks
Rust Version
rustc 1.55.0 (c8dfcfe04 2021-09-06)
Clap Version
2.33
Minimal reproducible code
When supporting a user on TeXitoi/structopt#129, I accidentally gave the following non-functional code, confused as to why it wasn't working:
when I should have done
Steps to reproduce the bug with the above code
Run it:
cargo run -- sum-one -a 1 2 -3
Actual Behaviour
Fails with unknown
-3
argExpected Behaviour
Prints
0
Additional Context
I think every place I use an
AppSettings
in my programs, I've done this wrong.The problem with this is that clap mixes subcommand logic (e.g. SubcommandRequiredElseHelp) with command logic (e.g. DisabledColoredHelp). Using the command logic without
global_setting
is likely a bug but conversely, using subcommand logic withglobal_setting
is likely a bug. I have not looked through all of the settings to see if there are any middle ground cases.EDIT: There is a third category, command display logic, like
help_heading
. This should not be treated withglobal
.At minimum, all examples should be updated to use
global_setting
for command-related settings, even if subcommands aren't used. People will take these examples and apply them to their subcommands or could later extend their command to have subcommands, so we should treat this as the de facto approach.However, this still requires a lot of communication to the user so they know which to use when. Alternatively
Option
and provide accessor functions thatunwrap_or(...)
so we can track whether it is explicitly set while being ergonomic. I've found this pattern fairly successful when dealing with layered config (example 1, example 2) and args (example).This approach provides the behavior users expect while giving them the control to do something we didn't predict.
Debug Output
No response
The text was updated successfully, but these errors were encountered: