Last active
May 20, 2020 04:16
-
-
Save sodonnell/981c15d0bb49c3b99992df54d15a0d21 to your computer and use it in GitHub Desktop.
Basic argument processing in python3
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
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use argparse instead