How to parse one group out of the two groups of command line arguments using argparse in Python3?

I want to develop a CLI that takes one of the two pairs of arguments together:

group-resources --limit 5

or

group-overlap --groups group1,group2

The group-resources is positional and limit is required. Same rules are for the rest of the two. However, my current code makes it a compulsion that all four are required.

My current code:

import argparse

import analysis
import cleanup
import etl
import initialize_db

parser = argparse.ArgumentParser(
    description='Inventory DB for Cloud Infrastructure')
parser.add_argument(
    'group-resources', help='List top k groups as per their memory, disk, and cpu count.')
parser.add_argument(
    '--limit', help='Number of groups in descending order of their resources.', type=int, required=True)
parser.add_argument(
    'group-overlap', help='Fetch nodes that belong in both the groups')
parser.add_argument(
    '--groups', help='specify the group names separated by a comma.', type=str, required=True
)

args = parser.parse_args()

print(args)

When I run python3 cli.py group-resources --limit 5, I get the following error:

usage: cli.py [-h] --limit LIMIT --groups GROUPS group-resources group-overlap
cli.py: error: the following arguments are required: group-overlap, --groups

I want that if I provide group-resources and limit, or if I provide group-overlap and groups, then my code should work. No other permutation should be allowed. How do I do that?

1 answer

  • answered 2020-09-27 04:08 รยקคгรђשค

    We can use subparsers. Here is how we can do with slight modification in your code.

    import argparse
    
    parser = argparse.ArgumentParser(
        description='Inventory DB for Cloud Infrastructure')
    
    subparsers = parser.add_subparsers(help='types of arguments')
    
    group_resources_parser = subparsers.add_parser("group-resources", help='List top k groups as per their memory, disk, and cpu count.')
    group_overlap_parser = subparsers.add_parser("group-overlap", help='Fetch nodes that belong in both the groups')
    group_resources_parser.add_argument(
        '--limit', help='Number of groups in descending order of their resources.', type=int, required=True)
    group_overlap_parser.add_argument(
        '--groups', help='specify the group names separated by a comma.', type=str, required=True
    )
    
    args = parser.parse_args()
    
    print(args)
    

    Examples:

    1. python3 cli.py group-resources

        usage: cli.py group-resources [-h] --limit LIMIT
        cli.py group-resources: error: the following arguments are required: --limit
      
    2. python3 cli.py group-overlap

       usage: cli.py group-overlap [-h] --groups GROUPS
       cli.py group-overlap: error: the following arguments are required: --groups
      
    3. python3 cli.py group-resources --limit 5 group-overlap

        usage: cli.py [-h] {group-resources,group-overlap} ...
        cli.py: error: unrecognized arguments: group-overlap