0

I'm trying to write a script that would take some flags and files as arguments and then execute other scripts, depend on the flag that the user chooses. For example, the command line should look like that:

main_script.py -flag1 -file_for_flag_1 another_file_for_flag_1

and

main_script.py -flag2 -file_for_flag_2

I tried to use the argparse library, but I don't know how to take the input files as arguments for the next steps and manipulate them as I want. I started with:

parser = argparse.ArgumentParser(description="Processing inputs")
parser.add_argument(
    "-flat_map",
    type=str,
    nargs="+",
    help="generates a flat addressmap from the given files",
)
parser.add_argument(
    "-json_convert",
    type=str,
    nargs="+",
    help="generates a flat addressmap from the given files",
)
args = parser.parse_args(args=["-flat_map"])
print(args)

I printed args in the end to see what I get from it but I got nothing I can work with. Would like to have some guidance. Thanks.

AMC
  • 2,642
  • 7
  • 13
  • 35
Mark Cohen
  • 13
  • 1
  • `args` is a simple `Namespace` object. `args.flat_map` will give one list of inputs. With '+' it expects at least one. Similarly `args.json_convert`. – hpaulj Apr 12 '20 at 20:44
  • Re using `json` input: https://stackoverflow.com/questions/61071878/using-argparse-in-python-to-parse-an-entire-json – hpaulj Apr 12 '20 at 20:44
  • Practice with the `argparse` tutorial or reference. https://docs.python.org/3/howto/argparse.html. `argparse` is primarily a means of `parsing`, understanding, the user input. Most cases you get strings or lists of strings. Your own code then uses those strings as input. – hpaulj Apr 12 '20 at 20:53

2 Answers2

0

You can convert the args to a dict (where the key is the arg option and the value is the arg value) if it's more convenient for you:

args_dict = {key: value for key, value in vars(parser.parse_args()).items() if value}
Gabio
  • 9,126
  • 3
  • 12
  • 32
0

Using argparse you can use sub-commands to select sub-modules:

import argparse

def run_command(parser, args):
    if args.command == 'command1':
        # add code for command1 here
        print(args)
    elif args.command == 'command2':
        # add code for command2 here
        print(args)

parser = argparse.ArgumentParser(
    prog='PROG', 
    epilog="See '<command> --help' to read about a specific sub-command."
)
subparsers = parser.add_subparsers(dest='command', help='Sub-commands')

A_parser = subparsers.add_parser('command1', help='Command 1')
A_parser.add_argument("--foo")
A_parser.add_argument('--bar')
A_parser.set_defaults(func=run_command)

B_parser = subparsers.add_parser('command2', help='Command 2')
B_parser.add_argument('--bar')
B_parser.add_argument('--baz')
B_parser.set_defaults(func=run_command)

args = parser.parse_args()
if args.command is not None:
    args.func(parser, args)
else:
    parser.print_help()

This generates a help page like so:

~ python args.py -h
usage: PROG [-h] {command1,command2} ...

positional arguments:
  {command1,command2}  Sub-commands
    command1           Command 1
    command2           Command 2

optional arguments:
  -h, --help           show this help message and exit

See '<command> --help' to read about a specific sub-command.

and help text for each sub-command:

~ python args.py B -h
arg.py command2 -h
usage: PROG command2 [-h] [--bar BAR] [--baz BAZ]

optional arguments:
  -h, --help  show this help message and exit
  --bar BAR
  --baz BAZ
Alex
  • 6,610
  • 3
  • 20
  • 38