Skip to content Skip to sidebar Skip to footer

Argparse And Mutually Exclusive Groups, Each With Their Own Required Setting

I have a program that needs to have an option to either test a list of server ids OR issue a command against the server. This means, if I issue --test, then nothing else is requir

Solution 1:

As suggested in the comments, the way to go, if you wish to have mutually exclusive test and run logics, would be to use subparsers. The following is an illustration of the idea:

#!/usr/bin/env python3
"""
Script to test or run commands on given servers.
./the_script.py test  # To test all servers
./the_script.py run --id 127.0.0.1 --command "echo hello world"
"""
from argparse import ArgumentParser, RawDescriptionHelpFormatter as RDHF


def test_servers(servers):
    """
    Given a list of servers, let's test them!
    """
    for server in servers:
        print('Just tested server {s}'.format(s=server))

def do_actual_work(server_id, command):
    """
    Given a server ID and a command, let's run the command on that server!
    """
    print('Connected to server {s}'.format(s=server_id))
    print('Ran command {c} successfully'.format(c=command))


if __name__ == '__main__':
    parser = ArgumentParser(description=__doc__, formatter_class=RDHF)
    subs = parser.add_subparsers()
    subs.required = True
    subs.dest = 'run or test'
    test_parser = subs.add_parser('test', help='Test all servers')
    test_parser.set_defaults(func=test_servers)
    run_parser = subs.add_parser('run', help='Run a command on the given server')
    run_parser.add_argument('-i', '--id',
                            help='The ID of the server to connect to and run commands',
                            required=True)
    run_parser.add_argument('-c', '--command',
                            help='The command to run',
                            required=True)
    run_parser.set_defaults(func=do_actual_work)
    args = parser.parse_args()
    
    if args.func.__name__ == 'test_servers':
        all_servers = ['127.0.0.1', '127.0.0.2']
        test_servers(all_servers)
    else:
        do_actual_work(args.id, args.command)

The script sets up both mutually exclusive and required subparsers test and run. For the test subparser, nothing else is required. However, for the run subparser, both --id and --command would be required. Each of these subparsers is associated with its designated target function. For simplicity I had the test_parser tied to test_servers; while run_parser is associated with do_actual_work.

Further, you should be able to call the script as follows to run all tests:

./the_script.py test

To run a specific command on a specific server, you call the script as follows:

./the_script.py run --id 127 --command "echo hello world"

I hope this proves useful.


Post a Comment for "Argparse And Mutually Exclusive Groups, Each With Their Own Required Setting"