Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
argparse cheat sheet
import argparse
parser = argparse.ArgumentParser()
### Positional
parser.add_argument('positional1')
parser.add_argument('positional2', help="positional argument 2")
parser.add_argument('positional3', help="positional argument 3 type int", type=int)
### Optional
parser.add_argument("--optional1", help="optional argument 1")
parser.add_argument("--optional2", help="optional argument 2", action="store_true")
parser.add_argument("-op3", "--optional3", action="store_true")
args = parser.parse_args()
print("args.positional1:" + args.positional1 + " " + str(type(args.positional1)))
print("args.positional2:" + args.positional2 + " " + str(type(args.positional2)))
print("args.positional3:" + str(args.positional3) + " " + str(type(args.positional3)))
if args.optional1:
print("args.optional1:" + args.optional1 + " " + str(type(args.optional1)))
print("args.optional2:" + str(args.optional2) + " " + str(type(args.optional2)))
print("args.optional3:" + str(args.op3) + " " + str(type(args.optional3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment