|
1 |
| -use anyhow::anyhow; |
| 1 | +use anyhow::{anyhow, Context as _}; |
2 | 2 | use cargo::core::{features, CliUnstable};
|
3 | 3 | use cargo::{self, drop_print, drop_println, CliResult, Config};
|
4 | 4 | use clap::{
|
@@ -55,6 +55,13 @@ pub fn main(config: &mut Config) -> CliResult {
|
55 | 55 | }
|
56 | 56 | };
|
57 | 57 |
|
| 58 | + if let Some(new_cwd) = args.get_one::<std::path::PathBuf>("directory") { |
| 59 | + // Change the configured directory to work in, before any config files are read |
| 60 | + config |
| 61 | + .set_cwd(new_cwd.as_path()) |
| 62 | + .with_context(|| "could not change to requested directory")?; |
| 63 | + } |
| 64 | + |
58 | 65 | // Global args need to be extracted before expanding aliases because the
|
59 | 66 | // clap code for extracting a subcommand discards global options
|
60 | 67 | // (appearing before the subcommand).
|
@@ -473,6 +480,13 @@ See 'cargo help <command>' for more information on a specific command.\n",
|
473 | 480 | .value_name("WHEN")
|
474 | 481 | .global(true),
|
475 | 482 | )
|
| 483 | + .arg( |
| 484 | + opt("directory", "Change to DIRECTORY before doing anything") |
| 485 | + .short('C') |
| 486 | + .value_name("DIRECTORY") |
| 487 | + .value_hint(clap::ValueHint::DirPath) |
| 488 | + .value_parser(clap::builder::ValueParser::path_buf()), |
| 489 | + ) |
476 | 490 | .arg(flag("frozen", "Require Cargo.lock and cache are up to date").global(true))
|
477 | 491 | .arg(flag("locked", "Require Cargo.lock is up to date").global(true))
|
478 | 492 | .arg(flag("offline", "Run without accessing the network").global(true))
|
|
0 commit comments