________ ___ ___
/ __/_ / / _ | _______ _/ _ \___ ________ ___ ____
/ _/ / /_/ __ |/ __/ _ `/ ___/ _ `/ __(_-</ -_) __/
/___/ /___/_/ |_/_/ \_, /_/ \_,_/_/ /___/\__/_/ .py
/___/
Never parse arguments again! (At least in python...)
$ git clone https://github.com/samulieronen/ez-argparser.git
# in your .py file
from ezargparser import ArgParser
Parser = ArgParser(arguments=args, argSchema=argSchema)
You need to provide a schema for your arguments.
Example:
ArgSchema = {
'filename': {'type': str},
'i_am_a_key': {'type': str, 'count': 2},
'i_can_be_anything': {'type': str, 'count': 1},
'random_number': {'type': int}
}
Each schema 'key' has to be unique, but they can be named however you want.
{'type': str, 'count': 2}
type
specifies the type you want the input to be. Defaults to string if not provided.
Optional: count
spcifies the number of arguments related to this key. Defaults to 1 if not provided
'count': 'all'
will take the all the remaining arguments and store them to the key.
Fetching arguments with a key:
Parser.fetchArg('filename')
# if count was > 1, returns a list of the arguments related to this specific key.
# else, returns the single argument
# returns None if key was not found
Fetching all arguments:
Parser.fetchAllArgs()
## returns a list of all arguments
Using EZ ArgParser with options
from ezargparser import ArgParser
Parser = ArgParser(arguments=args, argumentSchema=argSchema, options=True, optionSchema=optSchema)
As with arguments, if your program takes options, you need to specify a option schema.
OptSchema = {
'v': {'verbose': 'verbose'},
'l': {'verbose': 'long'},
'e': {'verbose': 'errors', 'params': {
'path': {'type': str},
'3_numbers': {'type': int, 'count': 3}
}}
}
This option schema specifies the following options:
- -v
- -l
- -e
Option bundling, f.ex. -vle NOT supported at the moment.
Name schema keys WITHOUT leading hyphens! '-'
Each schema key MUST be a valid option. Name them wisely.
If a option has a long version, you can specify it to the verbose
field.
If your option takes parameters:
'e': {'verbose': 'errors', 'params': {
'path': {'type': str},
'3_numbers': {'type': int, 'count': 3}
}}
Like with arguments,
type
specifies the type you want the parameter to be. Defaults to string if not provided.
Optional: count
spcifies the number of parameters related to this key.
- Note that
'count': 'all'
can NOT be used when dealing with option parameters (for now).
Check if option is active:
Parser.checkOption('v')
# returns True if active, False if not active or nonexistent in schema
Fetch option parameter:
Parser.fetchOptionParams('e', 'path')
# if count was > 1, returns a list of the parameters related to this specific key.
# else, returns the single parameter
# returns None if key or parameter key was not found