argss 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- argss-0.1.0/.github/workflows/pypi.yml +30 -0
- argss-0.1.0/.gitignore +10 -0
- argss-0.1.0/LICENSE +21 -0
- argss-0.1.0/PKG-INFO +171 -0
- argss-0.1.0/README.md +143 -0
- argss-0.1.0/argss/__init__.py +6 -0
- argss-0.1.0/argss/argument.py +39 -0
- argss-0.1.0/argss/cli.py +151 -0
- argss-0.1.0/argss/utils.py +56 -0
- argss-0.1.0/pyproject.toml +40 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
name: Publish Python Package to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "v*"
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
deploy:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
|
|
12
|
+
steps:
|
|
13
|
+
- name: Checkout code
|
|
14
|
+
uses: actions/checkout@v4
|
|
15
|
+
|
|
16
|
+
- name: Install uv
|
|
17
|
+
uses: astral-sh/setup-uv@v5
|
|
18
|
+
with:
|
|
19
|
+
enable-cache: true
|
|
20
|
+
|
|
21
|
+
- name: Set up Python
|
|
22
|
+
run: uv python install 3.14
|
|
23
|
+
|
|
24
|
+
- name: Build package
|
|
25
|
+
run: uv build
|
|
26
|
+
|
|
27
|
+
- name: Publish to PyPI
|
|
28
|
+
env:
|
|
29
|
+
UV_PUBLISH_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
|
|
30
|
+
run: uv publish
|
argss-0.1.0/.gitignore
ADDED
argss-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 kernel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
argss-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: argss
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Write type-annotated Python functions, get a CLI with argparse's native `--help` — no magic, no bloat. Flat commands only, synchronous execution
|
|
5
|
+
Project-URL: Homepage, https://github.com/Fkernel653/argss
|
|
6
|
+
Project-URL: Repository, https://github.com/Fkernel653/argss.git
|
|
7
|
+
Project-URL: Documentation, https://github.com/Fkernel653/argss#readme
|
|
8
|
+
Author: Fkernel653
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: argparse,cli,command-line,framework,lightweight
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Classifier: Topic :: Software Development :: User Interfaces
|
|
22
|
+
Classifier: Topic :: Terminals
|
|
23
|
+
Classifier: Topic :: Utilities
|
|
24
|
+
Requires-Python: >=3.14
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# argss — Stupidly Simple CLI builder (sync-only, no groups)
|
|
30
|
+
|
|
31
|
+
[](https://python.org)
|
|
32
|
+
[](https://pypi.org/project/argss/)
|
|
33
|
+
[](LICENSE)
|
|
34
|
+
[]()
|
|
35
|
+
[](https://docs.astral.sh/ruff/)
|
|
36
|
+
|
|
37
|
+
**argss** is a lightweight fork of [arg-kiss](https://github.com/Fkernel653/arg-kiss) — stripped down to the essentials.
|
|
38
|
+
|
|
39
|
+
Write type-annotated Python functions, get a CLI with argparse's native `--help` — no magic, no bloat. Flat commands only, synchronous execution.
|
|
40
|
+
|
|
41
|
+
## 🎯 Why argss?
|
|
42
|
+
|
|
43
|
+
| Feature | arg-kiss | argss |
|
|
44
|
+
|---------|----------|-------|
|
|
45
|
+
| `@cli.command()` | ✅ | ✅ |
|
|
46
|
+
| Type inference | ✅ | ✅ |
|
|
47
|
+
| Boolean flags | ✅ | ✅ |
|
|
48
|
+
| Global arguments | ✅ | ✅ |
|
|
49
|
+
| Command groups | ✅ | ❌ |
|
|
50
|
+
| Async support | ✅ | ❌ |
|
|
51
|
+
| `color` parameter (Python 3.14+ coloured help) | ✅ | ❌ |
|
|
52
|
+
| Dependencies | none | none |
|
|
53
|
+
| Lines of code | ~150 | ~110 |
|
|
54
|
+
|
|
55
|
+
> **Note:** arg-kiss provides a `color` parameter in `CLI()` that enables/disables coloured `--help` output on Python 3.14+. argss omits this parameter for simplicity.
|
|
56
|
+
|
|
57
|
+
Use **argss** when you want:
|
|
58
|
+
- Minimal code footprint
|
|
59
|
+
- No asyncio overhead
|
|
60
|
+
- Flat command structure (no sub-subcommands)
|
|
61
|
+
- Faster import time (~30% faster than arg-kiss)
|
|
62
|
+
|
|
63
|
+
## 🚀 Quick Start
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
pip install argss
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from argss import CLI
|
|
71
|
+
|
|
72
|
+
cli = CLI(name="todo", description="Task manager")
|
|
73
|
+
|
|
74
|
+
@cli.command()
|
|
75
|
+
def add(task: str, priority: int = 1, done: bool = False):
|
|
76
|
+
"""Add a task."""
|
|
77
|
+
status = "✓" if done else "○"
|
|
78
|
+
print(f"[{status}] {task} (priority: {priority})")
|
|
79
|
+
|
|
80
|
+
@cli.command()
|
|
81
|
+
def list_all():
|
|
82
|
+
"""Show all tasks."""
|
|
83
|
+
print("Nothing yet!")
|
|
84
|
+
|
|
85
|
+
cli.run()
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
$ python todo.py add "Buy milk" --priority 2
|
|
90
|
+
[○] Buy milk (priority: 2)
|
|
91
|
+
|
|
92
|
+
$ python todo.py list-all
|
|
93
|
+
Nothing yet!
|
|
94
|
+
|
|
95
|
+
$ python todo.py --help
|
|
96
|
+
usage: todo [-h] {add,list-all} ...
|
|
97
|
+
|
|
98
|
+
Task manager
|
|
99
|
+
|
|
100
|
+
positional arguments:
|
|
101
|
+
{add,list-all}
|
|
102
|
+
add Add a task.
|
|
103
|
+
list-all Show all tasks.
|
|
104
|
+
|
|
105
|
+
options:
|
|
106
|
+
-h, --help show this help message and exit
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## 📋 Commands & Features
|
|
110
|
+
|
|
111
|
+
### `@cli.command()` — Define commands from functions
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
@cli.command()
|
|
115
|
+
def fetch(url: str, retries: int = 3):
|
|
116
|
+
"""Download from URL with retries"""
|
|
117
|
+
print(f"Fetched {url} (retries: {retries})")
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Type → CLI mapping
|
|
121
|
+
|
|
122
|
+
| Function signature | CLI argument |
|
|
123
|
+
|--------------------|---------------|
|
|
124
|
+
| `name: str` | Positional `name` |
|
|
125
|
+
| `count: int = 1` | `--count 1` |
|
|
126
|
+
| `verbose: bool = False` | `--verbose` / `--no-verbose` |
|
|
127
|
+
| `mode: str \| None = None` | `--mode MODE` |
|
|
128
|
+
|
|
129
|
+
### Global arguments (apply to all commands)
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
cli.add_global_argument("--verbose", "-v", action="store_true", help="Verbose output")
|
|
133
|
+
cli.add_global_argument("--config", "-c", type=str, help="Config file path")
|
|
134
|
+
|
|
135
|
+
@cli.command()
|
|
136
|
+
def deploy(environment: str):
|
|
137
|
+
"""Deploy to environment."""
|
|
138
|
+
# Global arguments available in parsed namespace
|
|
139
|
+
pass
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## 🎨 CLI Configuration
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
cli = CLI(
|
|
146
|
+
name="myapp", # Program name (default: None)
|
|
147
|
+
description="Does amazing things", # Description in help (default: None)
|
|
148
|
+
version="2.0.0", # Adds --version flag (default: None)
|
|
149
|
+
)
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
| Option | Description |
|
|
153
|
+
|--------|-------------|
|
|
154
|
+
| `name` | Program name in help (default: `None`) |
|
|
155
|
+
| `description` | Description in help (default: `None`) |
|
|
156
|
+
| `version` | Adds `--version` flag (default: `None`) |
|
|
157
|
+
|
|
158
|
+
## 📄 License & Acknowledgments
|
|
159
|
+
|
|
160
|
+
MIT License — Built with Python standard library:
|
|
161
|
+
|
|
162
|
+
| Module | Purpose |
|
|
163
|
+
|--------|---------|
|
|
164
|
+
| `argparse` | CLI parsing engine |
|
|
165
|
+
| `inspect` | Signature introspection |
|
|
166
|
+
|
|
167
|
+
**Forked from:** [arg-kiss](https://github.com/Fkernel653/arg-kiss) by [Fkernel653](https://github.com/Fkernel653)
|
|
168
|
+
|
|
169
|
+
**argss author:** [Fkernel653](https://github.com/Fkernel653)
|
|
170
|
+
|
|
171
|
+
**Project:** [GitHub](https://github.com/Fkernel653/argss) • [PyPI](https://pypi.org/project/argss/)
|
argss-0.1.0/README.md
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# argss — Stupidly Simple CLI builder (sync-only, no groups)
|
|
2
|
+
|
|
3
|
+
[](https://python.org)
|
|
4
|
+
[](https://pypi.org/project/argss/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[]()
|
|
7
|
+
[](https://docs.astral.sh/ruff/)
|
|
8
|
+
|
|
9
|
+
**argss** is a lightweight fork of [arg-kiss](https://github.com/Fkernel653/arg-kiss) — stripped down to the essentials.
|
|
10
|
+
|
|
11
|
+
Write type-annotated Python functions, get a CLI with argparse's native `--help` — no magic, no bloat. Flat commands only, synchronous execution.
|
|
12
|
+
|
|
13
|
+
## 🎯 Why argss?
|
|
14
|
+
|
|
15
|
+
| Feature | arg-kiss | argss |
|
|
16
|
+
|---------|----------|-------|
|
|
17
|
+
| `@cli.command()` | ✅ | ✅ |
|
|
18
|
+
| Type inference | ✅ | ✅ |
|
|
19
|
+
| Boolean flags | ✅ | ✅ |
|
|
20
|
+
| Global arguments | ✅ | ✅ |
|
|
21
|
+
| Command groups | ✅ | ❌ |
|
|
22
|
+
| Async support | ✅ | ❌ |
|
|
23
|
+
| `color` parameter (Python 3.14+ coloured help) | ✅ | ❌ |
|
|
24
|
+
| Dependencies | none | none |
|
|
25
|
+
| Lines of code | ~150 | ~110 |
|
|
26
|
+
|
|
27
|
+
> **Note:** arg-kiss provides a `color` parameter in `CLI()` that enables/disables coloured `--help` output on Python 3.14+. argss omits this parameter for simplicity.
|
|
28
|
+
|
|
29
|
+
Use **argss** when you want:
|
|
30
|
+
- Minimal code footprint
|
|
31
|
+
- No asyncio overhead
|
|
32
|
+
- Flat command structure (no sub-subcommands)
|
|
33
|
+
- Faster import time (~30% faster than arg-kiss)
|
|
34
|
+
|
|
35
|
+
## 🚀 Quick Start
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install argss
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from argss import CLI
|
|
43
|
+
|
|
44
|
+
cli = CLI(name="todo", description="Task manager")
|
|
45
|
+
|
|
46
|
+
@cli.command()
|
|
47
|
+
def add(task: str, priority: int = 1, done: bool = False):
|
|
48
|
+
"""Add a task."""
|
|
49
|
+
status = "✓" if done else "○"
|
|
50
|
+
print(f"[{status}] {task} (priority: {priority})")
|
|
51
|
+
|
|
52
|
+
@cli.command()
|
|
53
|
+
def list_all():
|
|
54
|
+
"""Show all tasks."""
|
|
55
|
+
print("Nothing yet!")
|
|
56
|
+
|
|
57
|
+
cli.run()
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
$ python todo.py add "Buy milk" --priority 2
|
|
62
|
+
[○] Buy milk (priority: 2)
|
|
63
|
+
|
|
64
|
+
$ python todo.py list-all
|
|
65
|
+
Nothing yet!
|
|
66
|
+
|
|
67
|
+
$ python todo.py --help
|
|
68
|
+
usage: todo [-h] {add,list-all} ...
|
|
69
|
+
|
|
70
|
+
Task manager
|
|
71
|
+
|
|
72
|
+
positional arguments:
|
|
73
|
+
{add,list-all}
|
|
74
|
+
add Add a task.
|
|
75
|
+
list-all Show all tasks.
|
|
76
|
+
|
|
77
|
+
options:
|
|
78
|
+
-h, --help show this help message and exit
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## 📋 Commands & Features
|
|
82
|
+
|
|
83
|
+
### `@cli.command()` — Define commands from functions
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
@cli.command()
|
|
87
|
+
def fetch(url: str, retries: int = 3):
|
|
88
|
+
"""Download from URL with retries"""
|
|
89
|
+
print(f"Fetched {url} (retries: {retries})")
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Type → CLI mapping
|
|
93
|
+
|
|
94
|
+
| Function signature | CLI argument |
|
|
95
|
+
|--------------------|---------------|
|
|
96
|
+
| `name: str` | Positional `name` |
|
|
97
|
+
| `count: int = 1` | `--count 1` |
|
|
98
|
+
| `verbose: bool = False` | `--verbose` / `--no-verbose` |
|
|
99
|
+
| `mode: str \| None = None` | `--mode MODE` |
|
|
100
|
+
|
|
101
|
+
### Global arguments (apply to all commands)
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
cli.add_global_argument("--verbose", "-v", action="store_true", help="Verbose output")
|
|
105
|
+
cli.add_global_argument("--config", "-c", type=str, help="Config file path")
|
|
106
|
+
|
|
107
|
+
@cli.command()
|
|
108
|
+
def deploy(environment: str):
|
|
109
|
+
"""Deploy to environment."""
|
|
110
|
+
# Global arguments available in parsed namespace
|
|
111
|
+
pass
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## 🎨 CLI Configuration
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
cli = CLI(
|
|
118
|
+
name="myapp", # Program name (default: None)
|
|
119
|
+
description="Does amazing things", # Description in help (default: None)
|
|
120
|
+
version="2.0.0", # Adds --version flag (default: None)
|
|
121
|
+
)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
| Option | Description |
|
|
125
|
+
|--------|-------------|
|
|
126
|
+
| `name` | Program name in help (default: `None`) |
|
|
127
|
+
| `description` | Description in help (default: `None`) |
|
|
128
|
+
| `version` | Adds `--version` flag (default: `None`) |
|
|
129
|
+
|
|
130
|
+
## 📄 License & Acknowledgments
|
|
131
|
+
|
|
132
|
+
MIT License — Built with Python standard library:
|
|
133
|
+
|
|
134
|
+
| Module | Purpose |
|
|
135
|
+
|--------|---------|
|
|
136
|
+
| `argparse` | CLI parsing engine |
|
|
137
|
+
| `inspect` | Signature introspection |
|
|
138
|
+
|
|
139
|
+
**Forked from:** [arg-kiss](https://github.com/Fkernel653/arg-kiss) by [Fkernel653](https://github.com/Fkernel653)
|
|
140
|
+
|
|
141
|
+
**argss author:** [Fkernel653](https://github.com/Fkernel653)
|
|
142
|
+
|
|
143
|
+
**Project:** [GitHub](https://github.com/Fkernel653/argss) • [PyPI](https://pypi.org/project/argss/)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""Argument descriptor for CLI commands."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, List
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Argument:
|
|
9
|
+
"""Description of an argument for a command."""
|
|
10
|
+
|
|
11
|
+
def __init__(
|
|
12
|
+
self,
|
|
13
|
+
*flags: str,
|
|
14
|
+
type: type = str,
|
|
15
|
+
default: Any = None,
|
|
16
|
+
help: str = "",
|
|
17
|
+
required: bool = False,
|
|
18
|
+
choices: List[Any] | None = None,
|
|
19
|
+
action: str | None = None,
|
|
20
|
+
):
|
|
21
|
+
"""
|
|
22
|
+
Initialize a command argument descriptor.
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
*flags: Argument flags (e.g., "--output", "-o").
|
|
26
|
+
type: Expected type of the argument value.
|
|
27
|
+
default: Default value if the argument is not provided.
|
|
28
|
+
help: Help text describing the argument.
|
|
29
|
+
required: Whether the argument must be provided.
|
|
30
|
+
choices: List of allowed values for the argument.
|
|
31
|
+
action: Custom argparse action (e.g., "store_true", "store_false").
|
|
32
|
+
"""
|
|
33
|
+
self.flags = flags
|
|
34
|
+
self.type = type
|
|
35
|
+
self.default = default
|
|
36
|
+
self.help = help
|
|
37
|
+
self.required = required
|
|
38
|
+
self.choices = choices
|
|
39
|
+
self.action = action
|
argss-0.1.0/argss/cli.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"""Lightweight CLI builder with decorator-based command registration."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import inspect
|
|
7
|
+
import sys
|
|
8
|
+
from typing import Any, Callable, Dict, List
|
|
9
|
+
|
|
10
|
+
from .argument import Argument
|
|
11
|
+
from .utils import get_type_from_annotation, is_bool_type
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class CLI:
|
|
15
|
+
"""
|
|
16
|
+
Wrapper over argparse for building command-line interfaces with decorator-based command registration.
|
|
17
|
+
|
|
18
|
+
Features:
|
|
19
|
+
- Decorator-based command registration via @cli.command()
|
|
20
|
+
- Automatic argument inference from function signatures
|
|
21
|
+
- Support for boolean flags with --flag/--no-flag patterns
|
|
22
|
+
- Global arguments shared across all commands
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
name: str | None = None,
|
|
28
|
+
description: str | None = None,
|
|
29
|
+
version: str | None = None,
|
|
30
|
+
):
|
|
31
|
+
self.name = name
|
|
32
|
+
self.description = description
|
|
33
|
+
self.version = version
|
|
34
|
+
self._commands: Dict[str, dict] = {}
|
|
35
|
+
|
|
36
|
+
self.parser = argparse.ArgumentParser(prog=name, description=description)
|
|
37
|
+
|
|
38
|
+
self.subparsers = self.parser.add_subparsers(dest="command", title="Commands")
|
|
39
|
+
|
|
40
|
+
if version:
|
|
41
|
+
self.parser.add_argument("--version", action="version", version=version)
|
|
42
|
+
|
|
43
|
+
def add_global_argument(self, *flags: str, **kwargs: Any) -> None:
|
|
44
|
+
"""Add a global argument that applies to all commands."""
|
|
45
|
+
self.parser.add_argument(*flags, **kwargs)
|
|
46
|
+
|
|
47
|
+
def command(
|
|
48
|
+
self,
|
|
49
|
+
name: str | None = None,
|
|
50
|
+
description: str | None = None,
|
|
51
|
+
arguments: List[Argument] | None = None,
|
|
52
|
+
**parser_kwargs: Any,
|
|
53
|
+
) -> Callable:
|
|
54
|
+
"""
|
|
55
|
+
Decorator for creating a CLI command from a function.
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
def decorator(func: Callable) -> Callable:
|
|
59
|
+
cmd_name = name or func.__name__.replace("_", "-")
|
|
60
|
+
cmd_description = description or (func.__doc__ or "").strip()
|
|
61
|
+
|
|
62
|
+
parser = self.subparsers.add_parser(
|
|
63
|
+
cmd_name,
|
|
64
|
+
help=cmd_description.split("\n")[0] if cmd_description else None,
|
|
65
|
+
description=cmd_description,
|
|
66
|
+
**parser_kwargs,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
explicit_dests = set()
|
|
70
|
+
if arguments:
|
|
71
|
+
for arg in arguments:
|
|
72
|
+
kw = {
|
|
73
|
+
k: v
|
|
74
|
+
for k, v in vars(arg).items()
|
|
75
|
+
if k != "flags" and v is not None
|
|
76
|
+
}
|
|
77
|
+
explicit_dests.add(parser.add_argument(*arg.flags, **kw).dest)
|
|
78
|
+
|
|
79
|
+
for param_name, param in inspect.signature(func).parameters.items():
|
|
80
|
+
if param_name in explicit_dests:
|
|
81
|
+
continue
|
|
82
|
+
has_default = param.default is not inspect.Parameter.empty
|
|
83
|
+
|
|
84
|
+
if not has_default:
|
|
85
|
+
parser.add_argument(
|
|
86
|
+
param_name,
|
|
87
|
+
type=get_type_from_annotation(param.annotation, param.default),
|
|
88
|
+
help=param_name,
|
|
89
|
+
)
|
|
90
|
+
elif is_bool_type(param):
|
|
91
|
+
base_flag = param_name.replace("_", "-")
|
|
92
|
+
default_val = (
|
|
93
|
+
param.default
|
|
94
|
+
if param.default is not inspect.Parameter.empty
|
|
95
|
+
else False
|
|
96
|
+
)
|
|
97
|
+
group = parser.add_mutually_exclusive_group()
|
|
98
|
+
group.add_argument(
|
|
99
|
+
f"--{base_flag}",
|
|
100
|
+
action="store_true",
|
|
101
|
+
default=default_val,
|
|
102
|
+
dest=param_name,
|
|
103
|
+
help=f"Enable {param_name}",
|
|
104
|
+
)
|
|
105
|
+
group.add_argument(
|
|
106
|
+
f"--no-{base_flag}",
|
|
107
|
+
action="store_false",
|
|
108
|
+
default=default_val,
|
|
109
|
+
dest=param_name,
|
|
110
|
+
help=f"Disable {param_name}",
|
|
111
|
+
)
|
|
112
|
+
else:
|
|
113
|
+
parser.add_argument(
|
|
114
|
+
f"--{param_name.replace('_', '-')}",
|
|
115
|
+
type=get_type_from_annotation(param.annotation, param.default),
|
|
116
|
+
default=param.default,
|
|
117
|
+
help=f"{param_name} (default: {param.default})",
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
self._commands[cmd_name] = {
|
|
121
|
+
"func": func,
|
|
122
|
+
"parser": parser,
|
|
123
|
+
}
|
|
124
|
+
return func
|
|
125
|
+
|
|
126
|
+
return decorator
|
|
127
|
+
|
|
128
|
+
def run(self, args: List[str] | None = None) -> None:
|
|
129
|
+
"""
|
|
130
|
+
Parse command-line arguments and execute the appropriate command.
|
|
131
|
+
"""
|
|
132
|
+
args = sys.argv[1:] if args is None else args
|
|
133
|
+
namespace = self.parser.parse_args(args)
|
|
134
|
+
|
|
135
|
+
if namespace.command is None:
|
|
136
|
+
self.parser.print_help()
|
|
137
|
+
return
|
|
138
|
+
|
|
139
|
+
command_info = self._commands.get(namespace.command)
|
|
140
|
+
|
|
141
|
+
if command_info is None:
|
|
142
|
+
self.parser.print_help()
|
|
143
|
+
return
|
|
144
|
+
|
|
145
|
+
namespace_dict = vars(namespace)
|
|
146
|
+
func_kwargs = {k: v for k, v in namespace_dict.items() if k != "command"}
|
|
147
|
+
|
|
148
|
+
result = command_info["func"](**func_kwargs)
|
|
149
|
+
|
|
150
|
+
if result is not None:
|
|
151
|
+
sys.stdout.write(str(result) + "\n")
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""Internal utilities for type handling."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import inspect
|
|
6
|
+
from typing import Any, get_args, get_origin
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def get_type_from_annotation(annotation, default: Any = None) -> type:
|
|
10
|
+
"""Extract a usable type from a type annotation."""
|
|
11
|
+
if annotation is inspect.Parameter.empty:
|
|
12
|
+
return type(default) if default is not inspect.Parameter.empty else str
|
|
13
|
+
|
|
14
|
+
if isinstance(annotation, str):
|
|
15
|
+
return type(default) if default is not inspect.Parameter.empty else str
|
|
16
|
+
|
|
17
|
+
if isinstance(annotation, type):
|
|
18
|
+
return annotation
|
|
19
|
+
|
|
20
|
+
origin = get_origin(annotation)
|
|
21
|
+
if origin is not None:
|
|
22
|
+
args = get_args(annotation)
|
|
23
|
+
none_type = type(None)
|
|
24
|
+
non_none = [a for a in args if a is not none_type]
|
|
25
|
+
if non_none:
|
|
26
|
+
return non_none[0] if isinstance(non_none[0], type) else str
|
|
27
|
+
|
|
28
|
+
return str
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def is_bool_type(param: inspect.Parameter) -> bool:
|
|
32
|
+
"""Check if a function parameter represents a boolean flag."""
|
|
33
|
+
annotation = param.annotation
|
|
34
|
+
|
|
35
|
+
if annotation is inspect.Parameter.empty:
|
|
36
|
+
return isinstance(param.default, bool)
|
|
37
|
+
|
|
38
|
+
if isinstance(annotation, str):
|
|
39
|
+
return annotation in {
|
|
40
|
+
"bool",
|
|
41
|
+
"Optional[bool]",
|
|
42
|
+
"Union[bool, None]",
|
|
43
|
+
"Union[bool, NoneType]",
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if annotation is bool:
|
|
47
|
+
return True
|
|
48
|
+
|
|
49
|
+
origin = get_origin(annotation)
|
|
50
|
+
if origin is not None:
|
|
51
|
+
args = get_args(annotation)
|
|
52
|
+
none_type = type(None)
|
|
53
|
+
non_none = [a for a in args if a is not none_type]
|
|
54
|
+
return len(non_none) == 1 and non_none[0] is bool
|
|
55
|
+
|
|
56
|
+
return False
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "argss"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Write type-annotated Python functions, get a CLI with argparse's native `--help` — no magic, no bloat. Flat commands only, synchronous execution"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.14"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "Fkernel653"}
|
|
14
|
+
]
|
|
15
|
+
keywords = ["cli", "argparse", "command-line", "framework", "lightweight"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Programming Language :: Python :: 3.14",
|
|
25
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
26
|
+
"Topic :: Software Development :: User Interfaces",
|
|
27
|
+
"Topic :: Terminals",
|
|
28
|
+
"Topic :: Utilities",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.optional-dependencies]
|
|
32
|
+
dev = ["ruff"]
|
|
33
|
+
|
|
34
|
+
[project.urls]
|
|
35
|
+
Homepage = "https://github.com/Fkernel653/argss"
|
|
36
|
+
Repository = "https://github.com/Fkernel653/argss.git"
|
|
37
|
+
Documentation = "https://github.com/Fkernel653/argss#readme"
|
|
38
|
+
|
|
39
|
+
[tool.hatch.build.targets.wheel]
|
|
40
|
+
packages = ["argss"]
|