简体   繁体   中英

How to use python argparse with conditionally optional arguments?

Here is the current code.

import time
import collections
from modules import outputs
from modules import scrub
from modules import lookups

parser = argparse.ArgumentParser(description='AppMap Converter to Generate Asset Files from AppMapp Data')
parser.add_argument("operation", nargs='?', default="empty", help='The operation to perform')
parser.add_argument("input", nargs='?', default="empty", help='The input AppMapp File Path')
parser.add_argument("output", nargs='?', default="empty", help='The output Asset File Path')
args = parser.parse_args()

start = time.time()

if(args.operation == "Convert"):
    input_file_path = args.input
    output_file_path = args.output
    #DO LOTS OF STUFF
else:
    exit()

The script is called sacsproc, so I run it from the command line as follows:

./sacsproc Convert input.csv output.csv

This all works nicely, the problem is that I need more sacsproc commands which may have a totally different set of secondary parameters. ie one command might be:

./sacsproc Clean -rts input.csv output.csv err.csv

Thus, I am trying to determine how one defines arguments that are conditional on the first argument ? In my mind, I'm thinking about the zfs command line utilities that do what I am trying to do (eg zpool create mirror sdb sdc vs. zpool remove sda).

use subparsers

subparsers = parser.add_subparsers(help="sub-command help")

group1 = subparsers.add_parser("something",help="do something")
group1.set_defaults(which="g1") # some default value (so you know which group was activated)
group1.add_argument("ARG",help='do something on ARG')

group2 = subparsers.add_parser("other",help="do something else")
group2.set_defaults(which="g2") # give some default value
group2.add_argument("ARG",help='do something else on ARG')

ok ...

import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help="sub-command help")
g1 = subparsers.add_parser("thing1",help="bind to a port and just echo back anything it gets ... with a prompt")
g1.set_defaults(which="g1")
g1.add_argument("input",help='the input file')
g1.add_argument("output",help='the output file')
g2 = subparsers.add_parser("thing2",help="create a bridge between two ports, this is useful for generating a logfile")
g2.set_defaults(which="g2")
g2.add_argument("input",help='thie input file')
g2.add_argument("output",help='the output file')
g2.add_argument("error",help="the err file")


def print_help(args):
    print "ARGS:",args
    try:
        parser.parse_args(args)
    except:
        print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"

print_help(["-h"])
print_help(["thing1","-h"])
print_help(["thing2","-h"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM