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-04-12 09:27:57 +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-04-12 09:27:57 +00:00
|
|
|
|
2018-07-02 19:06:46 +00:00
|
|
|
use clap::Clap;
|
2018-04-12 09:27:57 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flatten() {
|
2018-07-02 19:06:46 +00:00
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
2018-04-12 09:27:57 +00:00
|
|
|
struct Common {
|
|
|
|
arg: i32,
|
|
|
|
}
|
|
|
|
|
2018-07-02 19:06:46 +00:00
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
2018-04-12 09:27:57 +00:00
|
|
|
struct Opt {
|
2018-07-02 19:06:46 +00:00
|
|
|
#[clap(flatten)]
|
2018-04-12 09:27:57 +00:00
|
|
|
common: Common,
|
|
|
|
}
|
2018-05-21 14:54:22 +00:00
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
common: Common { arg: 42 }
|
|
|
|
},
|
2018-07-02 19:06:46 +00:00
|
|
|
Opt::parse_from(&["test", "42"])
|
2018-05-21 14:54:22 +00:00
|
|
|
);
|
2018-07-13 00:32:28 +00:00
|
|
|
assert!(Opt::try_parse_from(&["test"]).is_err());
|
|
|
|
assert!(Opt::try_parse_from(&["test", "42", "24"]).is_err());
|
2018-04-12 09:27:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 16:19:05 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2018-04-12 09:27:57 +00:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn flatten_twice() {
|
2018-07-02 19:06:46 +00:00
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
2018-04-12 09:27:57 +00:00
|
|
|
struct Common {
|
|
|
|
arg: i32,
|
|
|
|
}
|
|
|
|
|
2018-07-02 19:06:46 +00:00
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
2018-04-12 09:27:57 +00:00
|
|
|
struct Opt {
|
2018-07-02 19:06:46 +00:00
|
|
|
#[clap(flatten)]
|
2018-04-12 09:27:57 +00:00
|
|
|
c1: Common,
|
|
|
|
// Defines "arg" twice, so this should not work.
|
2018-07-02 19:06:46 +00:00
|
|
|
#[clap(flatten)]
|
2018-04-12 09:27:57 +00:00
|
|
|
c2: Common,
|
|
|
|
}
|
2018-07-02 19:06:46 +00:00
|
|
|
Opt::parse_from(&["test", "42", "43"]);
|
2018-04-12 09:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flatten_in_subcommand() {
|
2018-07-02 19:06:46 +00:00
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
2018-04-12 09:27:57 +00:00
|
|
|
struct Common {
|
|
|
|
arg: i32,
|
|
|
|
}
|
|
|
|
|
2018-07-02 19:06:46 +00:00
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
2018-04-12 09:27:57 +00:00
|
|
|
struct Add {
|
2020-01-07 10:17:23 +00:00
|
|
|
#[clap(short)]
|
2018-04-12 09:27:57 +00:00
|
|
|
interactive: bool,
|
2018-07-02 19:06:46 +00:00
|
|
|
#[clap(flatten)]
|
2018-04-12 09:27:57 +00:00
|
|
|
common: Common,
|
|
|
|
}
|
|
|
|
|
2018-07-02 19:06:46 +00:00
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
2018-04-12 09:27:57 +00:00
|
|
|
enum Opt {
|
|
|
|
Fetch {
|
2020-01-07 10:17:23 +00:00
|
|
|
#[clap(short)]
|
2018-04-12 09:27:57 +00:00
|
|
|
all: bool,
|
2018-07-02 19:06:46 +00:00
|
|
|
#[clap(flatten)]
|
2018-04-12 09:27:57 +00:00
|
|
|
common: Common,
|
|
|
|
},
|
|
|
|
|
|
|
|
Add(Add),
|
|
|
|
}
|
|
|
|
|
2018-05-21 14:54:22 +00:00
|
|
|
assert_eq!(
|
|
|
|
Opt::Fetch {
|
|
|
|
all: false,
|
|
|
|
common: Common { arg: 42 }
|
|
|
|
},
|
2018-07-02 19:06:46 +00:00
|
|
|
Opt::parse_from(&["test", "fetch", "42"])
|
2018-05-21 14:54:22 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt::Add(Add {
|
|
|
|
interactive: true,
|
|
|
|
common: Common { arg: 43 }
|
|
|
|
}),
|
2018-07-02 19:06:46 +00:00
|
|
|
Opt::parse_from(&["test", "add", "-i", "43"])
|
2018-05-21 14:54:22 +00:00
|
|
|
);
|
2018-04-12 09:27:57 +00:00
|
|
|
}
|
2020-01-15 15:34:47 +00:00
|
|
|
|
2020-07-07 11:17:36 +00:00
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
|
|
enum BaseCli {
|
|
|
|
Command1(Command1),
|
|
|
|
}
|
2020-01-15 15:34:47 +00:00
|
|
|
|
2020-07-07 11:17:36 +00:00
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
|
|
struct Command1 {
|
|
|
|
arg1: i32,
|
|
|
|
}
|
2020-01-15 15:34:47 +00:00
|
|
|
|
2020-07-07 11:17:36 +00:00
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
|
|
struct Command2 {
|
|
|
|
arg2: i32,
|
|
|
|
}
|
2020-01-15 15:34:47 +00:00
|
|
|
|
2020-07-07 11:17:36 +00:00
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
|
|
enum Opt {
|
|
|
|
#[clap(flatten)]
|
|
|
|
BaseCli(BaseCli),
|
|
|
|
Command2(Command2),
|
|
|
|
}
|
2020-01-15 15:34:47 +00:00
|
|
|
|
2020-07-07 11:17:36 +00:00
|
|
|
#[test]
|
|
|
|
fn merge_subcommands_with_flatten() {
|
2020-01-15 15:34:47 +00:00
|
|
|
assert_eq!(
|
|
|
|
Opt::BaseCli(BaseCli::Command1(Command1 { arg1: 42 })),
|
|
|
|
Opt::parse_from(&["test", "command1", "42"])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt::Command2(Command2 { arg2: 43 }),
|
|
|
|
Opt::parse_from(&["test", "command2", "43"])
|
|
|
|
);
|
|
|
|
}
|
2020-08-25 07:53:54 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flatten_with_doc_comment() {
|
|
|
|
#[derive(Clap, Debug)]
|
|
|
|
struct DaemonOpts {
|
|
|
|
#[clap(short)]
|
|
|
|
user: String,
|
|
|
|
#[clap(short)]
|
|
|
|
group: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clap, Debug)]
|
|
|
|
#[clap(name = "basic")]
|
|
|
|
struct Opt {
|
|
|
|
/// A very important doc comment I just can't leave out!
|
|
|
|
#[clap(flatten)]
|
|
|
|
opts: DaemonOpts,
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 11:17:36 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn update_subcommands_with_flatten() {
|
|
|
|
let mut opt = Opt::BaseCli(BaseCli::Command1(Command1 { arg1: 12 }));
|
|
|
|
opt.update_from(&["test", "command1", "42"]);
|
|
|
|
assert_eq!(Opt::parse_from(&["test", "command1", "42"]), opt);
|
|
|
|
opt.update_from(&["test", "command2", "43"]);
|
|
|
|
assert_eq!(Opt::parse_from(&["test", "command2", "43"]), opt);
|
|
|
|
}
|