Skip to main content

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

arg_parser-1.0.4.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

arg_parser-1.0.4-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file arg_parser-1.0.4.tar.gz.

File metadata

  • Download URL: arg_parser-1.0.4.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.0

File hashes

Hashes for arg_parser-1.0.4.tar.gz
Algorithm Hash digest
SHA256 7f232b730e6bbd5b4c9bcb4bf55a167b6e117cc39e0a102baed4f21efd546adb
MD5 1426d722519dcf4d824a01fff2142641
BLAKE2b-256 cc07002175445e87f022f0cccea5020ba321be070745a90cf77386092bde8fa1

See more details on using hashes here.

File details

Details for the file arg_parser-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: arg_parser-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.0

File hashes

Hashes for arg_parser-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 84e3df818823d837391e29639b45eee4e2c7d7a02e113d37864432b9d55ba2df
MD5 863cc9003b9b4d4118df29f4e970d96c
BLAKE2b-256 01eb8b56bfa3b353d900cf5a6c9dc89df1640628b04009fd55901eeed76e0f2a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page