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 dictionary, 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 dictionary into an object of a typed class.
init_attrs_with_kwargs
is a helper just for this purpose.
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.6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80f0ede48056323afd090a319c628e552ad9c2a7aefb7cc53c2edf584ef4b5d8 |
|
MD5 | 24df7e63895d47f199c77e0e94c8550c |
|
BLAKE2b-256 | 2080da9cd5176e0531e86f2f57769f90ce0d7d602df26c9e4a5863156f091ba8 |
Hashes for init_attrs_with_kwargs-0.1.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce34f4bb094f7c1f6fbb4dc8d25b7ec2da8770e9b0b6001197ae9b4aeaa1a2db |
|
MD5 | 0e18c8c3cd1d6c7eabb9e56147d17c88 |
|
BLAKE2b-256 | d6a7362ddb39379eb0f3c4178f7af63c2fc4f902311e691d55fd182a2c7fca9c |