|
| 1 | +use uutils_args::{Arguments, Options, Value}; |
| 2 | + |
| 3 | +// Using a fully-fledged compile-error testsuite is a bit overkill, but we still |
| 4 | +// want to make sure that the `derive` crate generates reasonable error messages. |
| 5 | +// That's what this "example" is for. In the following, there are blocks of |
| 6 | +// lines, one marked as POSITIVE and multiple lines marked as NEGATIVE. The |
| 7 | +// committed version of this file should only contain POSITIVE. In order to run a |
| 8 | +// test, comment out the POSITIVE line, and use a NEGATIVE line instead, and |
| 9 | +// manually check whether you see a reasonable error message – ideally the error |
| 10 | +// message indicated by the comment. One way to do this is: |
| 11 | + |
| 12 | +// $ cargo build --example test_compile_errors_manually |
| 13 | + |
| 14 | +#[derive(Value, Debug, Default)] |
| 15 | +enum Flavor { |
| 16 | + #[default] |
| 17 | + #[value("kind", "nice")] |
| 18 | + Kind, |
| 19 | + #[value("condescending")] // POSITIVE |
| 20 | + // #[value(condescending)] // NEGATIVE: "expected comma-separated list of string literals" |
| 21 | + Condescending, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Arguments)] |
| 25 | +#[arguments(file = "examples/hello_world_help.md")] // POSITIVE |
| 26 | +// #[arguments(file = "examples/nonexistent.md")] // NEGATIVE: "cannot open help-string file" |
| 27 | +// #[arguments(file = "/dev/full")] // NEGATIVE: Causes OOM, FIXME |
| 28 | +// #[arguments(file = "/")] // NEGATIVE: "cannot read from help-string file" |
| 29 | +// #[arguments(file = "path/to/some/WRITE-ONLY/file")] // NEGATIVE: "cannot open help-string file" |
| 30 | +enum Arg { |
| 31 | + /// The name to greet |
| 32 | + #[arg("-n NAME", "--name[=NAME]", "name=NAME")] // POSITIVE |
| 33 | + // #[arg("-")] // NEGATIVE: flag name must be non-empty (cannot be just '-') |
| 34 | + // #[arg("-n NAME", "--name[NAME]", "name=NAME")] // NEGATIVE: "expected '=' after '[' in flag pattern" |
| 35 | + // #[arg("-n NAME", "--name[=NAME", "name=NAME")] // NEGATIVE: "expected final ']' in flag pattern" |
| 36 | + // #[arg(key="name")] // NEGATIVE: "can't parse arg attributes, expected one or more strings" |
| 37 | + Name(String), |
| 38 | + |
| 39 | + /// The number of times to greet |
| 40 | + #[arg("-c N", "--count=N")] |
| 41 | + Count(u8), |
| 42 | + |
| 43 | + #[arg("--flavor=FLAVOR")] |
| 44 | + Flavor(Flavor), |
| 45 | +} |
| 46 | + |
| 47 | +struct Settings { |
| 48 | + name: String, |
| 49 | + count: u8, |
| 50 | + flavor: Flavor, |
| 51 | +} |
| 52 | + |
| 53 | +impl Options<Arg> for Settings { |
| 54 | + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { |
| 55 | + match arg { |
| 56 | + Arg::Name(n) => self.name = n, |
| 57 | + Arg::Count(c) => self.count = c, |
| 58 | + Arg::Flavor(flavor) => self.flavor = flavor, |
| 59 | + } |
| 60 | + Ok(()) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +fn main() -> Result<(), uutils_args::Error> { |
| 65 | + let (settings, _operands) = Settings { |
| 66 | + name: String::new(), |
| 67 | + count: 1, |
| 68 | + flavor: Flavor::Kind, |
| 69 | + } |
| 70 | + .parse(std::env::args_os()) |
| 71 | + .unwrap(); |
| 72 | + |
| 73 | + for _ in 0..settings.count { |
| 74 | + match settings.flavor { |
| 75 | + Flavor::Kind => { println!("Hello, {}!", settings.name); } |
| 76 | + Flavor::Condescending => { println!("Ugh, {}.", settings.name); } |
| 77 | + } |
| 78 | + } |
| 79 | + Ok(()) |
| 80 | +} |
0 commit comments