Last active
August 15, 2022 19:14
-
-
Save tawateer/a47460dd055a2bd69f94 to your computer and use it in GitHub Desktop.
python argparse example
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
#!/bin/env python | |
""" argparse example. | |
url: https://mkaz.com/2014/07/26/python-argparse-cookbook/ | |
""" | |
import argparse | |
def main(): | |
parser = argparse.ArgumentParser(description='add, modify and delete upstream nodes') | |
parser.add_argument( | |
'action', choices=['add', 'modify', 'delete'], help='action of upstream nodes, one of add, modify, delete') | |
parser.add_argument( | |
'-n', '--name', required=True, type=str, help='upstream node name') | |
parser.add_argument( | |
'-l', '--loki_id', required=False, type=int, help='loki id') | |
parser.add_argument( | |
'-p', '--port', required=False, type=int, help='port') | |
parser.add_argument( | |
'-i', '--ip_hash', required=False, type=int, choices=[0, 1], help='ip_hash') | |
parser.add_argument( | |
'-o', '--online', required=False, type=int, choices=[0, 1], help='online') | |
args = parser.parse_args() | |
if args.action == "add": | |
print args.name, args.loki_id, args.port, args.ip_hash, args.online | |
elif args.action == "modify": | |
_dict = {} | |
for i in ["loki_id", "port", "ip_hash", "online"]: | |
if getattr(args, i) is not None: | |
_dict[i] = getattr(args, i) | |
print args.name, _dict | |
elif args.action == "delete": | |
print args.name | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! :)