Last active
April 1, 2022 14:24
-
-
Save mbarkhau/376e2ac7ed812a23e2e1 to your computer and use it in GitHub Desktop.
mimimal arg parsing
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
import sys | |
def getarg(argname, args=sys.argv[1:]): | |
long_arg = '--' + argname | |
short_arg = '-' + argname[:1] | |
for idx, arg in enumerate(args): | |
if arg.startswith(long_arg): | |
if arg == long_arg: | |
# check for parameter | |
if idx + 1 < len(args): | |
nextarg = args[idx + 1] | |
if not nextarg.startswith("-"): | |
return nextarg | |
return True | |
if '=' in arg: | |
argval = arg.split("=", 1)[-1] | |
return argval | |
if arg == short_arg: | |
return True | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment