diff options
author | Haibo Huang <hhb@google.com> | 2020-09-23 21:23:42 -0700 |
---|---|---|
committer | Haibo Huang <hhb@google.com> | 2020-09-23 21:23:42 -0700 |
commit | e7bfadf396e6d7aca36ef17424eae852404775d6 (patch) | |
tree | 254d00e0918f220a2662ca8f65ed510bd2220eba /examples | |
parent | 67fbbcc40fbcbcb1d18c74f7e1edcffb33d79b58 (diff) | |
download | structopt-e7bfadf396e6d7aca36ef17424eae852404775d6.tar.gz |
Upgrade rust/crates/structopt to 0.3.18
Test: make
Change-Id: Ib5d515b0f5e9ac7fdd0f2f8e2f00ecf54dbf63ca
Diffstat (limited to 'examples')
-rw-r--r-- | examples/README.md | 4 | ||||
-rw-r--r-- | examples/rename_all.rs | 4 | ||||
-rw-r--r-- | examples/required_if.rs | 43 |
3 files changed, 51 insertions, 0 deletions
diff --git a/examples/README.md b/examples/README.md index f0db20b..b485393 100644 --- a/examples/README.md +++ b/examples/README.md @@ -69,6 +69,10 @@ How to completely remove version. How `#[structopt(rename_all)]` works. +### [Required If](required_if.rs) + +How to use `#[structopt(required_if)]`. + ### [Skip](skip.rs) How to use `#[structopt(skip)]`. diff --git a/examples/rename_all.rs b/examples/rename_all.rs index 35f3c4f..c7c3538 100644 --- a/examples/rename_all.rs +++ b/examples/rename_all.rs @@ -17,6 +17,10 @@ //! - **Snake Case**: Keep all letters lowercase and indicate word boundaries //! with underscores. //! - **Verbatim**: Use the original attribute name defined in the code. +//! +//! - **Lower Case**: Keep all letters lowercase and remove word boundaries. +//! +//! - **Upper Case**: Keep all letters upperrcase and remove word boundaries. use structopt::StructOpt; diff --git a/examples/required_if.rs b/examples/required_if.rs new file mode 100644 index 0000000..cb6b414 --- /dev/null +++ b/examples/required_if.rs @@ -0,0 +1,43 @@ +//! How to use `required_if` with structopt. +use structopt::StructOpt; + +#[derive(Debug, StructOpt, PartialEq)] +struct Opt { + /// Where to write the output: to `stdout` or `file` + #[structopt(short)] + out_type: String, + + /// File name: only required when `out-type` is set to `file` + #[structopt(name = "FILE", required_if("out-type", "file"))] + file_name: Option<String>, +} + +fn main() { + let opt = Opt::from_args(); + println!("{:?}", opt); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_opt_out_type_file_without_file_name_returns_err() { + let opt = Opt::from_iter_safe(&["test", "-o", "file"]); + let err = opt.unwrap_err(); + assert_eq!(err.kind, clap::ErrorKind::MissingRequiredArgument); + } + + #[test] + fn test_opt_out_type_file_with_file_name_returns_ok() { + let opt = Opt::from_iter_safe(&["test", "-o", "file", "filename"]); + let opt = opt.unwrap(); + assert_eq!( + opt, + Opt { + out_type: "file".into(), + file_name: Some("filename".into()), + } + ); + } +} |