Skip to content

Commit 1a668ae

Browse files
committed
fix(task) subcommand parser skips global args
1 parent f8fee6c commit 1a668ae

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

cli/args/flags.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,15 @@ pub fn flags_from_vec(args: Vec<String>) -> clap::Result<Flags> {
554554
Some(("lsp", m)) => lsp_parse(&mut flags, m),
555555
Some(("repl", m)) => repl_parse(&mut flags, m),
556556
Some(("run", m)) => run_parse(&mut flags, m),
557-
Some(("task", m)) => task_parse(&mut flags, m, &args),
557+
Some(("task", m)) => task_parse(
558+
&mut flags,
559+
m,
560+
// temporary workaround until https://github.com/clap-rs/clap/issues/1538 is fixed
561+
&args[args
562+
.iter()
563+
.position(|el| el == matches.subcommand_name().unwrap())
564+
.unwrap()..],
565+
),
558566
Some(("test", m)) => test_parse(&mut flags, m),
559567
Some(("types", m)) => types_parse(&mut flags, m),
560568
Some(("uninstall", m)) => uninstall_parse(&mut flags, m),
@@ -2561,6 +2569,9 @@ fn run_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
25612569
flags.subcommand = DenoSubcommand::Run(RunFlags { script });
25622570
}
25632571

2572+
/// raw_args is the subcommand's command line, eg. for the deno invocation
2573+
/// `deno task echo`
2574+
/// raw_args has value ["task", "echo"]
25642575
fn task_parse(
25652576
flags: &mut Flags,
25662577
matches: &clap::ArgMatches,
@@ -2582,8 +2593,6 @@ fn task_parse(
25822593
}
25832594

25842595
if let Some(mut index) = matches.index_of("task_name_and_args") {
2585-
index += 1; // skip `task`
2586-
25872596
// temporary workaround until https://github.com/clap-rs/clap/issues/1538 is fixed
25882597
while index < raw_args.len() {
25892598
match raw_args[index].as_str() {
@@ -5764,6 +5773,25 @@ mod tests {
57645773
);
57655774
}
57665775

5776+
#[test]
5777+
fn task_with_global_flags() {
5778+
// can fail if the custom parser in task_parse() starts at the wrong index
5779+
let r =
5780+
flags_from_vec(svec!["deno", "--quiet", "--unstable", "task", "build"]);
5781+
assert_eq!(
5782+
r.unwrap(),
5783+
Flags {
5784+
subcommand: DenoSubcommand::Task(TaskFlags {
5785+
cwd: None,
5786+
task: "build".to_string(),
5787+
}),
5788+
unstable: true,
5789+
log_level: Some(log::Level::Error),
5790+
..Flags::default()
5791+
}
5792+
);
5793+
}
5794+
57675795
#[test]
57685796
fn task_subcommand_empty() {
57695797
let r = flags_from_vec(svec!["deno", "task"]);

0 commit comments

Comments
 (0)