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

Written by - 0 comments

Published on - 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.

RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder