Module to assist in defining application options and collecting user input for them from command line and environment variables.
Project description
anoptions
A python3 module to assist in defining application options and collecting user input for them from command line and environment variables.
Install
pip3 install anoptions
Usage
Follow the example. If short_name is omitted, the first letter of the "long" name parameter is used. Duplicate short or long names are not allowed.
Func is a required parameter. Give a function that converts the inputted value to the proper data type. If no conversion is desired, you can use Parameter.dummy
.
For flags where existence of a variable will always mark True use Parameter.flag
.
For flags where a more logical input parsing (for example silent='false' to be interpreted as False) is desired use Parameter.bool
(or just bool
).
Example
Code (main.py):
from anoptions import Parameter, Options
def main(argv):
parameters = [
Parameter("host", str, "mqtt_host"),
Parameter("port", int, "mqtt_port"),
Parameter("topic", str, "mqtt_topic"),
Parameter("dir", str, "directory"),
Parameter("delta", int, "delta", short_name='D'),
Parameter("silent", bool, "silent")
]
opt = Options(parameters, argv, 'appname')
d = opt.eval()
print(d)
if __name__ == "__main__":
main(sys.argv[1:])
Run:
APPNAME_PORT=1232 APPNAME_SILENT=1 python3 main.py -d /tmp --host 10.1.2.3 -D 60
{'mqtt_port': 1232, 'silent': True, 'filename_dir': '/tmp', 'mqtt_host': '10.1.2.3', 'delta': 60}
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 anoptions-1.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 565c25173904dc8ef357d8772c655cd2659be5b884d04a0143149cc257c9131d |
|
MD5 | 6bf454916ac91c4270b5fd143d134ca9 |
|
BLAKE2b-256 | f149a15ef88882badd7fb8ef7bd5a700998b7a23c7159cda499fb6c50679065b |