clap/tests/nested-subcommands.rs

215 lines
4.8 KiB
Rust
Raw Normal View History

2018-07-02 17:41:01 +00:00
// Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>,
// Kevin Knapp (@kbknapp) <kbknapp@gmail.com>, and
// Andrew Hobden (@hoverbear) <andrew@hoverbear.org>
//
2018-02-25 10:22:24 +00:00
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2018-07-02 17:41:01 +00:00
//
// This work was derived from Structopt (https://github.com/TeXitoi/structopt)
// commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
// MIT/Apache 2.0 license.
2018-05-21 14:54:22 +00:00
#[macro_use]
2018-07-02 19:06:46 +00:00
extern crate clap;
2018-07-02 19:06:46 +00:00
use clap::Clap;
2018-07-02 19:06:46 +00:00
#[derive(Clap, PartialEq, Debug)]
struct Opt {
2018-07-02 19:06:46 +00:00
#[clap(short = "f", long = "force")]
force: bool,
2018-07-02 19:06:46 +00:00
#[clap(short = "v", long = "verbose", parse(from_occurrences))]
verbose: u64,
2018-07-02 19:06:46 +00:00
#[clap(subcommand)]
2018-05-21 14:54:22 +00:00
cmd: Sub,
}
2018-07-02 19:06:46 +00:00
#[derive(Clap, PartialEq, Debug)]
enum Sub {
2018-07-02 19:06:46 +00:00
#[clap(name = "fetch")]
Fetch {},
2018-07-02 19:06:46 +00:00
#[clap(name = "add")]
2018-05-21 14:54:22 +00:00
Add {},
}
2018-07-02 19:06:46 +00:00
#[derive(Clap, PartialEq, Debug)]
struct Opt2 {
2018-07-02 19:06:46 +00:00
#[clap(short = "f", long = "force")]
force: bool,
2018-07-02 19:06:46 +00:00
#[clap(short = "v", long = "verbose", parse(from_occurrences))]
verbose: u64,
2018-07-02 19:06:46 +00:00
#[clap(subcommand)]
2018-05-21 14:54:22 +00:00
cmd: Option<Sub>,
}
#[test]
fn test_no_cmd() {
let result = Opt::try_parse_from(&["test"]);
assert!(result.is_err());
2018-05-21 14:54:22 +00:00
assert_eq!(
Opt2 {
force: false,
verbose: 0,
cmd: None
},
Opt2::parse_from(&["test"])
2018-05-21 14:54:22 +00:00
);
}
#[test]
fn test_fetch() {
2018-05-21 14:54:22 +00:00
assert_eq!(
Opt {
force: false,
verbose: 3,
cmd: Sub::Fetch {}
},
Opt::parse_from(&["test", "-vvv", "fetch"])
2018-05-21 14:54:22 +00:00
);
assert_eq!(
Opt {
force: true,
verbose: 0,
cmd: Sub::Fetch {}
},
Opt::parse_from(&["test", "--force", "fetch"])
2018-05-21 14:54:22 +00:00
);
}
#[test]
fn test_add() {
2018-05-21 14:54:22 +00:00
assert_eq!(
Opt {
force: false,
verbose: 0,
cmd: Sub::Add {}
},
Opt::parse_from(&["test", "add"])
2018-05-21 14:54:22 +00:00
);
assert_eq!(
Opt {
force: false,
verbose: 2,
cmd: Sub::Add {}
},
Opt::parse_from(&["test", "-vv", "add"])
2018-05-21 14:54:22 +00:00
);
}
#[test]
fn test_badinput() {
let result = Opt::try_parse_from(&["test", "badcmd"]);
assert!(result.is_err());
let result = Opt::try_parse_from(&["test", "add", "--verbose"]);
assert!(result.is_err());
let result = Opt::try_parse_from(&["test", "--badopt", "add"]);
assert!(result.is_err());
let result = Opt::try_parse_from(&["test", "add", "--badopt"]);
assert!(result.is_err());
}
2018-07-02 19:06:46 +00:00
#[derive(Clap, PartialEq, Debug)]
struct Opt3 {
2018-07-02 19:06:46 +00:00
#[clap(short = "a", long = "all")]
all: bool,
2018-07-02 19:06:46 +00:00
#[clap(subcommand)]
2018-05-21 14:54:22 +00:00
cmd: Sub2,
}
2018-07-02 19:06:46 +00:00
#[derive(Clap, PartialEq, Debug)]
enum Sub2 {
2018-07-02 19:06:46 +00:00
#[clap(name = "foo")]
Foo {
file: String,
2018-07-02 19:06:46 +00:00
#[clap(subcommand)]
2018-05-21 14:54:22 +00:00
cmd: Sub3,
},
2018-07-02 19:06:46 +00:00
#[clap(name = "bar")]
2018-05-21 14:54:22 +00:00
Bar {},
}
2018-07-02 19:06:46 +00:00
#[derive(Clap, PartialEq, Debug)]
enum Sub3 {
2018-07-02 19:06:46 +00:00
#[clap(name = "baz")]
Baz {},
2018-07-02 19:06:46 +00:00
#[clap(name = "quux")]
2018-05-21 14:54:22 +00:00
Quux {},
}
#[test]
fn test_subsubcommand() {
assert_eq!(
Opt3 {
all: true,
2018-05-21 14:54:22 +00:00
cmd: Sub2::Foo {
file: "lib.rs".to_string(),
cmd: Sub3::Quux {}
}
},
Opt3::parse_from(&["test", "--all", "foo", "lib.rs", "quux"])
);
}
2018-07-02 19:06:46 +00:00
#[derive(Clap, PartialEq, Debug)]
enum SubSubCmdWithOption {
2018-07-02 19:06:46 +00:00
#[clap(name = "remote")]
Remote {
2018-07-02 19:06:46 +00:00
#[clap(subcommand)]
2018-05-21 14:54:22 +00:00
cmd: Option<Remote>,
},
2018-07-02 19:06:46 +00:00
#[clap(name = "stash")]
Stash {
2018-07-02 19:06:46 +00:00
#[clap(subcommand)]
2018-05-21 14:54:22 +00:00
cmd: Stash,
},
}
2018-07-02 19:06:46 +00:00
#[derive(Clap, PartialEq, Debug)]
enum Remote {
2018-07-02 19:06:46 +00:00
#[clap(name = "add")]
Add { name: String, url: String },
2018-07-02 19:06:46 +00:00
#[clap(name = "remove")]
Remove { name: String },
}
2018-07-02 19:06:46 +00:00
#[derive(Clap, PartialEq, Debug)]
enum Stash {
2018-07-02 19:06:46 +00:00
#[clap(name = "save")]
Save,
2018-07-02 19:06:46 +00:00
#[clap(name = "pop")]
Pop,
}
#[test]
fn sub_sub_cmd_with_option() {
fn make(args: &[&str]) -> Option<SubSubCmdWithOption> {
use clap::{FromArgMatches, IntoApp};
2018-07-02 19:06:46 +00:00
SubSubCmdWithOption::into_app()
2018-05-21 14:54:22 +00:00
.get_matches_from_safe(args)
.ok()
.map(|m| SubSubCmdWithOption::from_argmatches(&m))
}
assert_eq!(
2018-05-21 14:54:22 +00:00
Some(SubSubCmdWithOption::Remote { cmd: None }),
make(&["", "remote"])
);
assert_eq!(
Some(SubSubCmdWithOption::Remote {
cmd: Some(Remote::Add {
name: "origin".into(),
url: "http".into()
})
}),
make(&["", "remote", "add", "origin", "http"])
);
2018-05-21 14:54:22 +00:00
assert_eq!(
Some(SubSubCmdWithOption::Stash { cmd: Stash::Save }),
make(&["", "stash", "save"])
);
assert_eq!(None, make(&["", "stash"]));
}