Skip to content

Commit 27674f8

Browse files
committed
docs(cli): Add CLI documentation page with argparse output
- Add create_parser() function to g module for documentation and --version - Create docs/cli/index.md using sphinx-argparse directive - Add cli/index to docs toctree - Support --version/-V flag in CLI
1 parent 74fed7f commit 27674f8

3 files changed

Lines changed: 112 additions & 5 deletions

File tree

docs/cli/index.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
(cli)=
2+
3+
# CLI
4+
5+
g is a minimal CLI wrapper that proxies to your current directory's VCS command.
6+
7+
## How it works
8+
9+
When you run `g`, it:
10+
11+
1. Walks up from your current directory looking for `.git`, `.svn`, or `.hg`
12+
2. Invokes the corresponding VCS (`git`, `svn`, or `hg`) with your arguments
13+
3. Exits after the command completes
14+
15+
## Usage
16+
17+
```console
18+
$ g status
19+
```
20+
21+
Is equivalent to:
22+
23+
```console
24+
$ git status # if in a git repo
25+
$ svn status # if in an svn repo
26+
$ hg status # if in an hg repo
27+
```
28+
29+
(cli-main)=
30+
31+
## Command
32+
33+
```{eval-rst}
34+
.. argparse::
35+
:module: g
36+
:func: create_parser
37+
:prog: g
38+
```
39+
40+
## Examples
41+
42+
```console
43+
$ g status
44+
$ g commit -m "Fix bug"
45+
$ g log --oneline -10
46+
$ g diff HEAD~1
47+
```
48+
49+
## Supported VCS
50+
51+
| Directory marker | VCS command |
52+
|------------------|-------------|
53+
| `.git` | `git` |
54+
| `.svn` | `svn` |
55+
| `.hg` | `hg` |

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
:hidden:
99
1010
quickstart
11+
cli/index
1112
```
1213

1314
```{toctree}

src/g/__init__.py

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from __future__ import annotations
55

6-
import io
6+
import argparse
77
import logging
88
import os
99
import pathlib
@@ -12,7 +12,9 @@
1212
import typing as t
1313
from os import PathLike
1414

15-
__all__ = ["DEFAULT", "run", "sys", "vcspath_registry"]
15+
from g.__about__ import __version__
16+
17+
__all__ = ["DEFAULT", "create_parser", "run", "sys", "vcspath_registry"]
1618

1719
vcspath_registry = {".git": "git", ".svn": "svn", ".hg": "hg"}
1820

@@ -28,6 +30,49 @@ def find_repo_type(path: pathlib.Path | str) -> str | None:
2830
return None
2931

3032

33+
def create_parser() -> argparse.ArgumentParser:
34+
"""Create argument parser for g CLI.
35+
36+
Returns
37+
-------
38+
argparse.ArgumentParser
39+
Configured argument parser for the g command.
40+
41+
Examples
42+
--------
43+
>>> parser = create_parser()
44+
>>> parser.prog
45+
'g'
46+
47+
>>> args = parser.parse_args(['status'])
48+
>>> args.vcs_args
49+
['status']
50+
51+
>>> args = parser.parse_args(['commit', '-m', 'message'])
52+
>>> args.vcs_args
53+
['commit', '-m', 'message']
54+
"""
55+
parser = argparse.ArgumentParser(
56+
prog="g",
57+
description="CLI alias for your current directory's VCS command (git, svn, hg).",
58+
epilog="All arguments are passed directly to the detected VCS.",
59+
formatter_class=argparse.RawDescriptionHelpFormatter,
60+
)
61+
parser.add_argument(
62+
"--version",
63+
"-V",
64+
action="version",
65+
version=f"%(prog)s {__version__}",
66+
)
67+
parser.add_argument(
68+
"vcs_args",
69+
nargs=argparse.REMAINDER,
70+
metavar="...",
71+
help="Arguments passed to the detected VCS (git, svn, or hg)",
72+
)
73+
return parser
74+
75+
3176
DEFAULT = object()
3277

3378

@@ -47,19 +92,25 @@ def run(
4792
returned, it would print *<Popen: returncode: 1 args: ['git']>* after command.
4893
"""
4994
# Interpret default kwargs lazily for mockability of argv
50-
if cmd is DEFAULT:
51-
cmd = find_repo_type(pathlib.Path.cwd())
5295
if cmd_args is DEFAULT:
5396
cmd_args = sys.argv[1:]
5497

98+
# Handle --version/-V before VCS detection
99+
assert isinstance(cmd_args, (tuple, list))
100+
if cmd_args and cmd_args[0] in ("--version", "-V"):
101+
parser = create_parser()
102+
parser.parse_args(["--version"]) # Will print version and exit
103+
104+
if cmd is DEFAULT:
105+
cmd = find_repo_type(pathlib.Path.cwd())
106+
55107
logging.basicConfig(level=logging.INFO, format="%(message)s")
56108

57109
if cmd is None:
58110
msg = "No VCS found in current directory."
59111
log.info(msg)
60112
return None
61113

62-
assert isinstance(cmd_args, (tuple, list))
63114
assert isinstance(cmd, (str, bytes, pathlib.Path))
64115

65116
proc = subprocess.Popen([cmd, *cmd_args], **kwargs)

0 commit comments

Comments
 (0)