|
1 |
| -use anyhow::anyhow; |
| 1 | +use anyhow::{anyhow, Context as _}; |
2 | 2 | use cargo::core::shell::Shell;
|
3 | 3 | use cargo::core::{features, CliUnstable};
|
4 | 4 | use cargo::{self, drop_print, drop_println, CliResult, Config};
|
@@ -32,6 +32,13 @@ pub fn main(config: &mut LazyConfig) -> CliResult {
|
32 | 32 | // the [alias] table).
|
33 | 33 | let config = config.get_mut();
|
34 | 34 |
|
| 35 | + if let Some(new_cwd) = args.get_one::<std::path::PathBuf>("directory") { |
| 36 | + // Change the configured directory to work in, before any config files are read |
| 37 | + config |
| 38 | + .set_cwd(new_cwd.as_path()) |
| 39 | + .with_context(|| "could not change to requested directory")?; |
| 40 | + } |
| 41 | + |
35 | 42 | let (expanded_args, global_args) = expand_aliases(config, args, vec![])?;
|
36 | 43 |
|
37 | 44 | if expanded_args
|
@@ -467,6 +474,13 @@ See 'cargo help <command>' for more information on a specific command.\n",
|
467 | 474 | .value_name("WHEN")
|
468 | 475 | .global(true),
|
469 | 476 | )
|
| 477 | + .arg( |
| 478 | + opt("directory", "Change to DIRECTORY before doing anything") |
| 479 | + .short('C') |
| 480 | + .value_name("DIRECTORY") |
| 481 | + .value_hint(clap::ValueHint::DirPath) |
| 482 | + .value_parser(clap::builder::ValueParser::path_buf()), |
| 483 | + ) |
470 | 484 | .arg(flag("frozen", "Require Cargo.lock and cache are up to date").global(true))
|
471 | 485 | .arg(flag("locked", "Require Cargo.lock is up to date").global(true))
|
472 | 486 | .arg(flag("offline", "Run without accessing the network").global(true))
|
|
0 commit comments