Skip to content

Commit ac7b36d

Browse files
committed
Support global flags after specs
1 parent a99c33f commit ac7b36d

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/rust/pants_ng/options/src/pants_invocation.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl PantsInvocation {
166166

167167
let mut unconsumed_args = args.arg_strings.into_iter().peekable();
168168

169-
let global_flags = consume_flags(&mut unconsumed_args);
169+
let mut global_flags = consume_flags(&mut unconsumed_args);
170170

171171
let mut commands = vec![];
172172
let mut cmd_opt = consume_cmd(&mut unconsumed_args);
@@ -191,6 +191,10 @@ impl PantsInvocation {
191191
));
192192
}
193193

194+
// Any flags after specs (but before passthru args) are considered to be global flags.
195+
// This makes it convenient to tack flags on at the end of an existing cmd line.
196+
global_flags.append(&mut consume_flags(&mut unconsumed_args));
197+
194198
let mut passthru = None;
195199
if let Some(s) = unconsumed_args.next() {
196200
if s == "--" {

src/rust/pants_ng/options/src/pants_invocation_tests.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,23 @@ fn test_command_and_subcommand() {
277277
);
278278

279279
assert_eq!(
280-
mk_invocation("pants --foo cmd --bar=baz subcmd --qux=quux path/to/file1 ./path2 -- passthru_arg --passthru-flag").unwrap(),
280+
mk_invocation("pants --foo cmd --bar=baz subcmd --qux=quux path/to/file1 ./path2 --global-flag-after-specs=val").unwrap(),
281281
PantsInvocation {
282-
global_flags: vec![mk_flag("foo", None),],
282+
global_flags: vec![mk_flag("foo", None), mk_flag("global-flag-after-specs", Some("val")),],
283+
commands: vec![mk_command(
284+
"cmd",
285+
vec![mk_flag("bar", Some("baz")),],
286+
Some(mk_subcommand("subcmd", vec![mk_flag("qux", Some("quux"))]))
287+
)],
288+
specs: vec!["path/to/file1".to_string(), "./path2".to_string()],
289+
passthru: None,
290+
},
291+
);
292+
293+
assert_eq!(
294+
mk_invocation("pants --foo cmd --bar=baz subcmd --qux=quux path/to/file1 ./path2 --global-flag-after-specs -- passthru_arg --passthru-flag").unwrap(),
295+
PantsInvocation {
296+
global_flags: vec![mk_flag("foo", None), mk_flag("global-flag-after-specs", None),],
283297
commands: vec![mk_command(
284298
"cmd",
285299
vec![mk_flag("bar", Some("baz")),],
@@ -311,9 +325,9 @@ fn test_multiple_commands_and_subcommands() {
311325
);
312326

313327
assert_eq!(
314-
mk_invocation("pants --global-flag cmd1 --cmd1-flag subcmd1 --subcmd1-flag + cmd2 --cmd2-flag + cmd3 --cmd3-flag subcmd3 --subcmd3-flag path/to/spec -- passthru").unwrap(),
328+
mk_invocation("pants --global-flag cmd1 --cmd1-flag subcmd1 --subcmd1-flag + cmd2 --cmd2-flag + cmd3 --cmd3-flag subcmd3 --subcmd3-flag path/to/spec --another-global-flag -- passthru").unwrap(),
315329
PantsInvocation {
316-
global_flags: vec![mk_flag("global-flag", None)],
330+
global_flags: vec![mk_flag("global-flag", None), mk_flag("another-global-flag", None),],
317331
commands: vec![mk_command(
318332
"cmd1",
319333
vec![mk_flag("cmd1-flag", None)],

0 commit comments

Comments
 (0)