Search code examples
pythonargparsecredstash

conditional subparser based on mutually exclusive group argument


I am working on extending the code in credstash with my code here:

https://github.com/willcrain1/credstash

My issue is that I would like to add a required argument, but only if you select -b as one of the mutually exclusive (you will have to provide the bucket name as well as the 'credstash' name) so s3 requires 2 parameters where dynamodb - the -t argument only requires one (just table name).

Right now the code already has subparsers for what action you want to take. I'm new to python so new to argparse, so any advice on an efficient way of how to add a conditional parameter -c Credstash but only when -b is used is appreciated.

usage: credstash [-h] [-r REGION] [-b BUCKET | -t TABLE] [-p PROFILE | -n ARN] {delete,get,getall,list,put,setup} ...


Solution

  • Copied from my comment:

    Try setting nargs=2 for -b. Usage would then look like

    [-b BUCKET BUCKET | -t TABLE]
    

    Adding metavar=('bucket', 'credstash') (tuple, not list) should change the usage to:

    [-b bucket credstach | -t TABLE]