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.

3
  • 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. Commented Apr 12, 2020 at 20:44
  • Re using json input: stackoverflow.com/questions/61071878/… Commented Apr 12, 2020 at 20:44
  • Practice with the argparse tutorial or reference. 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. Commented Apr 12, 2020 at 20:53

2 Answers 2

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

Comments

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

Comments

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.