Arg-Parser-Python
This package helps to parse args from the command line or the terminal to a dictionary and calls registered functions when the individual command is given. This makes dealing with args much easier, more efficient and better ordered.
Why to use it?
When you want to get the args with sys.argv they have to be in the correct position and if one is missing the script crashes. Executing a script working with sys.argv often looks like this:
py .\script.py get name.
If you integrate this script the args are registered like this and much easier to read out.
py .\script.py --get -n name As you can see this is variable and could also be called like this ... -n name --get. Values can be accessed with args["-argname"]
Integration:
Try to install this script with pip install arg_parser and then from arg_parser import * at the top of your script.
If this doesn't work you can download it from https://github.com/NightKylo/Python-Arg-Parser. After that put it into the same directory as your script. In your script add from arg_parser import * at the top.
Be aware that the better way is to install it via pip.
Usage:
To use the parser you have to initialize the Parser class via Parser(commands) where commands is a register-object created like that:
reg = Register(). You can register a command with reg + Command("--name", "description", ["-required_param"], ["-optional_param"]) and a parameter with reg + Parameter("-name", "description"). If you want you can use reg.add(Parameter("-name", "description")) instead of +. If there are no required or optional params remove the lists or put None.
Now you need to specify how to handle a command. It works like this: @parser("--command_name") def handle_command_name(args: dict): do_whatever_you_want(). In the decorator above the function you have to pass the command eg --get and the parser will call the function when the command is supplied.
args is in this case a dictionary of the given args form the structure { "-optname": "value", "-optname": "value" }. Be aware that this dictionary does not contain the given command, only the options.
If you want to start the handle process manually you can add a True to the initialization of the parser and a parser.handle_commands() when you want to handle them. By default, the parser handles the commands when all handler-functions are given.
Look at the example files at https://github.com/NightKylo/Python-Arg-Parser for more detailed information.
How does it work
If you want to know, how this package works go to https://github.com/NightKylo/Python-Arg-Parser/tree/main/arg_parser and look at the parser.py file.
Metadata
Release files for arg-parser 1.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| arg_parser-1.0.5.tar.gz | 8.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| arg_parser-1.0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.2 kB
Release files / arg_parser-1.0.5.tar.gz
| Download URL | arg_parser-1.0.5.tar.gz |
|---|---|
| Size | 8.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
42d4d1413c9c2a2b92ceb869678113827cd3002c9491bc2b9c6c72565dea5033
|
|
BLAKE2b-256 checksum How to use checksums |
a1070c70f7366d4cee342a5385785a3252115fa8a164453a1a51f2a87454ba81
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5
|
Release files / arg_parser-1.0.5-py3-none-any.whl
| Download URL | arg_parser-1.0.5-py3-none-any.whl |
|---|---|
| Size | 10.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a195395833b63a284c25af1792e55a8ed5c3eb7e1adf6509c3e01c6014153144
|
|
BLAKE2b-256 checksum How to use checksums |
806be2a58b936bb7f2c278503930bcf295e2e5e4d2dbbef515f338e92b396a7e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5
|