Skip to content

Instantly share code, notes, and snippets.

@sodonnell
Last active May 20, 2020 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sodonnell/981c15d0bb49c3b99992df54d15a0d21 to your computer and use it in GitHub Desktop.
Save sodonnell/981c15d0bb49c3b99992df54d15a0d21 to your computer and use it in GitHub Desktop.
Basic argument processing in python3
#!/usr/bin/env python3
#
# This gist shows how to process (4) common argument styles in python3 using the getopt module.
#
# -a
# -a val
# --a=val
# --a
#
import sys, getopt
opts, args = getopt.getopt(sys.argv[1:],"u:h",["url=","help"])
for opt, arg in opts:
if opt in ("-h", "--help"):
# -h
# --help
print('Add an active feed:\n')
print('\tpython3 feed.py -u https://somefeed.com/rss/ -a\n')
print('\tpython3 feed.py --url=https://somefeed.com/rss/ -a\n')
print('Add a de-activated feed:\n')
print('\tpython3 feed.py -u https://somefeed.com/rss/ -d\n')
print('\tpython3 feed.py --url=https://somefeed.com/rss/ -d\n')
sys.exit()
elif opt in ("-u", "--url"):
# -u https://someurl.com/
# --url=https://someurl.com/
url = arg
print(url)
@sodonnell
Copy link
Author

use argparse instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment