0

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. i.e. 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 (e.g. zpool create mirror sdb sdc vs. zpool remove sda).

1 Answer 1

1

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"])
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, points me in the right direction. After testting I think this works, but the help files seem a little wonky: $ ./sacsproc CombineAppMapp --help usage: sacsproc ConvertAppMapp [-h] [input] [output]
Moving things around I get (not what I expect): $ ./sacsproc CombineAppMapp --help usage: sacsproc [operation] ConvertAppMapp [-h] [input] [output]
I was hoping you could connect the dots yourself, but there is now a more complete example that is exactly what you want i think...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.