Skip to content

Commit 477bc0b

Browse files
committed
Remove defer chaining feature
1 parent 5af3073 commit 477bc0b

File tree

3 files changed

+7
-105
lines changed

3 files changed

+7
-105
lines changed

clap_builder/src/builder/command.rs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,6 @@ impl DeferFn {
5757
fn call(self, cmd: Command) -> Command {
5858
self.0(cmd)
5959
}
60-
61-
/// Chain `next_fn` with the current deferred function.
62-
///
63-
/// Using this function always allocates memory since it creates a new closure that captures
64-
/// `next_fn` & `self.0`
65-
#[inline(always)]
66-
fn then<F>(self, next_fn: F) -> Self
67-
where
68-
F: FnOnce(Command) -> Command + Send + Sync + 'static + Clone,
69-
{
70-
Self::new(move |cmd| next_fn(self.0(cmd)))
71-
}
7260
}
7361

7462
impl fmt::Debug for DeferFn {
@@ -611,11 +599,9 @@ impl Command {
611599
/// This is useful for large applications to delay definitions of subcommands until they are
612600
/// being invoked.
613601
///
614-
/// Calling this function multiple times will chain the calls to each function provided.
602+
/// Calling this function multiple times will replace existing deferred calls.
615603
///
616-
/// This function will allocate on the heap in the following situations:
617-
/// 1. when using the chaining feature.
618-
/// 2. when the `deferred` argument is a capturing closure
604+
/// This function will allocate on the heap if the `deferred` argument is a capturing closure
619605
///
620606
/// # Examples
621607
///
@@ -629,21 +615,14 @@ impl Command {
629615
/// .defer(|cmd| {
630616
/// cmd.arg(arg!(<config> "Required configuration file to use"))
631617
/// })
632-
/// .defer(move |cmd| {
633-
/// cmd.version(version)
634-
/// })
635618
/// )
636619
/// # ;
637620
/// ```
638621
pub fn defer<F>(mut self, deferred: F) -> Self
639622
where
640623
F: FnOnce(Command) -> Command + Send + Sync + 'static + Clone,
641624
{
642-
let defer_fn = match self.deferred {
643-
Some(existing_fn) => existing_fn.then(deferred),
644-
None => DeferFn::new(deferred),
645-
};
646-
self.deferred = Some(defer_fn);
625+
self.deferred = Some(DeferFn::new(deferred));
647626
self
648627
}
649628

tests/builder/propagate_globals.rs

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clap::{Arg, ArgAction, ArgMatches, Command};
22

33
fn get_app() -> Command {
4-
let captured_name = "inner1";
54
Command::new("myprog")
65
.arg(
76
Arg::new("GLOBAL_ARG")
@@ -19,8 +18,7 @@ fn get_app() -> Command {
1918
.action(ArgAction::Count),
2019
)
2120
.subcommand(Command::new("outer")
22-
.defer(|cmd| cmd.subcommand(Command::new("inner")))
23-
.defer(move |cmd| cmd.subcommand(Command::new(captured_name))))
21+
.defer(|cmd| cmd.subcommand(Command::new("inner"))))
2422
}
2523

2624
fn get_matches(cmd: Command, argv: &'static str) -> ArgMatches {
@@ -39,12 +37,6 @@ fn get_inner_matches(m: &ArgMatches) -> &ArgMatches {
3937
.expect("could not access inner subcommand")
4038
}
4139

42-
fn get_inner_capture_matches(m: &ArgMatches) -> &ArgMatches {
43-
get_outer_matches(m)
44-
.subcommand_matches("inner1")
45-
.expect("could not access inner1 subcommand")
46-
}
47-
4840
fn top_can_access_arg<T: Into<Option<&'static str>>>(m: &ArgMatches, val: T) -> bool {
4941
m.get_one::<String>("GLOBAL_ARG").map(|v| v.as_str()) == val.into()
5042
}
@@ -56,13 +48,6 @@ fn inner_can_access_arg<T: Into<Option<&'static str>>>(m: &ArgMatches, val: T) -
5648
== val.into()
5749
}
5850

59-
fn inner_capture_can_access_arg<T: Into<Option<&'static str>>>(m: &ArgMatches, val: T) -> bool {
60-
get_inner_capture_matches(m)
61-
.get_one::<String>("GLOBAL_ARG")
62-
.map(|v| v.as_str())
63-
== val.into()
64-
}
65-
6651
fn outer_can_access_arg<T: Into<Option<&'static str>>>(m: &ArgMatches, val: T) -> bool {
6752
get_outer_matches(m)
6853
.get_one::<String>("GLOBAL_ARG")
@@ -81,12 +66,6 @@ fn inner_can_access_flag(m: &ArgMatches, present: bool, occurrences: u8) -> bool
8166
&& (m.get_one::<u8>("GLOBAL_FLAG").copied() == Some(occurrences))
8267
}
8368

84-
fn inner_capture_can_access_flag(m: &ArgMatches, present: bool, occurrences: u8) -> bool {
85-
let m = get_inner_capture_matches(m);
86-
(m.contains_id("GLOBAL_FLAG") == present)
87-
&& (m.get_one::<u8>("GLOBAL_FLAG").copied() == Some(occurrences))
88-
}
89-
9069
fn outer_can_access_flag(m: &ArgMatches, present: bool, occurrences: u8) -> bool {
9170
let m = get_outer_matches(m);
9271
(m.contains_id("GLOBAL_FLAG") == present)
@@ -100,12 +79,6 @@ fn global_arg_used_top_level() {
10079
assert!(top_can_access_arg(&m, "some_value"));
10180
assert!(inner_can_access_arg(&m, "some_value"));
10281
assert!(outer_can_access_arg(&m, "some_value"));
103-
104-
let m = get_matches(get_app(), "myprog --global-arg=some_value outer inner1");
105-
106-
assert!(top_can_access_arg(&m, "some_value"));
107-
assert!(inner_capture_can_access_arg(&m, "some_value"));
108-
assert!(outer_can_access_arg(&m, "some_value"));
10982
}
11083

11184
#[test]
@@ -115,12 +88,6 @@ fn global_arg_used_outer() {
11588
assert!(top_can_access_arg(&m, "some_value"));
11689
assert!(inner_can_access_arg(&m, "some_value"));
11790
assert!(outer_can_access_arg(&m, "some_value"));
118-
119-
let m = get_matches(get_app(), "myprog outer --global-arg=some_value inner1");
120-
121-
assert!(top_can_access_arg(&m, "some_value"));
122-
assert!(inner_capture_can_access_arg(&m, "some_value"));
123-
assert!(outer_can_access_arg(&m, "some_value"));
12491
}
12592

12693
#[test]
@@ -130,12 +97,6 @@ fn global_arg_used_inner() {
13097
assert!(top_can_access_arg(&m, "some_value"));
13198
assert!(inner_can_access_arg(&m, "some_value"));
13299
assert!(outer_can_access_arg(&m, "some_value"));
133-
134-
let m = get_matches(get_app(), "myprog outer inner1 --global-arg=some_value");
135-
136-
assert!(top_can_access_arg(&m, "some_value"));
137-
assert!(inner_capture_can_access_arg(&m, "some_value"));
138-
assert!(outer_can_access_arg(&m, "some_value"));
139100
}
140101

141102
#[test]
@@ -145,12 +106,6 @@ fn global_arg_default_value() {
145106
assert!(top_can_access_arg(&m, "default_value"));
146107
assert!(inner_can_access_arg(&m, "default_value"));
147108
assert!(outer_can_access_arg(&m, "default_value"));
148-
149-
let m = get_matches(get_app(), "myprog outer inner1");
150-
151-
assert!(top_can_access_arg(&m, "default_value"));
152-
assert!(inner_capture_can_access_arg(&m, "default_value"));
153-
assert!(outer_can_access_arg(&m, "default_value"));
154109
}
155110

156111
#[test]
@@ -160,12 +115,6 @@ fn global_flag_used_top_level() {
160115
assert!(top_can_access_flag(&m, true, 1));
161116
assert!(inner_can_access_flag(&m, true, 1));
162117
assert!(outer_can_access_flag(&m, true, 1));
163-
164-
let m = get_matches(get_app(), "myprog --global-flag outer inner1");
165-
166-
assert!(top_can_access_flag(&m, true, 1));
167-
assert!(inner_capture_can_access_flag(&m, true, 1));
168-
assert!(outer_can_access_flag(&m, true, 1));
169118
}
170119

171120
#[test]
@@ -175,12 +124,6 @@ fn global_flag_used_outer() {
175124
assert!(top_can_access_flag(&m, true, 1));
176125
assert!(inner_can_access_flag(&m, true, 1));
177126
assert!(outer_can_access_flag(&m, true, 1));
178-
179-
let m = get_matches(get_app(), "myprog outer --global-flag inner1");
180-
181-
assert!(top_can_access_flag(&m, true, 1));
182-
assert!(inner_capture_can_access_flag(&m, true, 1));
183-
assert!(outer_can_access_flag(&m, true, 1));
184127
}
185128

186129
#[test]
@@ -190,12 +133,6 @@ fn global_flag_used_inner() {
190133
assert!(top_can_access_flag(&m, true, 1));
191134
assert!(inner_can_access_flag(&m, true, 1));
192135
assert!(outer_can_access_flag(&m, true, 1));
193-
194-
let m = get_matches(get_app(), "myprog outer inner1 --global-flag");
195-
196-
assert!(top_can_access_flag(&m, true, 1));
197-
assert!(inner_capture_can_access_flag(&m, true, 1));
198-
assert!(outer_can_access_flag(&m, true, 1));
199136
}
200137

201138
#[test]
@@ -205,12 +142,6 @@ fn global_flag_2x_used_top_level() {
205142
assert!(top_can_access_flag(&m, true, 2));
206143
assert!(inner_can_access_flag(&m, true, 2));
207144
assert!(outer_can_access_flag(&m, true, 2));
208-
209-
let m = get_matches(get_app(), "myprog --global-flag --global-flag outer inner1");
210-
211-
assert!(top_can_access_flag(&m, true, 2));
212-
assert!(inner_capture_can_access_flag(&m, true, 2));
213-
assert!(outer_can_access_flag(&m, true, 2));
214145
}
215146

216147
#[test]
@@ -220,10 +151,4 @@ fn global_flag_2x_used_inner() {
220151
assert!(top_can_access_flag(&m, true, 2));
221152
assert!(inner_can_access_flag(&m, true, 2));
222153
assert!(outer_can_access_flag(&m, true, 2));
223-
224-
let m = get_matches(get_app(), "myprog outer inner1 --global-flag --global-flag");
225-
226-
assert!(top_can_access_flag(&m, true, 2));
227-
assert!(inner_capture_can_access_flag(&m, true, 2));
228-
assert!(outer_can_access_flag(&m, true, 2));
229154
}

tests/builder/subcommands.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,10 @@ fn cant_have_args_with_multicall() {
545545
#[test]
546546
fn multicall_help_flag() {
547547
static EXPECTED: &str = "\
548-
Usage: foo bar [value] [value1]
548+
Usage: foo bar [value]
549549
550550
Arguments:
551-
[value]
552-
[value1]
551+
[value]
553552
554553
Options:
555554
-h, --help Print help
@@ -561,8 +560,7 @@ Options:
561560
.multicall(true)
562561
.subcommand(Command::new("foo").defer(|cmd| {
563562
cmd.subcommand(Command::new("bar")
564-
.defer(|cmd| cmd.arg(Arg::new("value")))
565-
.defer(|cmd| cmd.arg(Arg::new("value1"))))
563+
.defer(|cmd| cmd.arg(Arg::new("value"))))
566564
}));
567565
utils::assert_output(cmd, "foo bar --help", EXPECTED, false);
568566
}

0 commit comments

Comments
 (0)