A tool for parsing arguments from the command line to a dictionary and for executing predefined on_command functions.
Project description
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.
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
File details
Details for the file arg_parser-1.0.5.tar.gz
.
File metadata
- Download URL: arg_parser-1.0.5.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42d4d1413c9c2a2b92ceb869678113827cd3002c9491bc2b9c6c72565dea5033 |
|
MD5 | ebd9d842d62880ae0ca95260c53087f8 |
|
BLAKE2b-256 | a1070c70f7366d4cee342a5385785a3252115fa8a164453a1a51f2a87454ba81 |
File details
Details for the file arg_parser-1.0.5-py3-none-any.whl
.
File metadata
- Download URL: arg_parser-1.0.5-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a195395833b63a284c25af1792e55a8ed5c3eb7e1adf6509c3e01c6014153144 |
|
MD5 | ee3e87863a8b29e15a4165b5f9edbe30 |
|
BLAKE2b-256 | 806be2a58b936bb7f2c278503930bcf295e2e5e4d2dbbef515f338e92b396a7e |