Created
May 14, 2020 04:33
-
-
Save phanthaihuan/96ea8c956718015f2e5ffd2678bc4d5f to your computer and use it in GitHub Desktop.
How to access command line arguments in Python
This file contains 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 sys | |
print(sys.argv) | |
------- | |
import argparse | |
parser = argparse.ArgumentParser("simple_example") | |
parser.add_argument("counter", help="An integer will be increased by 1 and printed.", type=int) | |
args = parser.parse_args() | |
print(args.counter + 1) | |
------- | |
and the output for python prog.py -h | |
usage: simple_example [-h] counter | |
positional arguments: | |
counter counter will be increased by 1 and printed. | |
optional arguments: | |
-h, --help show this help message and exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment