A helper for type-checked command-line argument store
Project description
init_attrs_with_kwargs
A helper for type-checked command-line argument store.
init_args_with_kwargs
enables to store the command line arguments (positioned arguments and options) parsed with docopt
in an object of a typed class.
Sample
from init_attrs_with_kwargs import InitAttrsWKwArgs
class MyArgs(InitAttrsWKwArgs):
count: int
name: str
max_length: int
# Initialize from docopt's return value, with casting str to int
args: MyArgs = MyArgs(_cast_str_values=True, **{'<count>': '1', '--name': "Joe", '--max-length': '100'})
print("args.count=%s" % repr(args.count))
print("args.name=%s" % repr(args.name))
print("args.max_length=%s" % repr(args.max_length))
Another samples:
- sample1.py. Describes data type conversion and error handling.
- sample2.py. Storing
docopt
-parsed command line arguments in a type-hinted class's object.
Motivation
I like to use docopt to design command lines. With docopt, I can design a command line in a vague state at first, and then I can make it more and more concrete and detailed.
As a recent trend in Python coding, you can also use type hints for code completion in text editors such as VSCode. Here's the problem: since the result of docopt parsing is a dict, you will be accessing, for example, args["name"], and the editor's completion will not work. Similarly, you cannot benefit from static type checking with type hints.
This issue can be solved by converting a docopt dict into an object of a typed class.
init_attrs_with_kwargs
is a helper just for this purpose.
Links
-
(PyPI page of)
init_attrs_with_kwargs
https://pypi.org/project/init-attrs-with-kwargs/ -
type-docopt
, which introduces type declaration, and holds arguments/options in dict. https://pypi.org/project/type-docopt/
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for init_attrs_with_kwargs-0.1.8.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52ffdbdaf99a543a6d71341737fa56899a5a88bf4a8a26c849280752f5a0ea83 |
|
MD5 | f27f87f8a8a818cf198c32c331ff17f3 |
|
BLAKE2b-256 | 9cfec87f832608344a213f08ddf8606dad966702f7d980e52a4af3382b099c13 |
Hashes for init_attrs_with_kwargs-0.1.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1ebe78c70b4255296f2bd4c271e39206c6a6e6896aff8c8299420ae4a74b8f9 |
|
MD5 | 449dd0ac1a0797c475f2d40f8ea84fe9 |
|
BLAKE2b-256 | 75900dc72a01dce2ada5c9b54507ceb07de8e91d07c97498c2f705b028bdb249 |