Skip to content

Instantly share code, notes, and snippets.

@mbarkhau
Last active April 1, 2022 14:24
Show Gist options
  • Save mbarkhau/376e2ac7ed812a23e2e1 to your computer and use it in GitHub Desktop.
Save mbarkhau/376e2ac7ed812a23e2e1 to your computer and use it in GitHub Desktop.
mimimal arg parsing
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