Skip to content Skip to sidebar Skip to footer

Python Using Diferent Options Multiple Times With Argparse

I am working on a custom Nagios script in which I would like to implement parsing of command line arguments in the same way that is used on existing Nagios Plugin check_disk. In th

Solution 1:

To handle this:

check_disk -w 100 -c 50 -C -w 1000 -c 500 -p /foo -C -w 5% -c 3% -p /bar

I can imagine starting with a namespace

args = argparse.Namespace(w:[None], c:[None], p:[None])
args = parser.parse_args(args=args)

and define a couple of custom Action classes.

For `-C` use a class that appends a `None` to each of those 3 attributes
    namespace['w'].append(None), etc

For each of `w`, `c` and `p`, an Action class, that replaces the last `None`
    with the users value.  

In other words, use the C argument to 'reset' by advancing the lists, and then use the others to to adjust the default default values.

Alternatively start with a Namespace(C=[[None, None, None]]), and add append a list with each 'C'. Then 'w' would set the namespace['C'][-1][0] etc. (Or use a list of dicts).


Post a Comment for "Python Using Diferent Options Multiple Times With Argparse"