Python: Using argparse with either argument required by creating argument group with mutual exclusion

Written by - 0 comments

Published on February 18th 2020 - Listed in Python Coding Monitoring


I'm currently working on a pull request for the check_varnish.py monitoring plugin and was trying to figure out a way to tell Python's argparse function that either one parameter or another one should be required.

In the current script version, argparse uses the "required" keyword to determine which parameter/argument is required:

    argp = argparse.ArgumentParser(description=__doc__)
    argp.add_argument('-w', '--warning', metavar='RANGE', default='',
                      help='return warning if value is outside RANGE')
    argp.add_argument('-c', '--critical', metavar='RANGE', default='',
                      help='return critical if value is outside RANGE')
    argp.add_argument('-f', '--field', metavar='FIELD', dest='arg_field', required=True, action='store', default='MAIN.sess_dropped',
                      help='field to query')
    argp.add_argument('-n', '--name', metavar='NAME', dest='arg_name', action='store', default='',
                      help='name of Varnish instance (optional)')
    args = argp.parse_args()

Basically only the parameter -f / --field is required. This can be verified when executing the plugin:

 # /usr/lib/nagios/plugins/check_varnish.py
usage: check_varnish.py [-h] [-w RANGE] [-c RANGE] -f FIELD [-n NAME]
check_varnish.py: error: argument -f/--field is required

As I want to extend the plugin to have a possibility to use a list of fields (instead of a single varnishstat field), either -f / --field or -l / --list is a required parameter for the plugin. This can be achieved by grouping the arguments (in this case the possible required parameters) together and set a mutual exclusion. This means that the parameters in the same group are excluding each one another, so only one of them can be used. In additional to this, the "required" keyword can be assigned to this group.

    argp = argparse.ArgumentParser(description=__doc__)
    argp.add_argument('-w', '--warning', metavar='RANGE', default='',
                      help='return warning if value is outside RANGE')
    argp.add_argument('-c', '--critical', metavar='RANGE', default='',
                      help='return critical if value is outside RANGE')
    req = argp.add_mutually_exclusive_group(required=True)
    req.add_argument('-f', '--field', metavar='FIELD', dest='arg_field', action='store', default='MAIN.sess_dropped',
                      help='field to query')
    req.add_argument('-l', '--list', metavar='LIST', dest='arg_list', action='store', default='',
                      help='list of fields to query, no thresholds possible')

    argp.add_argument('-n', '--name', metavar='NAME', dest='arg_name', action='store', default='',
                      help='name of Varnish instance (optional)')
    args = argp.parse_args()

When the plugin is now executed without any parameters, the plugin throws the following error:

 # ./check_varnish.py
usage: check_varnish.py [-h] [-w RANGE] [-c RANGE] (-f FIELD | -l LIST)
                        [-n NAME]
check_varnish.py: error: one of the arguments -f/--field -l/--list is required



Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.