1 // Third Party
2 #[cfg(feature = "yaml")]
3 use yaml_rust::Yaml;
4 
5 // Internal
6 use App;
7 use ArgMatches;
8 
9 /// The abstract representation of a command line subcommand.
10 ///
11 /// This struct describes all the valid options of the subcommand for the program. Subcommands are
12 /// essentially "sub-[`App`]s" and contain all the same possibilities (such as their own
13 /// [arguments], subcommands, and settings).
14 ///
15 /// # Examples
16 ///
17 /// ```rust
18 /// # use clap::{App, Arg, SubCommand};
19 /// App::new("myprog")
20 ///     .subcommand(
21 ///         SubCommand::with_name("config")
22 ///             .about("Used for configuration")
23 ///             .arg(Arg::with_name("config_file")
24 ///                 .help("The configuration file to use")
25 ///                 .index(1)))
26 /// # ;
27 /// ```
28 /// [`App`]: ./struct.App.html
29 /// [arguments]: ./struct.Arg.html
30 #[derive(Debug, Clone)]
31 pub struct SubCommand<'a> {
32     #[doc(hidden)]
33     pub name: String,
34     #[doc(hidden)]
35     pub matches: ArgMatches<'a>,
36 }
37 
38 impl<'a> SubCommand<'a> {
39     /// Creates a new instance of a subcommand requiring a name. The name will be displayed
40     /// to the user when they print version or help and usage information.
41     ///
42     /// # Examples
43     ///
44     /// ```rust
45     /// # use clap::{App, Arg, SubCommand};
46     /// App::new("myprog")
47     ///     .subcommand(
48     ///         SubCommand::with_name("config"))
49     /// # ;
50     /// ```
with_name<'b>(name: &str) -> App<'a, 'b>51     pub fn with_name<'b>(name: &str) -> App<'a, 'b> {
52         App::new(name)
53     }
54 
55     /// Creates a new instance of a subcommand from a YAML (.yml) document
56     ///
57     /// # Examples
58     ///
59     /// ```ignore
60     /// # #[macro_use]
61     /// # extern crate clap;
62     /// # use clap::Subcommand;
63     /// # fn main() {
64     /// let sc_yaml = load_yaml!("test_subcommand.yml");
65     /// let sc = SubCommand::from_yaml(sc_yaml);
66     /// # }
67     /// ```
68     #[cfg(feature = "yaml")]
from_yaml(yaml: &Yaml) -> App69     pub fn from_yaml(yaml: &Yaml) -> App {
70         App::from_yaml(yaml)
71     }
72 }
73