Last active
May 10, 2021 09:14
-
-
Save wenijinew/6f01179e9517c4f6da9d9ade709963c1 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
#!/usr/bin/env python | |
import getopt, sys | |
ABNORMAL_STATE=2 | |
EMPTY="" | |
def hello(name): | |
print("Hello, ", name) | |
def usage(): | |
print(sys.argv[0], " <-h|--help> <-n|--name> [your name]") | |
def main(): | |
name = EMPTY | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], "hn:", ["help", "name="]) | |
# opts, args = getopt.gnu_getopt(sys.argv[1:], "hn:", ["help", "name="]) | |
print("opts: ", opts) | |
print("args: ", args) | |
for option, value in opts: | |
if option in ("-h", "--help"): | |
usage() | |
elif option in ("-n", "--name"): | |
name = value | |
if len(opts) == 0: | |
usage() | |
except getopt.GetoptError as err: | |
print(err) | |
usage() | |
sys.exit(ABNORMAL_STATE) | |
if (name != EMPTY): | |
hello(name) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment