Command Line Arguments
Example 1: Generate Help Argument and Message
import argparse
parser = argparse.ArgumentParser(description='A test program.')
args = parser.parse_args()$ ./test.py -h
$ ./test.py --help
A test program.
optional arguments:
-h, --help show this help message and exitExample 2: Handle a String Argument
import argparse
parser = argparse.ArgumentParser(description='A test program.')
parser.add_argument("print_string", help="Prints the supplied argument.")
args = parser.parse_args()
print(args.print_string)Example 3: Handle an Integer Argument
Example 4: Handle True and False Toggles
Example 5: Treat Argument Values as List
Example 6: File as command line argument for argparse - error message if argument is not valid
Example 7: File Extension Checking - argsparse
Example 8: Require either of two arguments using argparse
Example 8: If arg is A, do this, if B do that, if none of the above show help and quit
Last updated