Skip to content

Commit

Permalink
fix(parser): Rely on default_missing_value for flag actions
Browse files Browse the repository at this point in the history
In the short term, this just provides a back door to custom actions.
Longer term, we can explore a `SetConst` action that relies on this
behavior.  Really, `SetTrue` and `SetFalse` are shortcuts for such an
action but shortcuts can be helpful for usability.

Apparently, this also reduced `.text` size by 1k
  • Loading branch information
epage committed Jul 28, 2022
1 parent f76a867 commit 8e20782
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Removed `PartialEq` and `Eq` from `Command`
- `number_of_values(0)` no longer implies `takes_value(true).multiple_values(true)`
- `number_of_values(1)` no longer implies `multiple_values(true)`
- `ArgAction::SetTrue` and `ArgAction::SetFalse` now prioritize `Arg::default_missing_value` over their standard behavior
- *(help)* Make `DeriveDisplayOrder` the default and removed the setting. To sort help, set `next_display_order(None)` (#2808)
- *(help)* Subcommand display order respects `Command::next_display_order` instead of `DeriveDisplayOrder` and using its own initial display order value (#2808)
- *(env)* Parse `--help` and `--version` like any `ArgAction::SetTrue` flag (#3776)
Expand Down
12 changes: 12 additions & 0 deletions src/builder/action.rs
Expand Up @@ -260,6 +260,18 @@ impl ArgAction {
}
}

pub(crate) fn default_missing_value(&self) -> Option<&'static std::ffi::OsStr> {
match self {
Self::Set => None,
Self::Append => None,
Self::SetTrue => Some(std::ffi::OsStr::new("true")),
Self::SetFalse => Some(std::ffi::OsStr::new("false")),
Self::Count => None,
Self::Help => None,
Self::Version => None,
}
}

pub(crate) fn default_value_parser(&self) -> Option<super::ValueParser> {
match self {
Self::Set => None,
Expand Down
5 changes: 5 additions & 0 deletions src/builder/arg.rs
Expand Up @@ -4303,6 +4303,11 @@ impl<'help> Arg<'help> {
self.default_vals = vec![default_value];
}
}
if let Some(default_value) = action.default_missing_value() {
if self.default_missing_vals.is_empty() {
self.default_missing_vals = vec![default_value];
}
}
if action.takes_values() {
self.settings.set(ArgSettings::TakesValue);
} else {
Expand Down

0 comments on commit 8e20782

Please sign in to comment.