0

I want to make sure some necessary arguments is provided by user when they run the python script. And I want to break the program and print the help information if the required argument is missing.

Right now, I write some program like below to achieve this goal, If there are some convenient way to do this ?

import argparse

parser = argparse.ArgumentParser(description='Description of your program')

parser.add_argument('-a', '--arg1', type=str, help='Description of arg1')
parser.add_argument('-b', '--arg2', type=str, help='Description of arg2')
parser.add_argument('-c', '--arg3', type=str, help='Description of arg3')

args = parser.parse_args()

# Check if the required arguments have been provided
if not args.arg3:
    parser.print_help()
    exit()

if I setrequired == True in add_argument() function, I can't use the parser.print_help() function, so how do this in a fancy way.

xin king
  • 21
  • 6

0 Answers0