Skip to content

Commit a62e452

Browse files
committed
fix(AppSettings): fixes bug where subcmds didn't receive parent ver
Subcommands now receive the parent version when the setting AppSettings::GlobalVersion has been set.
1 parent 990ddfb commit a62e452

File tree

2 files changed

+67
-56
lines changed

2 files changed

+67
-56
lines changed

src/app/mod.rs

+32-28
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ use errors::Result as ClapResult;
5050
/// // Your program logic starts here...
5151
/// ```
5252
#[allow(missing_debug_implementations)]
53-
pub struct App<'a, 'b>(Parser<'a, 'b>) where 'a: 'b;
53+
pub struct App<'a, 'b> where 'a: 'b {
54+
#[doc(hidden)]
55+
pub p: Parser<'a, 'b>
56+
}
57+
5458

5559
impl<'a, 'b> App<'a, 'b> {
5660
/// Creates a new instance of an application requiring a name. The name may be, but doesn't
@@ -64,7 +68,7 @@ impl<'a, 'b> App<'a, 'b> {
6468
/// let prog = App::new("My Program")
6569
/// # ;
6670
/// ```
67-
pub fn new<S: Into<String>>(n: S) -> Self { App(Parser::with_name(n.into())) }
71+
pub fn new<S: Into<String>>(n: S) -> Self { App { p: Parser::with_name(n.into()) } }
6872

6973
/// Creates a new instace of `App` from a .yml (YAML) file. A full example of supported YAML
7074
/// objects can be found in `examples/17_yaml.rs` and `examples/17_yaml.yml`. One great use for
@@ -114,7 +118,7 @@ impl<'a, 'b> App<'a, 'b> {
114118
/// # ;
115119
/// ```
116120
pub fn author<S: Into<&'b str>>(mut self, author: S) -> Self {
117-
self.0.meta.author = Some(author.into());
121+
self.p.meta.author = Some(author.into());
118122
self
119123
}
120124

@@ -136,7 +140,7 @@ impl<'a, 'b> App<'a, 'b> {
136140
/// # ;
137141
/// ```
138142
pub fn bin_name<S: Into<String>>(mut self, name: S) -> Self {
139-
self.0.meta.bin_name = Some(name.into());
143+
self.p.meta.bin_name = Some(name.into());
140144
self
141145
}
142146

@@ -152,7 +156,7 @@ impl<'a, 'b> App<'a, 'b> {
152156
/// # ;
153157
/// ```
154158
pub fn about<S: Into<&'b str>>(mut self, about: S) -> Self {
155-
self.0.meta.about = Some(about.into());
159+
self.p.meta.about = Some(about.into());
156160
self
157161
}
158162

@@ -169,7 +173,7 @@ impl<'a, 'b> App<'a, 'b> {
169173
/// # ;
170174
/// ```
171175
pub fn after_help<S: Into<&'b str>>(mut self, help: S) -> Self {
172-
self.0.meta.more_help = Some(help.into());
176+
self.p.meta.more_help = Some(help.into());
173177
self
174178
}
175179

@@ -189,7 +193,7 @@ impl<'a, 'b> App<'a, 'b> {
189193
/// # ;
190194
/// ```
191195
pub fn version<S: Into<&'b str>>(mut self, ver: S) -> Self {
192-
self.0.meta.version = Some(ver.into());
196+
self.p.meta.version = Some(ver.into());
193197
self
194198
}
195199

@@ -217,7 +221,7 @@ impl<'a, 'b> App<'a, 'b> {
217221
/// # ;
218222
/// ```
219223
pub fn usage<S: Into<&'b str>>(mut self, usage: S) -> Self {
220-
self.0.meta.usage_str = Some(usage.into());
224+
self.p.meta.usage_str = Some(usage.into());
221225
self
222226
}
223227

@@ -255,7 +259,7 @@ impl<'a, 'b> App<'a, 'b> {
255259
/// # ;
256260
/// ```
257261
pub fn help<S: Into<&'b str>>(mut self, help: S) -> Self {
258-
self.0.meta.help_str = Some(help.into());
262+
self.p.meta.help_str = Some(help.into());
259263
self
260264
}
261265

@@ -277,7 +281,7 @@ impl<'a, 'b> App<'a, 'b> {
277281
/// # ;
278282
/// ```
279283
pub fn help_short<S: AsRef<str> + 'b>(mut self, s: S) -> Self {
280-
self.0.help_short(s.as_ref());
284+
self.p.help_short(s.as_ref());
281285
self
282286
}
283287

@@ -299,7 +303,7 @@ impl<'a, 'b> App<'a, 'b> {
299303
/// # ;
300304
/// ```
301305
pub fn version_short<S: AsRef<str>>(mut self, s: S) -> Self {
302-
self.0.version_short(s.as_ref());
306+
self.p.version_short(s.as_ref());
303307
self
304308
}
305309

@@ -317,7 +321,7 @@ impl<'a, 'b> App<'a, 'b> {
317321
/// # ;
318322
/// ```
319323
pub fn setting(mut self, setting: AppSettings) -> Self {
320-
self.0.set(setting);
324+
self.p.set(setting);
321325
self
322326
}
323327

@@ -336,7 +340,7 @@ impl<'a, 'b> App<'a, 'b> {
336340
/// ```
337341
pub fn settings(mut self, settings: &[AppSettings]) -> Self {
338342
for s in settings {
339-
self.0.set(*s);
343+
self.p.set(*s);
340344
}
341345
self
342346
}
@@ -362,7 +366,7 @@ impl<'a, 'b> App<'a, 'b> {
362366
/// # ;
363367
/// ```
364368
pub fn arg<A: Borrow<Arg<'a, 'b>> + 'a>(mut self, a: A) -> Self {
365-
self.0.add_arg(a.borrow());
369+
self.p.add_arg(a.borrow());
366370
self
367371
}
368372

@@ -381,7 +385,7 @@ impl<'a, 'b> App<'a, 'b> {
381385
/// ```
382386
pub fn args(mut self, args: &[Arg<'a, 'b>]) -> Self {
383387
for arg in args {
384-
self.0.add_arg(arg);
388+
self.p.add_arg(arg);
385389
}
386390
self
387391
}
@@ -401,7 +405,7 @@ impl<'a, 'b> App<'a, 'b> {
401405
/// # ;
402406
/// ```
403407
pub fn arg_from_usage(mut self, usage: &'a str) -> Self {
404-
self.0.add_arg(&Arg::from_usage(usage));
408+
self.p.add_arg(&Arg::from_usage(usage));
405409
self
406410
}
407411

@@ -426,7 +430,7 @@ impl<'a, 'b> App<'a, 'b> {
426430
pub fn args_from_usage(mut self, usage: &'a str) -> Self {
427431
for l in usage.lines() {
428432
if l.len() == 0 { continue; }
429-
self.0.add_arg(&Arg::from_usage(l.trim()));
433+
self.p.add_arg(&Arg::from_usage(l.trim()));
430434
}
431435
self
432436
}
@@ -464,7 +468,7 @@ impl<'a, 'b> App<'a, 'b> {
464468
/// # ;
465469
/// ```
466470
pub fn group(mut self, group: ArgGroup<'a>) -> Self {
467-
self.0.add_group(group);
471+
self.p.add_group(group);
468472
self
469473
}
470474

@@ -514,7 +518,7 @@ impl<'a, 'b> App<'a, 'b> {
514518
/// # ;
515519
/// ```
516520
pub fn subcommand(mut self, subcmd: App<'a, 'b>) -> Self {
517-
self.0.add_subcommand(subcmd);
521+
self.p.add_subcommand(subcmd);
518522
self
519523
}
520524

@@ -536,7 +540,7 @@ impl<'a, 'b> App<'a, 'b> {
536540
where I: IntoIterator<Item = App<'a, 'b>>
537541
{
538542
for subcmd in subcmds.into_iter() {
539-
self.0.add_subcommand(subcmd);
543+
self.p.add_subcommand(subcmd);
540544
}
541545
self
542546
}
@@ -569,7 +573,7 @@ impl<'a, 'b> App<'a, 'b> {
569573
/// app.write_help(&mut out).ok().expect("failed to write to stdout");
570574
/// ```
571575
pub fn write_help<W: Write>(&self, w: &mut W) -> ClapResult<()> {
572-
self.0.write_help(w)
576+
self.p.write_help(w)
573577
}
574578

575579
/// Starts the parsing process, upon a failed parse an error will be displayed to the user and
@@ -690,10 +694,10 @@ impl<'a, 'b> App<'a, 'b> {
690694
T: Into<OsString>
691695
{
692696
// Verify all positional assertions pass
693-
self.0.verify_positionals();
697+
self.p.verify_positionals();
694698
// If there are global arguments, we need to propgate them down to subcommands
695699
// before parsing incase we run into a subcommand
696-
self.0.propogate_globals();
700+
self.p.propogate_globals();
697701

698702
let mut matcher = ArgMatcher::new();
699703

@@ -705,22 +709,22 @@ impl<'a, 'b> App<'a, 'b> {
705709
// will have two arguments, './target/release/my_prog', '-a' but we don't want
706710
// to display
707711
// the full path when displaying help messages and such
708-
if !self.0.is_set(AppSettings::NoBinaryName) {
712+
if !self.p.is_set(AppSettings::NoBinaryName) {
709713
if let Some(name) = it.next() {
710714
let bn_os = name.into();
711715
let p = Path::new(&*bn_os);
712716
if let Some(f) = p.file_name() {
713717
if let Some(s) = f.to_os_string().to_str() {
714-
if let None = self.0.meta.bin_name {
715-
self.0.meta.bin_name = Some(s.to_owned());
718+
if let None = self.p.meta.bin_name {
719+
self.p.meta.bin_name = Some(s.to_owned());
716720
}
717721
}
718722
}
719723
}
720724
}
721725

722726
// do the real parsing
723-
if let Err(e) = self.0.get_matches_with(&mut matcher, &mut it) {
727+
if let Err(e) = self.p.get_matches_with(&mut matcher, &mut it) {
724728
return Err(e);
725729
}
726730

@@ -732,7 +736,7 @@ impl<'a, 'b> App<'a, 'b> {
732736
fn maybe_wait_for_exit(&self, e: Error) -> ! {
733737
if e.use_stderr() {
734738
wlnerr!("{}", e.message);
735-
if self.0.is_set(AppSettings::WaitOnError) {
739+
if self.p.is_set(AppSettings::WaitOnError) {
736740
wlnerr!("\nPress [ENTER] / [RETURN] to continue...");
737741
let mut s = String::new();
738742
let i = io::stdin();

0 commit comments

Comments
 (0)