@@ -1924,8 +1924,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
1924
1924
matches
1925
1925
}
1926
1926
1927
- /// Starts the parsing process. Called on top level parent app **ONLY** then recursively calls
1928
- /// the real parsing function for all subcommands
1927
+ /// Starts the parsing process without consuming the `App` struct `self`. This is normally not
1928
+ /// the desired functionality, instead prefer `App::get_matches_from_safe` which *does*
1929
+ /// consume `self`.
1929
1930
///
1930
1931
/// **NOTE:** The first argument will be parsed as the binary name.
1931
1932
///
@@ -1936,21 +1937,20 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
1936
1937
/// **NOTE:** This method should only be used when is absolutely necessary to handle errors
1937
1938
/// manually.
1938
1939
///
1939
- ///
1940
1940
/// # Example
1941
1941
///
1942
1942
/// ```no_run
1943
1943
/// # use clap::{App, Arg};
1944
1944
/// let arg_vec = vec!["my_prog", "some", "args", "to", "parse"];
1945
1945
///
1946
- /// let matches = App::new("myprog")
1946
+ /// let mut app = App::new("myprog");
1947
1947
/// // Args and options go here...
1948
- /// .get_matches_from_safe (arg_vec)
1948
+ /// let matches = app.get_matches_from_safe_borrow (arg_vec)
1949
1949
/// .unwrap_or_else( |e| { panic!("An error occurs: {}", e) });
1950
1950
/// ```
1951
- pub fn get_matches_from_safe < I , T > ( mut self ,
1952
- itr : I )
1953
- -> Result < ArgMatches < ' ar , ' ar > , ClapError >
1951
+ pub fn get_matches_from_safe_borrow < I , T > ( & mut self ,
1952
+ itr : I )
1953
+ -> Result < ArgMatches < ' ar , ' ar > , ClapError >
1954
1954
where I : IntoIterator < Item = T > ,
1955
1955
T : AsRef < str >
1956
1956
{
@@ -1987,6 +1987,39 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
1987
1987
Ok ( matches)
1988
1988
}
1989
1989
1990
+ /// Starts the parsing process. Called on top level parent app **ONLY** then recursively calls
1991
+ /// the real parsing function for all subcommands
1992
+ ///
1993
+ /// **NOTE:** The first argument will be parsed as the binary name.
1994
+ ///
1995
+ /// **NOTE:** This method should only be used when absolutely necessary, such as needing to
1996
+ /// parse arguments from something other than `std::env::args()`. If you are unsure, use
1997
+ /// `App::get_matches_safe()`
1998
+ ///
1999
+ /// **NOTE:** This method should only be used when is absolutely necessary to handle errors
2000
+ /// manually.
2001
+ ///
2002
+ ///
2003
+ /// # Example
2004
+ ///
2005
+ /// ```no_run
2006
+ /// # use clap::{App, Arg};
2007
+ /// let arg_vec = vec!["my_prog", "some", "args", "to", "parse"];
2008
+ ///
2009
+ /// let matches = App::new("myprog")
2010
+ /// // Args and options go here...
2011
+ /// .get_matches_from_safe(arg_vec)
2012
+ /// .unwrap_or_else( |e| { panic!("An error occurs: {}", e) });
2013
+ /// ```
2014
+ pub fn get_matches_from_safe < I , T > ( mut self ,
2015
+ itr : I )
2016
+ -> Result < ArgMatches < ' ar , ' ar > , ClapError >
2017
+ where I : IntoIterator < Item = T > ,
2018
+ T : AsRef < str >
2019
+ {
2020
+ self . get_matches_from_safe_borrow ( itr)
2021
+ }
2022
+
1990
2023
fn verify_positionals ( & mut self ) {
1991
2024
// Because you must wait until all arguments have been supplied, this is the first chance
1992
2025
// to make assertions on positional argument indexes
@@ -2251,7 +2284,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
2251
2284
// may be trying to pass might match a subcommand name
2252
2285
if !pos_only {
2253
2286
if self . subcommands . contains_key ( arg_slice) {
2254
- if arg_slice == "help" {
2287
+ if arg_slice == "help" && self . needs_subcmd_help {
2255
2288
self . print_help ( ) ;
2256
2289
}
2257
2290
subcmd_name = Some ( arg_slice. to_owned ( ) ) ;
@@ -2343,7 +2376,6 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
2343
2376
vals. insert ( len, arg_slice. to_owned ( ) ) ;
2344
2377
}
2345
2378
}
2346
-
2347
2379
} else {
2348
2380
// Only increment the positional counter if it doesn't allow multiples
2349
2381
pos_counter += 1 ;
@@ -2407,7 +2439,6 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
2407
2439
2408
2440
parse_group_reqs ! ( self , p) ;
2409
2441
}
2410
-
2411
2442
} else {
2412
2443
return Err ( self . report_error ( format ! ( "The argument '{}' was found, but '{}' \
2413
2444
wasn't expecting any", Format :: Warning ( arg. as_ref( ) ) ,
0 commit comments