Skip to content

Commit 578f21d

Browse files
committed
--chdir/-C
1 parent 1822b08 commit 578f21d

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ jobs:
195195
PKGX_DIR: ${{ github.workspace }}/pkgx
196196
PKGX_DIST_URL: https://dist.pkgx.dev
197197

198+
- name: --chdir
199+
run: |
200+
d=$(mktemp -d)
201+
test $(pwd) != $d
202+
test $(pkgx -C $d -- pwd) = $d
203+
test $(pkgx --chdir $d -- pwd) = $d
204+
198205
- name: generate coverage
199206
run: |
200207
pkgx +llvm.org -- llvm-profdata merge -sparse $(find . -name default_\*.prof\*) -o default.profdata

crates/cli/src/args.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct Flags {
1414
pub version_n_continue: bool,
1515
pub shebang: bool,
1616
pub sync: bool,
17+
pub chdir: Option<String>,
1718
}
1819

1920
pub struct Args {
@@ -36,9 +37,11 @@ pub fn parse() -> Args {
3637
let mut version_n_continue = false;
3738
let mut shebang = false;
3839
let mut sync = false;
40+
let mut chdir = None;
3941
let json_latest_v: isize = 2;
4042

41-
for arg in std::env::args().skip(1) {
43+
let mut args_iter = std::env::args().skip(1);
44+
while let Some(arg) = args_iter.next() {
4245
if collecting_args {
4346
args.push(arg);
4447
} else if arg.starts_with('+') {
@@ -59,6 +62,7 @@ pub fn parse() -> Args {
5962
}
6063
json = Some(2);
6164
}
65+
"--chdir" | "--cd" => chdir = args_iter.next(),
6266
"--json=v1" => json = Some(1),
6367
"--json=v2" => json = Some(2),
6468
"--silent" => silent = true,
@@ -105,6 +109,7 @@ pub fn parse() -> Args {
105109
'v' => version_n_continue = true,
106110
'!' => shebang = true,
107111
'Q' => mode = Mode::Query,
112+
'C' => chdir = args_iter.next(),
108113
_ => panic!("unknown argument: -{}", c),
109114
}
110115
}
@@ -127,6 +132,7 @@ pub fn parse() -> Args {
127132
quiet,
128133
version_n_continue,
129134
sync,
135+
chdir,
130136
},
131137
}
132138
}

crates/cli/src/help.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ modes:
2828
$ pkgx --version
2929
3030
flags:
31-
-q, --quiet # suppress brief informational messages
32-
-qq, --silent # no chat. no errors. just execute.
33-
-v # print version and continue
34-
--sync # sync first (note: rarely if ever needed)
35-
-j,--json=v2 # output JSON (if sensible)
31+
-q, --quiet # suppress brief informational messages
32+
-qq, --silent # no chat. no errors. just execute.
33+
-j, --json=v2 # output JSON (if sensible)
34+
-C, --chdir <d> # change directory first
35+
--sync # sync first (note: rarely if ever needed)
36+
-v # print version and continue
3637
3738
more:
3839
$ OPEN https://docs.pkgx.sh

crates/cli/src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2424
find_program,
2525
} = args::parse();
2626

27+
if let Some(dir) = &flags.chdir {
28+
std::env::set_current_dir(dir)?;
29+
}
30+
2731
if flags.version_n_continue {
2832
eprintln!("pkgx {}", env!("CARGO_PKG_VERSION"));
2933
}

0 commit comments

Comments
 (0)