Skip to content

Commit

Permalink
fix: Deprecate Command::trailing_var_arg
Browse files Browse the repository at this point in the history
Now that we have it on `Arg`, we don't need it on `Command`
  • Loading branch information
epage committed Sep 7, 2022
1 parent b07d02e commit bffce7f
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Deprecated
- `Arg::number_of_values` in favor of `Arg::num_args`
- `default_value_os`, `default_values_os`, `default_value_if_os`, and `default_value_ifs_os` as the non `_os` variants now accept either a `str` or an `OsStr`
- `Command::dont_collapse_args_in_usage` is now the default and is deprecated
- `Command::trailing_var_arg` in favor of `Arg::trailing_var_arg`
- *(derive)* `structopt` and `clap` attributes in favor of the more specific `command`, `arg`, and `value`

### Features
Expand Down
8 changes: 6 additions & 2 deletions clap_bench/benches/06_rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,13 @@ fn build_cli() -> Command {
Command::new("run")
.about("Run a command with an environment configured for a given toolchain")
.after_help(RUN_HELP)
.trailing_var_arg(true)
.arg(Arg::new("toolchain").required(true))
.arg(Arg::new("command").required(true).num_args(1..)),
.arg(
Arg::new("command")
.required(true)
.num_args(1..)
.trailing_var_arg(true),
),
)
.subcommand(
Command::new("which")
Expand Down
9 changes: 3 additions & 6 deletions clap_complete/examples/completion-derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ use std::io;
use std::path::PathBuf;

#[derive(Parser, Debug, PartialEq)]
#[command(
name = "value_hints_derive",
// Command::trailing_var_ar is required to use ValueHint::CommandWithArguments
trailing_var_arg = true,
)]
#[command(name = "value_hints_derive")]
struct Opt {
/// If provided, outputs the completion file for given shell
#[arg(long = "generate", value_enum)]
Expand All @@ -45,7 +41,8 @@ struct Opt {
cmd_name: Option<OsString>,
#[arg(short, long, value_hint = ValueHint::CommandString)]
cmd: Option<String>,
#[arg(value_hint = ValueHint::CommandWithArguments)]
// Command::trailing_var_ar is required to use ValueHint::CommandWithArguments
#[arg(trailing_var_arg = true, value_hint = ValueHint::CommandWithArguments)]
command_with_args: Vec<String>,
#[arg(short, long, value_hint = ValueHint::Username)]
user: Option<String>,
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/examples/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ use std::io;

fn build_cli() -> Command {
Command::new("value_hints")
// AppSettings::TrailingVarArg is required to use ValueHint::CommandWithArguments
.trailing_var_arg(true)
.arg(
Arg::new("generator")
.long("generate")
Expand Down Expand Up @@ -69,6 +67,8 @@ fn build_cli() -> Command {
.arg(
Arg::new("command_with_args")
.num_args(1..)
// AppSettings::TrailingVarArg is required to use ValueHint::CommandWithArguments
.trailing_var_arg(true)
.value_hint(ValueHint::CommandWithArguments),
)
.arg(
Expand Down
2 changes: 1 addition & 1 deletion clap_complete/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ pub fn sub_subcommands_command(name: &'static str) -> clap::Command {

pub fn value_hint_command(name: &'static str) -> clap::Command {
clap::Command::new(name)
.trailing_var_arg(true)
.arg(
clap::Arg::new("choice")
.long("choice")
Expand Down Expand Up @@ -225,6 +224,7 @@ pub fn value_hint_command(name: &'static str) -> clap::Command {
clap::Arg::new("command_with_args")
.action(clap::ArgAction::Set)
.num_args(1..)
.trailing_var_arg(true)
.value_hint(clap::ValueHint::CommandWithArguments),
)
.arg(
Expand Down
2 changes: 1 addition & 1 deletion clap_complete_fig/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ pub fn sub_subcommands_command(name: &'static str) -> clap::Command {

pub fn value_hint_command(name: &'static str) -> clap::Command {
clap::Command::new(name)
.trailing_var_arg(true)
.arg(
clap::Arg::new("choice")
.long("choice")
Expand Down Expand Up @@ -221,6 +220,7 @@ pub fn value_hint_command(name: &'static str) -> clap::Command {
clap::Arg::new("command_with_args")
.action(clap::ArgAction::Set)
.num_args(1..)
.trailing_var_arg(true)
.value_hint(clap::ValueHint::CommandWithArguments),
)
.arg(
Expand Down
3 changes: 1 addition & 2 deletions clap_mangen/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ pub fn sub_subcommands_command(name: &'static str) -> clap::Command {

pub fn value_hint_command(name: &'static str) -> clap::Command {
clap::Command::new(name)
.trailing_var_arg(true)
.arg(
clap::Arg::new("choice")
.long("choice")
Expand Down Expand Up @@ -219,6 +218,7 @@ pub fn value_hint_command(name: &'static str) -> clap::Command {
clap::Arg::new("command_with_args")
.action(clap::ArgAction::Set)
.num_args(1..)
.trailing_var_arg(true)
.value_hint(clap::ValueHint::CommandWithArguments),
)
.arg(
Expand Down Expand Up @@ -283,7 +283,6 @@ pub fn assert_matches_path(expected_path: impl AsRef<std::path::Path>, cmd: clap

pub fn possible_values_command(name: &'static str) -> clap::Command {
clap::Command::new(name)
.trailing_var_arg(true)
.arg(
clap::Arg::new("choice")
.long("choice")
Expand Down
33 changes: 10 additions & 23 deletions src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2000,28 +2000,11 @@ impl Command {
}
}

/// Specifies that the final positional argument is a "VarArg" and that `clap` should not
/// attempt to parse any further args, as if the user had used a `--`.
///
/// The values of the trailing positional argument will contain all args from itself on.
///
/// **NOTE:** [`Arg::value_delimiter`] still applies if set.
///
/// **NOTE:** The final positional argument **must** have [`Arg::num_args(..)`].
///
/// # Examples
///
/// ```rust
/// # use clap::{Command, arg};
/// let m = Command::new("myprog")
/// .trailing_var_arg(true)
/// .arg(arg!(<cmd> ... "commands to run"))
/// .get_matches_from(vec!["myprog", "arg1", "-r", "val1"]);
///
/// let trail: Vec<_> = m.get_many::<String>("cmd").unwrap().collect();
/// assert_eq!(trail, ["arg1", "-r", "val1"]);
/// ```
/// [`Arg::num_args(..)`]: crate::Arg::num_args()
#[doc(hidden)]
#[cfg_attr(
feature = "deprecated",
deprecated(since = "4.0.0", note = "Replaced with `Arg::trailing_var_arg`")
)]
pub fn trailing_var_arg(self, yes: bool) -> Self {
if yes {
self.setting(AppSettings::TrailingVarArg)
Expand Down Expand Up @@ -3600,7 +3583,11 @@ impl Command {
self.is_set(AppSettings::AllowNegativeNumbers)
}

/// Report whether [`Command::trailing_var_arg`] is set
#[doc(hidden)]
#[cfg_attr(
feature = "deprecated",
deprecated(since = "4.0.0", note = "Replaced with `Arg::is_trailing_var_arg_set`")
)]
pub fn is_trailing_var_arg_set(&self) -> bool {
self.is_set(AppSettings::TrailingVarArg)
}
Expand Down
16 changes: 8 additions & 8 deletions tests/builder/app_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,8 @@ fn stop_delim_values_only_pos_follows() {
#[test]
fn dont_delim_values_trailingvararg() {
let m = Command::new("positional")
.trailing_var_arg(true)
.dont_delimit_trailing_values(true)
.arg(arg!([opt] ... "some pos"))
.arg(arg!([opt] ... "some pos").trailing_var_arg(true))
.try_get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"])
.unwrap();
assert!(m.contains_id("opt"));
Expand Down Expand Up @@ -372,8 +371,7 @@ fn delim_values_only_pos_follows() {
#[test]
fn delim_values_trailingvararg() {
let m = Command::new("positional")
.trailing_var_arg(true)
.arg(arg!([opt] ... "some pos"))
.arg(arg!([opt] ... "some pos").trailing_var_arg(true))
.try_get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"])
.unwrap();
assert!(m.contains_id("opt"));
Expand Down Expand Up @@ -410,8 +408,11 @@ fn delim_values_only_pos_follows_with_delim() {
#[test]
fn delim_values_trailingvararg_with_delim() {
let m = Command::new("positional")
.trailing_var_arg(true)
.arg(arg!([opt] ... "some pos").value_delimiter(','))
.arg(
arg!([opt] ... "some pos")
.value_delimiter(',')
.trailing_var_arg(true),
)
.try_get_matches_from(vec!["", "test", "--foo", "-Wl,-bar"])
.unwrap();
assert!(m.contains_id("opt"));
Expand Down Expand Up @@ -520,9 +521,8 @@ fn allow_negative_numbers_fail() {
#[test]
fn leading_double_hyphen_trailingvararg() {
let m = Command::new("positional")
.trailing_var_arg(true)
.allow_hyphen_values(true)
.arg(arg!([opt] ... "some pos"))
.arg(arg!([opt] ... "some pos").trailing_var_arg(true))
.try_get_matches_from(vec!["", "--foo", "-Wl", "bar"])
.unwrap();
assert!(m.contains_id("opt"));
Expand Down

0 comments on commit bffce7f

Please sign in to comment.