summaryrefslogtreecommitdiff
path: root/cli/flags.rs
diff options
context:
space:
mode:
authorKayla Washburn <mckayla@hey.com>2022-06-08 15:30:16 -0600
committerGitHub <noreply@github.com>2022-06-08 15:30:16 -0600
commit94068b7109af64eba2cb9fd299e62705a5358069 (patch)
tree7ba43427fb32d43009437898bbd336932c02e754 /cli/flags.rs
parenta0a13b3a1b5b654ad36a25c4785cab539555840a (diff)
feat(task): add `--cwd` flag for configuring the working directory (#14823)
Diffstat (limited to 'cli/flags.rs')
-rw-r--r--cli/flags.rs63
1 files changed, 60 insertions, 3 deletions
diff --git a/cli/flags.rs b/cli/flags.rs
index dbb716df8..f4f162dcd 100644
--- a/cli/flags.rs
+++ b/cli/flags.rs
@@ -163,6 +163,7 @@ pub struct RunFlags {
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct TaskFlags {
+ pub cwd: Option<String>,
pub task: String,
}
@@ -1465,6 +1466,14 @@ fn task_subcommand<'a>() -> Command<'a> {
Command::new("task")
.trailing_var_arg(true)
.args(config_args())
+ .arg(
+ Arg::new("cwd")
+ .long("cwd")
+ .value_name("DIR")
+ .help("Specify the directory to run the task in")
+ .takes_value(true)
+ .value_hint(ValueHint::DirPath)
+ )
// Ideally the task name and trailing arguments should be two separate clap
// arguments, but there is a bug in clap that's preventing us from doing
// this (https://github.com/clap-rs/clap/issues/1538). Once that's fixed,
@@ -2567,7 +2576,15 @@ fn task_parse(
) {
config_args_parse(flags, matches);
- let mut task_name = "".to_string();
+ let mut task_flags = TaskFlags {
+ cwd: None,
+ task: String::new(),
+ };
+
+ if let Some(cwd) = matches.value_of("cwd") {
+ task_flags.cwd = Some(cwd.to_string());
+ }
+
if let Some(mut index) = matches.index_of("task_name_and_args") {
index += 1; // skip `task`
@@ -2578,6 +2595,10 @@ fn task_parse(
flags.config_flag = ConfigFlag::Path(raw_args[index + 1].to_string());
index += 2;
}
+ "--cwd" => {
+ task_flags.cwd = Some(raw_args[index + 1].to_string());
+ index += 2;
+ }
"--no-config" => {
flags.config_flag = ConfigFlag::Disabled;
index += 1;
@@ -2591,7 +2612,7 @@ fn task_parse(
}
if index < raw_args.len() {
- task_name = raw_args[index].to_string();
+ task_flags.task = raw_args[index].to_string();
index += 1;
if index < raw_args.len() {
@@ -2602,7 +2623,7 @@ fn task_parse(
}
}
- flags.subcommand = DenoSubcommand::Task(TaskFlags { task: task_name });
+ flags.subcommand = DenoSubcommand::Task(task_flags);
}
fn test_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
@@ -5597,6 +5618,7 @@ mod tests {
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Task(TaskFlags {
+ cwd: None,
task: "build".to_string(),
}),
argv: svec!["hello", "world"],
@@ -5609,6 +5631,19 @@ mod tests {
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Task(TaskFlags {
+ cwd: None,
+ task: "build".to_string(),
+ }),
+ ..Flags::default()
+ }
+ );
+
+ let r = flags_from_vec(svec!["deno", "task", "--cwd", "foo", "build"]);
+ assert_eq!(
+ r.unwrap(),
+ Flags {
+ subcommand: DenoSubcommand::Task(TaskFlags {
+ cwd: Some("foo".to_string()),
task: "build".to_string(),
}),
..Flags::default()
@@ -5632,6 +5667,7 @@ mod tests {
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Task(TaskFlags {
+ cwd: None,
task: "build".to_string(),
}),
argv: svec!["--", "hello", "world"],
@@ -5639,6 +5675,21 @@ mod tests {
..Flags::default()
}
);
+
+ let r = flags_from_vec(svec![
+ "deno", "task", "--cwd", "foo", "build", "--", "hello", "world"
+ ]);
+ assert_eq!(
+ r.unwrap(),
+ Flags {
+ subcommand: DenoSubcommand::Task(TaskFlags {
+ cwd: Some("foo".to_string()),
+ task: "build".to_string(),
+ }),
+ argv: svec!["--", "hello", "world"],
+ ..Flags::default()
+ }
+ );
}
#[test]
@@ -5649,6 +5700,7 @@ mod tests {
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Task(TaskFlags {
+ cwd: None,
task: "build".to_string(),
}),
argv: svec!["--"],
@@ -5664,6 +5716,7 @@ mod tests {
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Task(TaskFlags {
+ cwd: None,
task: "build".to_string(),
}),
argv: svec!["-1", "--test"],
@@ -5679,6 +5732,7 @@ mod tests {
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Task(TaskFlags {
+ cwd: None,
task: "build".to_string(),
}),
argv: svec!["--test"],
@@ -5694,6 +5748,7 @@ mod tests {
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Task(TaskFlags {
+ cwd: None,
task: "".to_string(),
}),
..Flags::default()
@@ -5708,6 +5763,7 @@ mod tests {
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Task(TaskFlags {
+ cwd: None,
task: "".to_string(),
}),
config_flag: ConfigFlag::Path("deno.jsonc".to_string()),
@@ -5723,6 +5779,7 @@ mod tests {
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Task(TaskFlags {
+ cwd: None,
task: "".to_string(),
}),
config_flag: ConfigFlag::Path("deno.jsonc".to_string()),