Skip to content

Instantly share code, notes, and snippets.

@phanthaihuan
Created May 14, 2020 04:33
Show Gist options
  • Save phanthaihuan/96ea8c956718015f2e5ffd2678bc4d5f to your computer and use it in GitHub Desktop.
Save phanthaihuan/96ea8c956718015f2e5ffd2678bc4d5f to your computer and use it in GitHub Desktop.
How to access command line arguments in Python
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