Skip to content

Instantly share code, notes, and snippets.

@thibthibaut
Last active November 3, 2020 11:13
Show Gist options
  • Save thibthibaut/e01872d8c33fc3f67dc21d97be14803b to your computer and use it in GitHub Desktop.
Save thibthibaut/e01872d8c33fc3f67dc21d97be14803b to your computer and use it in GitHub Desktop.
import argparse
# Create the parser and add arguments
parser = argparse.ArgumentParser()
parser.add_argument(dest='argument0', help="This is the first argument")
# Cast the input to string, int or float type
parser.add_argument(dest='argument1', type=str, help="A string argument")
parser.add_argument(dest='argument2', type=int, help="An integer argument")
parser.add_argument(dest='argument3', type=float, help="A float argument")
# Validate that the input is in specified list
parser.add_argument(dest='argument4', choices=['red', 'green', 'blue'])
# Accept multiple inputs for an argument, returned as a list
# Will be of type string, unless specified
parser.add_argument(dest='argument5', nargs=2, type=int)
# Optional positional argument (length 0 or 1)
parser.add_argument(dest='argument6', nargs='?')
# Parse and print the results
args = parser.parse_args()
print(args.argument1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment