yadopt 2024.10.5__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.
- yadopt-2024.10.5/.gitignore +13 -0
- yadopt-2024.10.5/LICENSE +21 -0
- yadopt-2024.10.5/Makefile +54 -0
- yadopt-2024.10.5/PKG-INFO +220 -0
- yadopt-2024.10.5/README.md +183 -0
- yadopt-2024.10.5/examples/00_usage_in_readme.py +27 -0
- yadopt-2024.10.5/examples/01_type_hint.py +47 -0
- yadopt-2024.10.5/examples/02_decorator.py +39 -0
- yadopt-2024.10.5/examples/03_subcommand.py +36 -0
- yadopt-2024.10.5/examples/README.md +24 -0
- yadopt-2024.10.5/figures/yadopt_logo.svg +30 -0
- yadopt-2024.10.5/pyproject.toml +64 -0
- yadopt-2024.10.5/requirements-dev.txt +5 -0
- yadopt-2024.10.5/tests/run_tests.py +58 -0
- yadopt-2024.10.5/tests/testcase01.py +97 -0
- yadopt-2024.10.5/tests/testcase02.py +88 -0
- yadopt-2024.10.5/tests/testcase03.py +97 -0
- yadopt-2024.10.5/tests/testcase04.py +53 -0
- yadopt-2024.10.5/tests/testcase05.py +50 -0
- yadopt-2024.10.5/tests/testcase06.py +50 -0
- yadopt-2024.10.5/tests/testcase07.py +38 -0
- yadopt-2024.10.5/tests/testcase08.py +78 -0
- yadopt-2024.10.5/tests/testcase09.py +78 -0
- yadopt-2024.10.5/tests/testcase_wrap.py +68 -0
- yadopt-2024.10.5/yadopt/__init__.py +15 -0
- yadopt-2024.10.5/yadopt/argopt.py +132 -0
- yadopt-2024.10.5/yadopt/argvec.py +212 -0
- yadopt-2024.10.5/yadopt/checker.py +45 -0
- yadopt-2024.10.5/yadopt/docstr.py +117 -0
- yadopt-2024.10.5/yadopt/errors.py +152 -0
- yadopt-2024.10.5/yadopt/gendat.py +107 -0
- yadopt-2024.10.5/yadopt/usage.py +129 -0
- yadopt-2024.10.5/yadopt/utils.py +145 -0
- yadopt-2024.10.5/yadopt/yadopt.py +135 -0
yadopt-2024.10.5/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Tetsuya Ishikawa
|
|
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.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Makefile
|
|
2
|
+
|
|
3
|
+
SOFTWARE = yadopt
|
|
4
|
+
|
|
5
|
+
help:
|
|
6
|
+
@echo "Usage:"
|
|
7
|
+
@echo " make <command>"
|
|
8
|
+
@echo ""
|
|
9
|
+
@echo "Build commands:"
|
|
10
|
+
@echo " build Build package"
|
|
11
|
+
@echo " testpypi Upload package to TestPyPi"
|
|
12
|
+
@echo " pypi Upload package to PyPi"
|
|
13
|
+
@echo " install-test Install from TestPyPi"
|
|
14
|
+
@echo ""
|
|
15
|
+
@echo "Test and code check commands:"
|
|
16
|
+
@echo " check Check the code quality"
|
|
17
|
+
@echo " count Count the lines of code"
|
|
18
|
+
@echo " coverage Measure code coverage"
|
|
19
|
+
@echo ""
|
|
20
|
+
@echo "Other commands:"
|
|
21
|
+
@echo " clean Cleanup cache files"
|
|
22
|
+
@echo " help Show this message"
|
|
23
|
+
|
|
24
|
+
check:
|
|
25
|
+
pyflakes yadopt/*.py
|
|
26
|
+
pylint yadopt/*.py
|
|
27
|
+
|
|
28
|
+
count:
|
|
29
|
+
cloc --by-file yadopt/*.py
|
|
30
|
+
|
|
31
|
+
coverage:
|
|
32
|
+
rm -rf .coverage
|
|
33
|
+
coverage run --source yadopt tests/run_tests.py
|
|
34
|
+
coverage html
|
|
35
|
+
|
|
36
|
+
test:
|
|
37
|
+
python3 tests/run_tests.py
|
|
38
|
+
|
|
39
|
+
build:
|
|
40
|
+
python3 -m build
|
|
41
|
+
|
|
42
|
+
testpypi:
|
|
43
|
+
twine upload --repository pypitest dist/*
|
|
44
|
+
|
|
45
|
+
pypi:
|
|
46
|
+
twine upload --repository pypi dist/*
|
|
47
|
+
|
|
48
|
+
install-test:
|
|
49
|
+
python3 -m pip install --index-url https://test.pypi.org/simple/ $(SOFTWARE)
|
|
50
|
+
|
|
51
|
+
clean:
|
|
52
|
+
rm -rf dist .coverage htmlcov `find -type d | grep __pycache__`
|
|
53
|
+
|
|
54
|
+
# vim: noexpandtab tabstop=4 shiftwidth=4
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: yadopt
|
|
3
|
+
Version: 2024.10.5
|
|
4
|
+
Summary: Yet another docopt, a human-friendly command line arguments parser.
|
|
5
|
+
Project-URL: Repository, https://github.com/tiskw/yadopt
|
|
6
|
+
Project-URL: Issues, https://github.com/tiskw/yadopt/issues
|
|
7
|
+
Author-email: Tetsuya Ishikawa <tiskw111@gmail.com>
|
|
8
|
+
License: MIT License
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2024 Tetsuya Ishikawa
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Classifier: Development Status :: 3 - Alpha
|
|
31
|
+
Classifier: Intended Audience :: Developers
|
|
32
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
33
|
+
Classifier: Operating System :: OS Independent
|
|
34
|
+
Classifier: Programming Language :: Python :: 3
|
|
35
|
+
Requires-Python: >=3.9
|
|
36
|
+
Description-Content-Type: text/markdown
|
|
37
|
+
|
|
38
|
+
<div align="center">
|
|
39
|
+
<img src="figures/yadopt_logo.svg" width="90%">
|
|
40
|
+
</div>
|
|
41
|
+
|
|
42
|
+
<div align="center">
|
|
43
|
+
<img src="https://img.shields.io/badge/PYTHON-%3E%3D3.9-blue.svg?style=for-the-badge&logo=python&logoColor=white">
|
|
44
|
+
|
|
45
|
+
<img src="https://img.shields.io/badge/LICENSE-MIT-orange.svg?style=for-the-badge">
|
|
46
|
+
|
|
47
|
+
<img src="https://img.shields.io/badge/COVERAGE-96%25-green.svg?style=for-the-badge">
|
|
48
|
+
|
|
49
|
+
<img src="https://img.shields.io/badge/QUALITY-9.94/10.0-yellow.svg?style=for-the-badge">
|
|
50
|
+
</div>
|
|
51
|
+
|
|
52
|
+
YadOpt - Yet another docopt
|
|
53
|
+
================================================================================
|
|
54
|
+
|
|
55
|
+
YadOpt is a Python re-implementation of [docopt](https://github.com/docopt/docopt)
|
|
56
|
+
and [docopt-ng](https://github.com/jazzband/docopt-ng), a human-friendly
|
|
57
|
+
command-line argument parser with type hinting and utility functions.
|
|
58
|
+
YadOpt helps you to create beautiful command-line interfaces, just like docopt
|
|
59
|
+
and docopt-ng, however, **YadOpt also supports date type hinting**.
|
|
60
|
+
|
|
61
|
+
The following is the typical usage of YadOpt:
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
"""
|
|
65
|
+
Usage:
|
|
66
|
+
train.py <config_path> [--epochs INT] [--model STR] [--lr FLT]
|
|
67
|
+
train.py --help
|
|
68
|
+
|
|
69
|
+
Train a neural network model.
|
|
70
|
+
|
|
71
|
+
Arguments:
|
|
72
|
+
config_path Path to config file.
|
|
73
|
+
|
|
74
|
+
Training options:
|
|
75
|
+
--epochs INT The number of training epochs. [default: 100]
|
|
76
|
+
--model STR Neural network model name. [default: mlp]
|
|
77
|
+
--lr FLT Learning rate. [default: 1.0E-3]
|
|
78
|
+
|
|
79
|
+
Other options:
|
|
80
|
+
-h, --help Show this help message and exit.
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
import yadopt
|
|
84
|
+
|
|
85
|
+
if __name__ == "__main__":
|
|
86
|
+
args = yadopt.parse(__doc__)
|
|
87
|
+
print(args)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Please save the above code as `sample.py`, and run it as follows:
|
|
91
|
+
|
|
92
|
+
```console
|
|
93
|
+
$ python3 sample.py config.toml --epochs 10 --model=cnn
|
|
94
|
+
YadOptArgs(config_path=config.toml, epochs=10, model=cnn, lr=0.001, help=False)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
In the above code, the parsed command-line arguments are stored in the `arg`
|
|
98
|
+
and you can access each argument using dot notation, like `arg.config_path`.
|
|
99
|
+
Also, the parsed command-line arguments are typed, in other words,
|
|
100
|
+
the `arg` variable satisfies the following assertions:
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
assert isinstance(args.config_path, pathlib.Path)
|
|
104
|
+
assert isinstance(args.epochs, int)
|
|
105
|
+
assert isinstance(args.model, str)
|
|
106
|
+
assert isinstance(args.lr, float)
|
|
107
|
+
assert isinstance(args.help, bool)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
More realistic examples can be found in the [examples](./examples/README.md)
|
|
111
|
+
directory.
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
Installation
|
|
115
|
+
--------------------------------------------------------------------------------
|
|
116
|
+
|
|
117
|
+
Please install from [pip](https://pip.pypa.io/en/stable/).
|
|
118
|
+
|
|
119
|
+
```console
|
|
120
|
+
$ pip install yadopt
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
Usage
|
|
125
|
+
--------------------------------------------------------------------------------
|
|
126
|
+
|
|
127
|
+
### Use `parse` function
|
|
128
|
+
|
|
129
|
+
The `yadopt.parse` function allows you to parse command-line arguments based on
|
|
130
|
+
your docstring. The function is designed to parse `sys.argv` by default, but
|
|
131
|
+
you can explicitly specify the argument vector by using the second argument
|
|
132
|
+
of the function, just like as follows:
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
# Parse sys.argv
|
|
136
|
+
args = yadopt.parse(__doc__)
|
|
137
|
+
|
|
138
|
+
# Parse the given argv.
|
|
139
|
+
args = yadopt.parse(__doc__, argv)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Use `wrap` function
|
|
143
|
+
|
|
144
|
+
YadOpt supports the decorator approach for command-line parsing by the decorator
|
|
145
|
+
`@yadopt.wrap` which takes the same arguments as the function `yadopt.parse`.
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
@yadopt.wrap(__doc__)
|
|
149
|
+
def main(args: yadopt.YadOptArgs, real_arg: str):
|
|
150
|
+
...
|
|
151
|
+
|
|
152
|
+
if __name__ == "__main__":
|
|
153
|
+
main("real argument")
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Dictionary and namedtuple support
|
|
157
|
+
|
|
158
|
+
The returned value of `yadopt.parse` is an instance of `YadOptArgs` that is
|
|
159
|
+
a normal mutable Python class. However, sometimes a dictionary that has
|
|
160
|
+
the `get` accessor, or an immutable namedtuple, may be preferable.
|
|
161
|
+
In that case, please try `.to_dict` and `.to_namedtuple` functions.
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
# Convert the returned value to dictionary.
|
|
165
|
+
args = yadopt.parse(__doc__).to_dict()
|
|
166
|
+
|
|
167
|
+
# Convert the returned value to namedtuple.
|
|
168
|
+
args = yadopt.parse(__doc__).to_namedtuple()
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
API
|
|
173
|
+
--------------------------------------------------------------------------------
|
|
174
|
+
|
|
175
|
+
### `yadopt.parse`
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
yadopt.parse(
|
|
179
|
+
docstr: str,
|
|
180
|
+
argv: list[str] = None,
|
|
181
|
+
default_type: str = "auto",
|
|
182
|
+
force_continue: bool = False,
|
|
183
|
+
) -> YadOptArgs
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Args
|
|
187
|
+
|
|
188
|
+
| Name | Type | Default value | Description |
|
|
189
|
+
|:-----------------|:-----------:|:-------------:|:------------|
|
|
190
|
+
| `docstr` | `str` | - | A help message string that will be parsed to create an object of command line arguments. We recommend to write a help message in the docstring of your Python script and use `__doc__` here. |
|
|
191
|
+
| `argv` | `list[str]` | `None` | An argument vector to be parsed. YadOpt uses the command line arguments passed to your Python script, `sys.argv[1:]`, by default. |
|
|
192
|
+
| `default_type` | `str\|type` | `"auto"` | Default data type of arguments and options. The default value `"auto"` means automatic determination. |
|
|
193
|
+
| `force_continue` | `bool` | `False` | If `True`, do not exit the software regardless of whether YadOpt succeeds command line parsing or not. |
|
|
194
|
+
|
|
195
|
+
### Returns
|
|
196
|
+
|
|
197
|
+
The returned value is an instance of the `YadOptArgs` class that represents parsed
|
|
198
|
+
command line arguments. The `YadOptArgs` class is a normal mutable Python
|
|
199
|
+
class and users can access to parsed command line arguments by the dot notation.
|
|
200
|
+
If you wish to convert `YadOptArgs` to dictionary type, please use `.to_dict()`
|
|
201
|
+
function. Likewise, if you prefer an immutable data type, please try
|
|
202
|
+
`.to_namedtuple()` function.
|
|
203
|
+
|
|
204
|
+
### `yadopt.wrap`
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
yadopt.wrap(*pargs, **kwargs) -> Callable
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Args
|
|
211
|
+
|
|
212
|
+
The same as the arguments of `yadopt.parse` function.
|
|
213
|
+
|
|
214
|
+
### Returns
|
|
215
|
+
|
|
216
|
+
The `yadopt.wrap` is a Python decorator function that allows users to modify
|
|
217
|
+
the behavior of functions or methods, therefore the returned value of this
|
|
218
|
+
function is a callable object. The first argument of the target function of
|
|
219
|
+
this decorator is curried by the result of `yadopt.parse` and the curried
|
|
220
|
+
object will be returned.
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="figures/yadopt_logo.svg" width="90%">
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
<img src="https://img.shields.io/badge/PYTHON-%3E%3D3.9-blue.svg?style=for-the-badge&logo=python&logoColor=white">
|
|
7
|
+
|
|
8
|
+
<img src="https://img.shields.io/badge/LICENSE-MIT-orange.svg?style=for-the-badge">
|
|
9
|
+
|
|
10
|
+
<img src="https://img.shields.io/badge/COVERAGE-96%25-green.svg?style=for-the-badge">
|
|
11
|
+
|
|
12
|
+
<img src="https://img.shields.io/badge/QUALITY-9.94/10.0-yellow.svg?style=for-the-badge">
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
YadOpt - Yet another docopt
|
|
16
|
+
================================================================================
|
|
17
|
+
|
|
18
|
+
YadOpt is a Python re-implementation of [docopt](https://github.com/docopt/docopt)
|
|
19
|
+
and [docopt-ng](https://github.com/jazzband/docopt-ng), a human-friendly
|
|
20
|
+
command-line argument parser with type hinting and utility functions.
|
|
21
|
+
YadOpt helps you to create beautiful command-line interfaces, just like docopt
|
|
22
|
+
and docopt-ng, however, **YadOpt also supports date type hinting**.
|
|
23
|
+
|
|
24
|
+
The following is the typical usage of YadOpt:
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
"""
|
|
28
|
+
Usage:
|
|
29
|
+
train.py <config_path> [--epochs INT] [--model STR] [--lr FLT]
|
|
30
|
+
train.py --help
|
|
31
|
+
|
|
32
|
+
Train a neural network model.
|
|
33
|
+
|
|
34
|
+
Arguments:
|
|
35
|
+
config_path Path to config file.
|
|
36
|
+
|
|
37
|
+
Training options:
|
|
38
|
+
--epochs INT The number of training epochs. [default: 100]
|
|
39
|
+
--model STR Neural network model name. [default: mlp]
|
|
40
|
+
--lr FLT Learning rate. [default: 1.0E-3]
|
|
41
|
+
|
|
42
|
+
Other options:
|
|
43
|
+
-h, --help Show this help message and exit.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
import yadopt
|
|
47
|
+
|
|
48
|
+
if __name__ == "__main__":
|
|
49
|
+
args = yadopt.parse(__doc__)
|
|
50
|
+
print(args)
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Please save the above code as `sample.py`, and run it as follows:
|
|
54
|
+
|
|
55
|
+
```console
|
|
56
|
+
$ python3 sample.py config.toml --epochs 10 --model=cnn
|
|
57
|
+
YadOptArgs(config_path=config.toml, epochs=10, model=cnn, lr=0.001, help=False)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
In the above code, the parsed command-line arguments are stored in the `arg`
|
|
61
|
+
and you can access each argument using dot notation, like `arg.config_path`.
|
|
62
|
+
Also, the parsed command-line arguments are typed, in other words,
|
|
63
|
+
the `arg` variable satisfies the following assertions:
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
assert isinstance(args.config_path, pathlib.Path)
|
|
67
|
+
assert isinstance(args.epochs, int)
|
|
68
|
+
assert isinstance(args.model, str)
|
|
69
|
+
assert isinstance(args.lr, float)
|
|
70
|
+
assert isinstance(args.help, bool)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
More realistic examples can be found in the [examples](./examples/README.md)
|
|
74
|
+
directory.
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
Installation
|
|
78
|
+
--------------------------------------------------------------------------------
|
|
79
|
+
|
|
80
|
+
Please install from [pip](https://pip.pypa.io/en/stable/).
|
|
81
|
+
|
|
82
|
+
```console
|
|
83
|
+
$ pip install yadopt
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
Usage
|
|
88
|
+
--------------------------------------------------------------------------------
|
|
89
|
+
|
|
90
|
+
### Use `parse` function
|
|
91
|
+
|
|
92
|
+
The `yadopt.parse` function allows you to parse command-line arguments based on
|
|
93
|
+
your docstring. The function is designed to parse `sys.argv` by default, but
|
|
94
|
+
you can explicitly specify the argument vector by using the second argument
|
|
95
|
+
of the function, just like as follows:
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
# Parse sys.argv
|
|
99
|
+
args = yadopt.parse(__doc__)
|
|
100
|
+
|
|
101
|
+
# Parse the given argv.
|
|
102
|
+
args = yadopt.parse(__doc__, argv)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Use `wrap` function
|
|
106
|
+
|
|
107
|
+
YadOpt supports the decorator approach for command-line parsing by the decorator
|
|
108
|
+
`@yadopt.wrap` which takes the same arguments as the function `yadopt.parse`.
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
@yadopt.wrap(__doc__)
|
|
112
|
+
def main(args: yadopt.YadOptArgs, real_arg: str):
|
|
113
|
+
...
|
|
114
|
+
|
|
115
|
+
if __name__ == "__main__":
|
|
116
|
+
main("real argument")
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Dictionary and namedtuple support
|
|
120
|
+
|
|
121
|
+
The returned value of `yadopt.parse` is an instance of `YadOptArgs` that is
|
|
122
|
+
a normal mutable Python class. However, sometimes a dictionary that has
|
|
123
|
+
the `get` accessor, or an immutable namedtuple, may be preferable.
|
|
124
|
+
In that case, please try `.to_dict` and `.to_namedtuple` functions.
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
# Convert the returned value to dictionary.
|
|
128
|
+
args = yadopt.parse(__doc__).to_dict()
|
|
129
|
+
|
|
130
|
+
# Convert the returned value to namedtuple.
|
|
131
|
+
args = yadopt.parse(__doc__).to_namedtuple()
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
API
|
|
136
|
+
--------------------------------------------------------------------------------
|
|
137
|
+
|
|
138
|
+
### `yadopt.parse`
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
yadopt.parse(
|
|
142
|
+
docstr: str,
|
|
143
|
+
argv: list[str] = None,
|
|
144
|
+
default_type: str = "auto",
|
|
145
|
+
force_continue: bool = False,
|
|
146
|
+
) -> YadOptArgs
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Args
|
|
150
|
+
|
|
151
|
+
| Name | Type | Default value | Description |
|
|
152
|
+
|:-----------------|:-----------:|:-------------:|:------------|
|
|
153
|
+
| `docstr` | `str` | - | A help message string that will be parsed to create an object of command line arguments. We recommend to write a help message in the docstring of your Python script and use `__doc__` here. |
|
|
154
|
+
| `argv` | `list[str]` | `None` | An argument vector to be parsed. YadOpt uses the command line arguments passed to your Python script, `sys.argv[1:]`, by default. |
|
|
155
|
+
| `default_type` | `str\|type` | `"auto"` | Default data type of arguments and options. The default value `"auto"` means automatic determination. |
|
|
156
|
+
| `force_continue` | `bool` | `False` | If `True`, do not exit the software regardless of whether YadOpt succeeds command line parsing or not. |
|
|
157
|
+
|
|
158
|
+
### Returns
|
|
159
|
+
|
|
160
|
+
The returned value is an instance of the `YadOptArgs` class that represents parsed
|
|
161
|
+
command line arguments. The `YadOptArgs` class is a normal mutable Python
|
|
162
|
+
class and users can access to parsed command line arguments by the dot notation.
|
|
163
|
+
If you wish to convert `YadOptArgs` to dictionary type, please use `.to_dict()`
|
|
164
|
+
function. Likewise, if you prefer an immutable data type, please try
|
|
165
|
+
`.to_namedtuple()` function.
|
|
166
|
+
|
|
167
|
+
### `yadopt.wrap`
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
yadopt.wrap(*pargs, **kwargs) -> Callable
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Args
|
|
174
|
+
|
|
175
|
+
The same as the arguments of `yadopt.parse` function.
|
|
176
|
+
|
|
177
|
+
### Returns
|
|
178
|
+
|
|
179
|
+
The `yadopt.wrap` is a Python decorator function that allows users to modify
|
|
180
|
+
the behavior of functions or methods, therefore the returned value of this
|
|
181
|
+
function is a callable object. The first argument of the target function of
|
|
182
|
+
this decorator is curried by the result of `yadopt.parse` and the curried
|
|
183
|
+
object will be returned.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Usage:
|
|
3
|
+
train.py <config_path> [--epochs INT] [--model STR] [--lr FLT]
|
|
4
|
+
train.py --help
|
|
5
|
+
|
|
6
|
+
Train a neural network model.
|
|
7
|
+
|
|
8
|
+
Arguments:
|
|
9
|
+
config_path Path to config file.
|
|
10
|
+
|
|
11
|
+
Training options:
|
|
12
|
+
--epochs INT The number of training epochs. [default: 100]
|
|
13
|
+
--model STR Neural network model name. [default: mlp]
|
|
14
|
+
--lr FLT Learning rate. [default: 1.0E-3]
|
|
15
|
+
|
|
16
|
+
Other options:
|
|
17
|
+
-h, --help Show this help message and exit.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import yadopt
|
|
21
|
+
|
|
22
|
+
if __name__ == "__main__":
|
|
23
|
+
argv = ["train.py", "config.toml", "--epochs", "10", "--model=cnn"]
|
|
24
|
+
args = yadopt.parse(__doc__, argv)
|
|
25
|
+
print(args)
|
|
26
|
+
|
|
27
|
+
# vim: expandtab tabstop=4 shiftwidth=4 fdm=marker
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Usage:
|
|
3
|
+
01_type_hint.py <config_path> [--epochs INT] [--model STR] [--lr FLT]
|
|
4
|
+
01_type_hint.py --help
|
|
5
|
+
|
|
6
|
+
Train a neural network model.
|
|
7
|
+
|
|
8
|
+
Arguments:
|
|
9
|
+
config_path Path to config file.
|
|
10
|
+
|
|
11
|
+
Training options:
|
|
12
|
+
--epochs INT The number of training epochs. [default: 100]
|
|
13
|
+
--model STR Neural network model name. [default: mlp]
|
|
14
|
+
--lr FLT Learning rate. [default: 1.0E-3]
|
|
15
|
+
|
|
16
|
+
Other options:
|
|
17
|
+
-h, --help Show this help message and exit.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
# Import standard libraries.
|
|
21
|
+
import pathlib
|
|
22
|
+
|
|
23
|
+
# Import YadOpt.
|
|
24
|
+
import yadopt
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def main():
|
|
28
|
+
"""
|
|
29
|
+
Entry point of this script.
|
|
30
|
+
"""
|
|
31
|
+
# Parse command line arguments using YadOpt.
|
|
32
|
+
args = yadopt.parse(__doc__)
|
|
33
|
+
print(args)
|
|
34
|
+
|
|
35
|
+
# Data type assertions.
|
|
36
|
+
assert isinstance(args.config_path, pathlib.Path)
|
|
37
|
+
assert isinstance(args.epochs, int)
|
|
38
|
+
assert isinstance(args.model, str)
|
|
39
|
+
assert isinstance(args.lr, float)
|
|
40
|
+
assert isinstance(args.help, bool)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
if __name__ == "__main__":
|
|
44
|
+
main()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# vim: expandtab tabstop=4 shiftwidth=4 fdm=marker
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Usage:
|
|
3
|
+
02_decorator.py <config_path> [--epochs INT] [--model STR] [--lr FLT]
|
|
4
|
+
02_decorator.py --help
|
|
5
|
+
|
|
6
|
+
Train a neural network model.
|
|
7
|
+
|
|
8
|
+
Arguments:
|
|
9
|
+
config_path Path to config file.
|
|
10
|
+
|
|
11
|
+
Training options:
|
|
12
|
+
--epochs INT The number of training epochs. [default: 100]
|
|
13
|
+
--model STR Neural network model name. [default: mlp]
|
|
14
|
+
--lr FLT Learning rate. [default: 1.0E-3]
|
|
15
|
+
|
|
16
|
+
Other options:
|
|
17
|
+
-h, --help Show this help message and exit.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
# Import standard libraries.
|
|
21
|
+
import pathlib
|
|
22
|
+
|
|
23
|
+
# Import YadOpt.
|
|
24
|
+
import yadopt
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@yadopt.wrap(__doc__)
|
|
28
|
+
def main(args: yadopt.YadOptArgs):
|
|
29
|
+
"""
|
|
30
|
+
Entry point of this script.
|
|
31
|
+
"""
|
|
32
|
+
print(args)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
if __name__ == "__main__":
|
|
36
|
+
main()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# vim: expandtab tabstop=4 shiftwidth=4 fdm=marker
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Usage:
|
|
3
|
+
03_subcommand.py foo [--foo_opt INT]
|
|
4
|
+
03_subcommand.py bar [--bar_opt INT]
|
|
5
|
+
03_subcommand.py --help
|
|
6
|
+
|
|
7
|
+
Test of subcommand in YadOpt.
|
|
8
|
+
|
|
9
|
+
Options:
|
|
10
|
+
--foo_opt INT Options of subcommand foo. [default: 0]
|
|
11
|
+
--bar_opt STR Options of subcommand bar. [default: 0]
|
|
12
|
+
|
|
13
|
+
Other options:
|
|
14
|
+
-h, --help Show this help message and exit.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
# Import standard libraries.
|
|
18
|
+
import pathlib
|
|
19
|
+
|
|
20
|
+
# Import YadOpt.
|
|
21
|
+
import yadopt
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def main():
|
|
25
|
+
"""
|
|
26
|
+
Entry point of this script.
|
|
27
|
+
"""
|
|
28
|
+
args = yadopt.parse(__doc__)
|
|
29
|
+
print(args)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
if __name__ == "__main__":
|
|
33
|
+
main()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# vim: expandtab tabstop=4 shiftwidth=4 fdm=marker
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Examples
|
|
2
|
+
================================================================================
|
|
3
|
+
|
|
4
|
+
### 01_type_hint.py
|
|
5
|
+
|
|
6
|
+
```console
|
|
7
|
+
$ python3 01_type_hint.py config.toml --epochs 10 --model cnn --lr 0.01
|
|
8
|
+
YadOptArgs(config_path=config.toml, epochs=10, model=cnn, lr=0.01, help=False)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### 02_decorator.py
|
|
12
|
+
|
|
13
|
+
```console
|
|
14
|
+
$ python3 02_decorator.py config.toml --epochs 10 --model cnn --lr 0.01
|
|
15
|
+
YadOptArgs(config_path=config.toml, epochs=10, model=cnn, lr=0.01, help=False)
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### 03_sub_command.py
|
|
19
|
+
|
|
20
|
+
```console
|
|
21
|
+
$ python3 03_subcommand.py foo --foo_opt 10
|
|
22
|
+
YadOptArgs(foo_opt=0, bar_opt=0, help=False, foo=True)
|
|
23
|
+
```
|
|
24
|
+
|