• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // https://github.com/TeXitoi/structopt/issues/{NUMBER}
2 
3 use crate::utils;
4 
5 use clap::{ArgGroup, Args, Parser, Subcommand};
6 
7 #[test]
issue_151_groups_within_subcommands()8 fn issue_151_groups_within_subcommands() {
9     #[derive(Args, Debug)]
10     #[command(group = ArgGroup::new("verb").required(true).multiple(true))]
11     struct Opt {
12         #[arg(long, group = "verb")]
13         foo: Option<String>,
14         #[arg(long, group = "verb")]
15         bar: Option<String>,
16     }
17 
18     #[derive(Debug, Parser)]
19     struct Cli {
20         #[command(flatten)]
21         a: Opt,
22     }
23 
24     assert!(Cli::try_parse_from(["test"]).is_err());
25     assert!(Cli::try_parse_from(["test", "--foo=v1"]).is_ok());
26     assert!(Cli::try_parse_from(["test", "--bar=v2"]).is_ok());
27     assert!(Cli::try_parse_from(["test", "--zebra=v3"]).is_err());
28     assert!(Cli::try_parse_from(["test", "--foo=v1", "--bar=v2"]).is_ok());
29 }
30 
31 #[test]
issue_289()32 fn issue_289() {
33     #[derive(Parser)]
34     #[command(infer_subcommands = true)]
35     enum Args {
36         SomeCommand {
37             #[command(subcommand)]
38             sub: SubSubCommand,
39         },
40         AnotherCommand,
41     }
42 
43     #[derive(Subcommand)]
44     #[command(infer_subcommands = true)]
45     enum SubSubCommand {
46         TestCommand,
47     }
48 
49     assert!(Args::try_parse_from(["test", "some-command", "test-command"]).is_ok());
50     assert!(Args::try_parse_from(["test", "some", "test-command"]).is_ok());
51     assert!(Args::try_parse_from(["test", "some-command", "test"]).is_ok());
52     assert!(Args::try_parse_from(["test", "some", "test"]).is_ok());
53 }
54 
55 #[test]
issue_324()56 fn issue_324() {
57     fn my_version() -> &'static str {
58         "MY_VERSION"
59     }
60 
61     #[derive(Parser)]
62     #[command(version = my_version())]
63     #[command(help_template = utils::FULL_TEMPLATE)]
64     struct Opt {
65         #[command(subcommand)]
66         _cmd: SubCommand,
67     }
68 
69     #[derive(Subcommand)]
70     enum SubCommand {
71         Start,
72     }
73 
74     let help = utils::get_long_help::<Opt>();
75     assert!(help.contains("MY_VERSION"));
76 }
77 
78 #[test]
issue_418()79 fn issue_418() {
80     #[derive(Debug, Parser)]
81     struct Opts {
82         #[command(subcommand)]
83         /// The command to run
84         command: Command,
85     }
86 
87     #[derive(Debug, Subcommand)]
88     enum Command {
89         /// Reticulate the splines
90         #[command(visible_alias = "ret")]
91         Reticulate {
92             /// How many splines
93             num_splines: u8,
94         },
95         /// Frobnicate the rest
96         #[command(visible_alias = "frob")]
97         Frobnicate,
98     }
99 
100     let help = utils::get_long_help::<Opts>();
101     assert!(help.contains("Reticulate the splines [aliases: ret]"));
102 }
103 
104 #[test]
issue_490()105 fn issue_490() {
106     use clap::Parser;
107     use std::iter::FromIterator;
108     use std::str::FromStr;
109 
110     struct U16ish;
111     impl FromStr for U16ish {
112         type Err = ();
113         fn from_str(_: &str) -> Result<Self, Self::Err> {
114             unimplemented!()
115         }
116     }
117     impl<'a> FromIterator<&'a U16ish> for Vec<u16> {
118         fn from_iter<T: IntoIterator<Item = &'a U16ish>>(_: T) -> Self {
119             unimplemented!()
120         }
121     }
122 
123     #[derive(Parser, Debug)]
124     struct Opt {
125         opt_vec: Vec<u16>,
126         #[arg(long)]
127         opt_opt_vec: Option<Vec<u16>>,
128     }
129 
130     // Assert that it compiles
131 }
132