feat(cli): add --cwd DIR to run against another project (closes #69) - #80
Merged
shauryagangrade merged 1 commit intoAug 14, 2026
Merged
Conversation
…yagangrade#69) GCode always operated in the directory it was launched from, so pointing it at another project meant cd-ing there and back. --cwd DIR does what git -C does. The chdir happens immediately after parse_args and before load_env(), which is the ordering that matters: .gcoderc discovery (load_config(project_root= os.getcwd())), the banner and every tool resolve paths against the working directory, so a chdir performed any later would leave them describing different projects in one session. A bad --cwd goes through parser.error() -- one line, exit 2, no traceback -- matching how the CLI reports other bad input. OSError covers both a missing directory and a path that is a file, and the message carries strerror so the two are distinguishable. Tests: five in test_cli.py -- --help lists the flag; the chdir is observed from load_env, proving it precedes config loading; the directory is untouched without the flag; and a missing directory and a file both exit 2. Two fail on main.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #69.
What
gcode --cwd DIR, the equivalent ofgit -C:The banner shows
/some/project,.gcodercis read from there, and every tool resolves paths against it.The ordering is the whole feature
The
chdirruns immediately afterparse_args, beforeload_env(). That placement is what makes the acceptance criteria hold:.gcodercdiscovery (load_config(project_root=os.getcwd())),ui.banner(..., os.getcwd())and all the tools resolve against the working directory. Achdirperformed any later leaves them describing different projects within one session — config from the launch directory, tools operating in the target.So rather than assert the banner text, the test observes the working directory from inside
load_env— the first call after the chdir — which pins the ordering directly:Errors
A bad
--cwdgoes throughparser.error(): one line on stderr, exit 2, no traceback — the same treatment as other bad input.OSErrorcovers both a missing directory and a path that turns out to be a file, and the message carriesstrerrorso the two are distinguishable:Tests
Five in
tests/test_cli.py:--helplists the flagload_env, proving it precedes config loadingEach restores the original cwd in a
finally, so a failure can't leak into later tests. Two fail onmain(checked by stashing onlygcode/cli.py).Verification
pytest tests/test_cli.py— 15 passed.ruff checkclean.Full suite on this branch: 57 passed, 2 failed — the 2 are
test_tools.py::test_grep/::test_grep_ignore_case, which fail on a Windows checkout ofmainfor an unrelated reason, sent separately as #78.The issue mentions
load_config(project_root=...)from #48; that already exists onmainand needed no change, since it readsos.getcwd()at call time.