Replace manual ctx.args parsing with a proper @click.option using is_flag=False and flag_value='.'. This enables tab completion for the --cwd / -C option and directory-only path completion after it. Also bump click dependency to >=8.3.2 to avoid the optional-value regression in 8.3.0/8.3.1.
88 lines
2.4 KiB
Python
88 lines
2.4 KiB
Python
"""Start Claude Code in the myclaude project directory."""
|
|
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import click
|
|
from rich.console import Console
|
|
|
|
from myclaude.project_root import get_myclaude_project_root
|
|
|
|
console = Console()
|
|
stderr_console = Console(stderr=True)
|
|
|
|
|
|
def _resolve_chat_cwd() -> Path:
|
|
root = get_myclaude_project_root()
|
|
fallback = Path.home() / ".myclaude"
|
|
if root == fallback:
|
|
stderr_console.print(
|
|
"[red]Cannot resolve myclaude project root.[/red] Run from the repo or set "
|
|
"[cyan]MYCLAUDE_PROJECT_ROOT[/cyan].",
|
|
)
|
|
raise SystemExit(1)
|
|
return root
|
|
|
|
|
|
@click.command(
|
|
"chat",
|
|
context_settings={
|
|
"ignore_unknown_options": True,
|
|
"allow_extra_args": True,
|
|
},
|
|
)
|
|
@click.option(
|
|
"--dangerously-skip-permissions",
|
|
is_flag=True,
|
|
default=False,
|
|
help="Forward to claude: bypass permission checks (sandbox / isolated use only).",
|
|
)
|
|
@click.option(
|
|
"--cwd",
|
|
"-C",
|
|
is_flag=False,
|
|
flag_value=".",
|
|
default=None,
|
|
type=click.Path(dir_okay=True, file_okay=False),
|
|
help="Use specified path as working directory (default: myclaude project root).",
|
|
)
|
|
@click.pass_context
|
|
def chat_cmd(
|
|
ctx: click.Context,
|
|
dangerously_skip_permissions: bool,
|
|
cwd: str | None,
|
|
) -> None:
|
|
"""Run `claude` in the myclaude repo so CLAUDE.md and .claude/ apply; extra args pass through.
|
|
|
|
\b
|
|
Working directory options:
|
|
--cwd, -C Use current directory as working directory
|
|
--cwd /path, -C/path Use specified path as working directory
|
|
(none) Use myclaude project root (default)
|
|
"""
|
|
binary = os.environ.get("CLAUDE_BIN") or shutil.which("claude")
|
|
if not binary:
|
|
stderr_console.print(
|
|
"[red]claude CLI not found in PATH.[/red] Install Claude Code or set "
|
|
"[cyan]CLAUDE_BIN[/cyan].",
|
|
)
|
|
raise SystemExit(127)
|
|
|
|
if cwd is None:
|
|
chat_cwd = _resolve_chat_cwd()
|
|
else:
|
|
chat_cwd = Path(cwd).resolve()
|
|
if not chat_cwd.is_dir():
|
|
stderr_console.print(f"[red]Not a directory:[/red] {cwd}")
|
|
raise SystemExit(1)
|
|
|
|
cmd: list[str] = [binary]
|
|
if dangerously_skip_permissions:
|
|
cmd.append("--dangerously-skip-permissions")
|
|
cmd.extend(ctx.args)
|
|
|
|
proc = subprocess.run(cmd, cwd=str(chat_cwd), check=False)
|
|
raise SystemExit(proc.returncode)
|