Last active
November 3, 2020 11:13
-
-
Save thibthibaut/e01872d8c33fc3f67dc21d97be14803b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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