Skip to content

Commit

Permalink
Merge pull request #3992 from epage/action
Browse files Browse the repository at this point in the history
docs: Shift focus from takes_value to actions
  • Loading branch information
epage committed Jul 26, 2022
2 parents 55f2bb6 + 5f20fe1 commit ee06707
Show file tree
Hide file tree
Showing 62 changed files with 1,194 additions and 866 deletions.
10 changes: 2 additions & 8 deletions clap_bench/benches/03_complex.rs
Expand Up @@ -57,7 +57,6 @@ pub fn build_from_builder(c: &mut Criterion) {
.help("tests options")
.short('o')
.long("option")
.takes_value(true)
.multiple_values(true)
.action(ArgAction::Append),
)
Expand All @@ -82,7 +81,7 @@ pub fn build_from_builder(c: &mut Criterion) {
.help("tests long options with exclusions")
.conflicts_with("option")
.requires("positional2")
.takes_value(true)
.action(ArgAction::Set)
.long("long-option-2"),
)
.arg(
Expand All @@ -94,13 +93,12 @@ pub fn build_from_builder(c: &mut Criterion) {
Arg::new("option3")
.short('O')
.long("Option")
.takes_value(true)
.action(ArgAction::Set)
.help("tests options with specific value sets")
.value_parser(OPT3_VALS),
)
.arg(
Arg::new("positional3")
.takes_value(true)
.multiple_values(true)
.help("tests positionals with specific values")
.index(4)
Expand All @@ -115,7 +113,6 @@ pub fn build_from_builder(c: &mut Criterion) {
.arg(
Arg::new("multvalsmo")
.long("multvalsmo")
.takes_value(true)
.multiple_values(true)
.action(ArgAction::Append)
.help("Tests multiple values, not mult occs")
Expand All @@ -124,7 +121,6 @@ pub fn build_from_builder(c: &mut Criterion) {
.arg(
Arg::new("minvals")
.long("minvals2")
.takes_value(true)
.multiple_values(true)
.action(ArgAction::Append)
.help("Tests 2 min vals")
Expand All @@ -133,7 +129,6 @@ pub fn build_from_builder(c: &mut Criterion) {
.arg(
Arg::new("maxvals")
.long("maxvals3")
.takes_value(true)
.multiple_values(true)
.action(ArgAction::Append)
.help("Tests 3 max vals")
Expand All @@ -148,7 +143,6 @@ pub fn build_from_builder(c: &mut Criterion) {
Arg::new("scoption")
.short('o')
.long("option")
.takes_value(true)
.multiple_values(true)
.action(ArgAction::Append)
.help("tests options"),
Expand Down
6 changes: 2 additions & 4 deletions clap_bench/benches/04_new_help.rs
Expand Up @@ -47,7 +47,7 @@ fn app_example3<'c>() -> Command<'c> {
.args(&[
Arg::new("config")
.help("sets the config file to use")
.takes_value(true)
.action(ArgAction::Set)
.short('c')
.long("config"),
Arg::new("input")
Expand Down Expand Up @@ -118,7 +118,6 @@ fn app_example7<'c>() -> Command<'c> {
.arg(
Arg::new("input")
.help("the input file to use")
.takes_value(true)
.multiple_values(true)
.action(ArgAction::Append)
.required(true)
Expand All @@ -136,7 +135,6 @@ fn app_example8<'c>() -> Command<'c> {
.arg(
Arg::new("input")
.help("the input file to use")
.takes_value(true)
.multiple_values(true)
.action(ArgAction::Append)
.required(true)
Expand All @@ -152,7 +150,7 @@ fn app_example10<'c>() -> Command<'c> {
Arg::new("CONFIG")
.help("The config file to use (default is \"config.json\")")
.short('c')
.takes_value(true),
.action(ArgAction::Set),
)
}

Expand Down
15 changes: 3 additions & 12 deletions clap_bench/benches/05_ripgrep.rs
Expand Up @@ -323,13 +323,12 @@ where
"type-list",
"version",
]))
.arg(arg("path").takes_value(true).multiple_values(true))
.arg(arg("path").multiple_values(true))
.arg(
flag("regexp")
.short('e')
.allow_hyphen_values(true)
.action(ArgAction::Append)
.takes_value(true)
.value_name("pattern"),
)
.arg(
Expand All @@ -345,22 +344,16 @@ where
.arg(
flag("color")
.value_name("WHEN")
.takes_value(true)
.action(ArgAction::Set)
.hide_possible_values(true)
.value_parser(["never", "auto", "always", "ansi"]),
)
.arg(
flag("colors")
.value_name("SPEC")
.action(ArgAction::Append)
.takes_value(true),
)
.arg(flag("colors").value_name("SPEC").action(ArgAction::Append))
.arg(flag("fixed-strings").short('F'))
.arg(
flag("glob")
.short('g')
.action(ArgAction::Append)
.takes_value(true)
.value_name("GLOB"),
)
.arg(flag("ignore-case").short('i'))
Expand All @@ -371,14 +364,12 @@ where
flag("type")
.short('t')
.action(ArgAction::Append)
.takes_value(true)
.value_name("TYPE"),
)
.arg(
flag("type-not")
.short('T')
.action(ArgAction::Append)
.takes_value(true)
.value_name("TYPE"),
)
.arg(flag("unrestricted").short('u').action(ArgAction::Append))
Expand Down
71 changes: 51 additions & 20 deletions clap_bench/benches/06_rustup.rs
Expand Up @@ -2,7 +2,7 @@
//
// CLI used is from rustup 408ed84f0e50511ed44a405dd91365e5da588790

use clap::{Arg, ArgGroup, Command};
use clap::{Arg, ArgAction, ArgGroup, Command};
use criterion::{criterion_group, criterion_main, Criterion};

pub fn build_rustup(c: &mut Criterion) {
Expand Down Expand Up @@ -105,31 +105,51 @@ fn build_cli() -> Command<'static> {
.subcommand(
Command::new("list")
.about("List installed and available targets")
.arg(Arg::new("toolchain").long("toolchain").takes_value(true)),
.arg(
Arg::new("toolchain")
.long("toolchain")
.action(ArgAction::Set),
),
)
.subcommand(
Command::new("add")
.about("Add a target to a Rust toolchain")
.arg(Arg::new("target").required(true))
.arg(Arg::new("toolchain").long("toolchain").takes_value(true)),
.arg(
Arg::new("toolchain")
.long("toolchain")
.action(ArgAction::Set),
),
)
.subcommand(
Command::new("remove")
.about("Remove a target from a Rust toolchain")
.arg(Arg::new("target").required(true))
.arg(Arg::new("toolchain").long("toolchain").takes_value(true)),
.arg(
Arg::new("toolchain")
.long("toolchain")
.action(ArgAction::Set),
),
)
.subcommand(
Command::new("install")
.hide(true) // synonym for 'add'
.arg(Arg::new("target").required(true))
.arg(Arg::new("toolchain").long("toolchain").takes_value(true)),
.arg(
Arg::new("toolchain")
.long("toolchain")
.action(ArgAction::Set),
),
)
.subcommand(
Command::new("uninstall")
.hide(true) // synonym for 'remove'
.arg(Arg::new("target").required(true))
.arg(Arg::new("toolchain").long("toolchain").takes_value(true)),
.arg(
Arg::new("toolchain")
.long("toolchain")
.action(ArgAction::Set),
),
),
)
.subcommand(
Expand All @@ -138,21 +158,33 @@ fn build_cli() -> Command<'static> {
.subcommand(
Command::new("list")
.about("List installed and available components")
.arg(Arg::new("toolchain").long("toolchain").takes_value(true)),
.arg(
Arg::new("toolchain")
.long("toolchain")
.action(ArgAction::Set),
),
)
.subcommand(
Command::new("add")
.about("Add a component to a Rust toolchain")
.arg(Arg::new("component").required(true))
.arg(Arg::new("toolchain").long("toolchain").takes_value(true))
.arg(Arg::new("target").long("target").takes_value(true)),
.arg(
Arg::new("toolchain")
.long("toolchain")
.action(ArgAction::Set),
)
.arg(Arg::new("target").long("target").action(ArgAction::Set)),
)
.subcommand(
Command::new("remove")
.about("Remove a component from a Rust toolchain")
.arg(Arg::new("component").required(true))
.arg(Arg::new("toolchain").long("toolchain").takes_value(true))
.arg(Arg::new("target").long("target").takes_value(true)),
.arg(
Arg::new("toolchain")
.long("toolchain")
.action(ArgAction::Set),
)
.arg(Arg::new("target").long("target").action(ArgAction::Set)),
),
)
.subcommand(
Expand All @@ -172,7 +204,7 @@ fn build_cli() -> Command<'static> {
.arg(
Arg::new("path")
.long("path")
.takes_value(true)
.action(ArgAction::Set)
.help("Path to the directory"),
)
.arg(
Expand All @@ -190,7 +222,7 @@ fn build_cli() -> Command<'static> {
Command::new("remove")
.hide(true) // synonym for 'unset'
.about("Remove the override toolchain for a directory")
.arg(Arg::new("path").long("path").takes_value(true))
.arg(Arg::new("path").long("path").action(ArgAction::Set))
.arg(
Arg::new("nonexistent")
.long("nonexistent")
Expand All @@ -204,12 +236,7 @@ fn build_cli() -> Command<'static> {
.after_help(RUN_HELP)
.trailing_var_arg(true)
.arg(Arg::new("toolchain").required(true))
.arg(
Arg::new("command")
.required(true)
.takes_value(true)
.multiple_values(true),
),
.arg(Arg::new("command").required(true).multiple_values(true)),
)
.subcommand(
Command::new("which")
Expand All @@ -236,7 +263,11 @@ fn build_cli() -> Command<'static> {
Command::new("man")
.about("View the man page for a given command")
.arg(Arg::new("command").required(true))
.arg(Arg::new("toolchain").long("toolchain").takes_value(true)),
.arg(
Arg::new("toolchain")
.long("toolchain")
.action(ArgAction::Set),
),
)
.subcommand(
Command::new("self")
Expand Down
1 change: 0 additions & 1 deletion clap_complete/examples/completion.rs
Expand Up @@ -68,7 +68,6 @@ fn build_cli() -> Command<'static> {
)
.arg(
Arg::new("command_with_args")
.takes_value(true)
.multiple_values(true)
.value_hint(ValueHint::CommandWithArguments),
)
Expand Down
6 changes: 2 additions & 4 deletions clap_complete/src/generator/mod.rs
Expand Up @@ -83,9 +83,7 @@ pub trait Generator {
///
/// ```
/// // src/cli.rs
///
/// use clap::{Command, Arg};
///
/// # use clap::{Command, Arg, ArgAction};
/// pub fn build_cli() -> Command<'static> {
/// Command::new("compl")
/// .about("Tests completions")
Expand All @@ -95,7 +93,7 @@ pub trait Generator {
/// .about("tests things")
/// .arg(Arg::new("case")
/// .long("case")
/// .takes_value(true)
/// .action(ArgAction::Set)
/// .help("the case to test")))
/// }
/// ```
Expand Down
2 changes: 2 additions & 0 deletions clap_complete/src/generator/utils.rs
Expand Up @@ -141,6 +141,7 @@ pub fn possible_values<'help>(a: &Arg<'help>) -> Option<Vec<clap::PossibleValue<
mod tests {
use super::*;
use clap::Arg;
use clap::ArgAction;
use pretty_assertions::assert_eq;

fn common_app() -> Command<'static> {
Expand All @@ -152,6 +153,7 @@ mod tests {
.short_alias('c')
.visible_short_alias('p')
.long("file")
.action(ArgAction::SetTrue)
.visible_alias("path"),
),
)
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/src/lib.rs
Expand Up @@ -22,7 +22,7 @@
//! ## Example
//!
//! ```rust,no_run
//! use clap::{Command, Arg, ValueHint, value_parser};
//! use clap::{Command, Arg, ValueHint, value_parser, ArgAction};
//! use clap_complete::{generate, Generator, Shell};
//! use std::io;
//!
Expand All @@ -35,7 +35,7 @@
//! .arg(
//! Arg::new("generator")
//! .long("generate")
//! .takes_value(true)
//! .action(ArgAction::Set)
//! .value_parser(value_parser!(Shell)),
//! )
//! }
Expand Down

0 comments on commit ee06707

Please sign in to comment.