~dricottone/gap

ref: 5369ed747ff966fab12b3922273392a429919d96 gap/README.md -rw-r--r-- 1.6 KiB
5369ed74Dominic Ricottone Minor cleanup 5 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# generated argument parser

A package that uses external configuration files to generate a static,
standalone parser module.


## Usage

To install, try:

```
pipx install git+https://git.dominic-ricottone.com/~dricottone/gap
```


## To-Do

Python 3.11 introduces a `tomllib` module. **Release 1.0.2** will continue to
work perfectly for Python 3.6 or later. **Release 1.0.3** will continue to
function perfectly as well, but the test suite will fail with Python 3.10 or
earlier due to `mypy` not having type stubs for `tomllib`. **Release 1.0.4**
will drop `toml` as a dependency and therefore drop support for Python 3.10 or
earlier.


## Workflow

Given a configuration like:

```toml
# Quickly define '--help', '-h', and '-x'
[help]
number = 0
alternatives = ['h', 'x']

# Immediately raise an error if too few values given.
[range]
number = 2

# Take variable numbers of values, only raising an error at the minimum.
# Will greedily consume arguments up to the maximum.
[files]
minimum = 1
maximum = 9
```

You could include the following in your build process:

```shell
python -m gap my-project-cli.conf > cli.py
```

Your argument parser then is accessible in Python like:

```python
import sys, cli

options, positionals = cli.main(sys.argv[1:])

if "help" in options.keys():
	print_help_message()

# It's just a built-in dict, so use all the usual methods as you please.
for file in options.get("files", []):
	read_input(file)
else:
	for argument in positionals:
		read_input(file)
```


## Licensing

This software is distributed under the GPL license.

I claim absolutely no license over any code generated *by* this software.