mirror of
https://github.com/clap-rs/clap.git
synced 2026-01-25 08:16:14 +00:00
wip: refactoring App into Cmd
This commit is contained in:
1847
src/build/app.rs
1847
src/build/app.rs
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
53
src/build/built_cmd.rs
Normal file
53
src/build/built_cmd.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
pub struct BuiltCmd<'help> {
|
||||
// Used to identify the Cmd and all it's aliases in an efficient manner
|
||||
#[doc(hidden)]
|
||||
pub ids: Vec<ArgId>,
|
||||
// Settings that change how the args are parsed, or App behaves
|
||||
#[doc(hidden)]
|
||||
pub settings: CmdFlags,
|
||||
// Global settings (i.e. all subcommands)
|
||||
#[doc(hidden)]
|
||||
pub g_settings: CmdFlags,
|
||||
// The list of valid arguments
|
||||
#[doc(hidden)]
|
||||
pub args: ArgsVec<'help>,
|
||||
// A list of valid subcommands
|
||||
#[doc(hidden)]
|
||||
pub subcommands: Vec<ArgId>,
|
||||
}
|
||||
|
||||
impl<'help> BuiltCmd<'help> {
|
||||
pub(crate) fn parse<T: ArgV>(&mut self, it: &mut Peekable<T>) -> ClapResult<ParseResult> {
|
||||
debugln!("Cmd::parse;");
|
||||
let mut matcher = {
|
||||
let mut parser = Parser::new();
|
||||
|
||||
// do the real parsing
|
||||
parser.parse(it, self)?
|
||||
};
|
||||
|
||||
let global_arg_vec: Vec<u64> = self.global_args().map(|ga| ga.id).collect();
|
||||
matcher.propagate_globals(&global_arg_vec);
|
||||
|
||||
Ok(matcher.into())
|
||||
}
|
||||
}
|
||||
|
||||
// Facade for ArgsVec
|
||||
#[doc(hidden)]
|
||||
impl<'help> BuiltCmd<'help> {
|
||||
pub fn args(&self) -> Args<'help> { self.args.args() }
|
||||
pub fn args_mut(&mut self) -> ArgsMut<'help> { self.args.args_mut() }
|
||||
pub fn flags(&self) -> Flags<'help> { self.args.flags() }
|
||||
pub fn flags_mut(&mut self) -> FlagsMut<'help> { self.args.flags_mut() }
|
||||
pub fn options(&self) -> Options<'help> { self.args.args() }
|
||||
pub fn options_mut(&mut self) -> OptionsMut<'help> { self.args.args() }
|
||||
pub fn positionals(&self) -> Positionals<'help> { self.args.positionals() }
|
||||
pub fn positionals_mut(&mut self) -> PositionalsMut<'help> { self.args.positionals_mut() }
|
||||
pub fn global_args(&self) -> impl Iterator<Item=&Arg<'help>> {
|
||||
self.args().filter(|x| x.is_set(ArgSettings::Global))
|
||||
}
|
||||
pub fn global_args_mut(&mut self) -> impl Iterator<Item=&mut Arg<'help>> {
|
||||
self.args_mut().filter(|x| x.is_set(ArgSettings::Global))
|
||||
}
|
||||
}
|
||||
1670
src/build/cmd.rs
Normal file
1670
src/build/cmd.rs
Normal file
File diff suppressed because it is too large
Load Diff
1058
src/build/cmd/settings.rs
Normal file
1058
src/build/cmd/settings.rs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -804,22 +804,24 @@ macro_rules! clap_app {
|
||||
}
|
||||
|
||||
macro_rules! impl_settings {
|
||||
($n:ident, $($v:ident => $c:path),+) => {
|
||||
pub fn set(&mut self, s: $n) {
|
||||
match s {
|
||||
$($n::$v => self.0.insert($c)),+
|
||||
($FLAGS:ident, $SETTING:ident, $($v:ident => $c:path),+) => {
|
||||
impl $FLAGS {
|
||||
pub fn set(&mut self, s: $SETTING) {
|
||||
match s {
|
||||
$($SETTING::$v => self.insert($c)),+
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unset(&mut self, s: $n) {
|
||||
match s {
|
||||
$($n::$v => self.0.remove($c)),+
|
||||
pub fn unset(&mut self, s: $SETTING) {
|
||||
match s {
|
||||
$($SETTING::$v => self.remove($c)),+
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_set(&self, s: $n) -> bool {
|
||||
match s {
|
||||
$($n::$v => self.0.contains($c)),+
|
||||
pub fn is_set(&self, s: $SETTING) -> bool {
|
||||
match s {
|
||||
$($SETTING::$v => self.contains($c)),+
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
21
src/parse/argv.rs
Normal file
21
src/parse/argv.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use std::env;
|
||||
|
||||
pub trait ArgV : Iterator<Item=T> where T: AsRef<OsStr> { }
|
||||
|
||||
impl<T> ArgV for Vec<T> where T: AsRef<OsStr> { }
|
||||
impl<T> ArgV for env::Args { }
|
||||
impl<T> ArgV for env::ArgsOs { }
|
||||
impl<'a, T> ArgV for &'a [T] where T: AsRef<OsStr> { }
|
||||
|
||||
macro_rules! impl_argv_arr {
|
||||
($($n:expr)+) => {
|
||||
$(impl<T> ArgV for [T; $n] where T: AsRef<OsStr> { })+
|
||||
}
|
||||
}
|
||||
|
||||
impl_argv_arr! {
|
||||
1 2 3 4 5 6 7 8 9 10
|
||||
11 12 13 14 15 16 17 18 19 20
|
||||
21 22 23 24 25 26 27 28 29 30
|
||||
31 32
|
||||
}
|
||||
0
src/parse/result.rs
Normal file
0
src/parse/result.rs
Normal file
@@ -1,37 +1,39 @@
|
||||
# Highlights
|
||||
|
||||
* `App` vs `Cmd` (No more `SubCommand`)
|
||||
* `AppSettings` vs `CmdSettings`
|
||||
* Lazy propagation
|
||||
* Lazy requirement validation
|
||||
* `App::write_help` takes `&mut self` now
|
||||
* `App::override_usage` No longer implies `\t` which allows multi lined usages
|
||||
* `Cmd::write_help` takes `&mut self` now
|
||||
* `Cmd::override_usage` No longer implies `\t` which allows multi lined usages
|
||||
* In usage parser, for options `[name]... --option [val]` results in `ArgSettings::MultipleOccurrences` but `--option [val]...` results in `ArgSettings::MultipleValues` *and* `ArgSettings::MultipleOccurrences`. Before both resulted in the same thing
|
||||
* Allow empty values no longer default
|
||||
* UseValueDelimiter no longer the default
|
||||
* Multiple delima fixed (vals vs occurrences)
|
||||
* Ability to mutate args once they've been added to an `App`
|
||||
* `App::args` and `App::arg` are more generic
|
||||
* Ability to mutate args once they've been added to an `Cmd`
|
||||
* `Cmd::args` and `Cmd::arg` are more generic
|
||||
* Can unset global settings
|
||||
* Instead of adding arg with long `--help` or `--version` you can use `App::mut_arg` to override things
|
||||
* Instead of adding arg with long `--help` or `--version` you can use `Cmd::mut_arg` to override things
|
||||
* Caution, must fully override
|
||||
* No longer forces auto-handle of help/ver however if still desired `AppSettings::NoAuto{Help,Version}`
|
||||
* No longer forces auto-handle of help/ver however if still desired `CmdSettings::NoAuto{Help,Version}`
|
||||
|
||||
# How to Upgrade
|
||||
|
||||
### If you use `Arg::multiple(true)`
|
||||
|
||||
|
||||
# Deprecations
|
||||
|
||||
## Simple Renames
|
||||
|
||||
### App
|
||||
|
||||
- `App::get_matches_safe` -> `App::try_get_matches`
|
||||
- `App::get_matches_from_safe` -> `App::try_get_matches_from`
|
||||
- `App::get_matches_safe_borrow` -> `App::try_get_matches_from_mut`
|
||||
- `App::usage` -> `App::override_usage`
|
||||
- `App::help` -> `App::override_help`
|
||||
- `App::template` -> `App::help_template`
|
||||
- `App::get_matches` -> `App::parse`
|
||||
- `App::get_matches_safe` -> `App::try_parse`
|
||||
- `App::get_matches_from_safe` -> `App::try_parse_from`
|
||||
- `App::get_matches_safe_borrow` -> `App::try_parse_from_mut`
|
||||
- `App::usage` -> `Cmd::override_usage`
|
||||
- `App::help` -> `Cmd::override_help`
|
||||
- `App::template` -> `Cmd::help_template`
|
||||
|
||||
### Arg
|
||||
|
||||
@@ -39,8 +41,8 @@
|
||||
- `Arg::set` -> `Arg::setting`
|
||||
- `Arg::from_yaml` -> `Arg::from`
|
||||
- `Arg::with_name` -> `Arg::new`
|
||||
- `Arg::group` -> Use App::group
|
||||
- `Arg::groups` -> Use App::group
|
||||
- `Arg::group` -> Use Cmd::group
|
||||
- `Arg::groups` -> Use Cmd::group
|
||||
|
||||
### ArgGroup
|
||||
|
||||
@@ -50,18 +52,21 @@
|
||||
|
||||
### App
|
||||
|
||||
- `App::version_message` -> `App::mut_arg`
|
||||
- `App::version_short` -> `App::mut_arg`
|
||||
- `App::help_message` -> `App::mut_arg`
|
||||
- `App::help_short` -> `App::mut_arg`
|
||||
- `App::args_from_usage` -> `App::args(&str)`
|
||||
- `App::arg_from_usage` -> `App::arg(&str)`
|
||||
- `App::write_help` -> `&self` -> `&mut self` (#808)
|
||||
- `App::version_message` -> `Cmd::mut_arg`
|
||||
- `App::version_short` -> `Cmd::mut_arg`
|
||||
- `App::help_message` -> `Cmd::mut_arg`
|
||||
- `App::help_short` -> `Cmd::mut_arg`
|
||||
- `App::args_from_usage` -> `Cmd::args(&str)`
|
||||
- `App::arg_from_usage` -> `Cmd::arg(&str)`
|
||||
- `Cmd::write_help` -> `&self` -> `&mut self` (#808)
|
||||
- `App::gen_completions` -> `clap_completions::generate`
|
||||
- `App::gen_completions_to` -> `clap_completions::generate_to`
|
||||
- `App::settings` -> `App::setting(Setting1 | Setting2)`
|
||||
- `App::unset_settings` -> `App::unset_setting(Setting1 | Setting2)`
|
||||
- `App::global_settings` -> `App::global_setting(Setting1 | Setting2)`
|
||||
|
||||
#### Not Done Yet
|
||||
|
||||
- `App::settings` -> `Cmd::setting(Setting1 | Setting2)`
|
||||
- `App::unset_settings` -> `Cmd::unset_setting(Setting1 | Setting2)`
|
||||
- `App::global_settings` -> `Cmd::global_setting(Setting1 | Setting2)`
|
||||
|
||||
### Arg
|
||||
|
||||
@@ -69,10 +74,10 @@
|
||||
|
||||
# Additional APIs
|
||||
|
||||
## App
|
||||
## Cmd (former App)
|
||||
|
||||
* `App::mut_arg`
|
||||
* `App::unset_global_setting`
|
||||
* `Cmd::mut_arg`
|
||||
* `Cmd::unset_global_setting`
|
||||
|
||||
## Arg
|
||||
|
||||
|
||||
Reference in New Issue
Block a user