Extends the native Python3 `argparse.ArgumentParser` to supports using maps as a `choices` argument type.
Project description
Wintersdeep Argument Parser
This is a small extension to the native Python3 argparse.ArgumentParser
. It alters the behaviour of the choices
argument on the add_argument
method such that it supports the use of Mapping
objects such as dict
.
When using a Mapping
object as a choices
constraint the user will be able to choose only from the keys of the Mapping
object specified. The arguments value in the resulting Namespace
will be that which is associated with the key in the provided Mapping - e.g. if the user specified the key "abc" the value would be taken from Mapping["abc"]
.
In such cases
from wintersdeep.argparse import ArgumentParser
formatters = {
"bin": lambda n: f"{n:b}",
"oct": lambda n: f"{n:o}",
"hex": lambda n: f"{n:x}"
}
argument_parser = ArgumentParser()
argument_parser.add_argument("N", nargs="+", type=int)
argument_parser.add_argument("-f", choices=formatters, default_key='hex')
arguments = argument_parser.parse_args()
for n in arguments.N:
print( arguments.formatter(n) )
Installation
pip install wintersdeep.argparse
Features
- Allows usage of
Mapping
type objects as achoices
constraint onArgumentParser::add_argument
. Accepted input will be constrained to key values in the given map and the value in the resultingNamespace
will be that keys associated value. - Added a
default_key
keyword argument; you can specify eitherdefault
ordefault_key
but not both. When usingdefault_key
the default value will be the value associated with the specifieddefault_key
in thechoices
map.default_key
allows you to ensure documentation and behaviour remain syncronised and readable.default
can still be used as normal if preferred, and is required when the default option is not user selectable.
Behaviour Notes
-
The
action
wasn't applied to thedefault
value (specified or derived fromdefault_key
).This is by intention and is consistent with the native
ArgumentParser
behaviour - the default value is provided "as-is" when the option is not specifed. -
The
Action
returned byadd_argument
is not the type specifiedThe libraries behaviours are implemented by shimming
add_argument
, and inserting a customaction
whenchoices
are aMapping
type.If you have set your own
action
thenadd_argument
will preserve this and invoke it after the value has been translated, so you should still get the expected behaviour. However this does mean theAction
class return byadd_argument
will not be of the type you originally specified. Your originalaction
can be found in the[action].next_action
property.from argparse import Action from wintersdeep.argparse import ArgumentParser # A custom action to add a prefix to all formatters. class AddPrefix(Action): def __call__(self, parser, namespace, values, option_string=None): # by the time this action receives `values` it has been converted into its Mapping associated value (in context a lambda function from `formatters`. prefix_fn = lambda x: f"Your number is {values(x)}" setattr(namespace, self.dest, prefix_fn) formatters = { "bin": lambda n: f"{n:b}", "oct": lambda n: f"{n:o}", "hex": lambda n: f"{n:x}" } argument_parser = ArgumentParser() argument_parser.add_argument("N", nargs="+", type=int) argument_parser.add_argument("-f", choices=formatters, default_key='hex') arguments = argument_parser.parse_args() for n in arguments.N: print( arguments.formatter(n) ) print(action.__class__.__name__) # "MappingChoicesAction" print(action.next_action.__class__) # <class '__main__.AddPrefix'>
Project details
Release history Release notifications | RSS feed
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 wintersdeep_argparse-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6801b75648a7b003500a05b36f91284721aacd33c70bb5454a462a5930253ee |
|
MD5 | 5c71d955a0fc372f3be112e112738efd |
|
BLAKE2b-256 | e6b11b90a7ecf932da7a669689616b301f8eaf1b5ab465baa4d086037135cd42 |
Hashes for wintersdeep.argparse-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cef17ad51ceabdba01d952997ee5cc39004ea50454bbde326d92c431f270a12 |
|
MD5 | f3e5421bcda901b41fbdd8238c53ea3f |
|
BLAKE2b-256 | 590b0bd93b96056bc98d4844dfa5b101a5d822a18d5b45eda6681fbe8249b530 |