Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@eliasdorneles
Created June 16, 2020 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliasdorneles/51fc37dd4910fe07694a30ed6a70aa57 to your computer and use it in GitHub Desktop.
Save eliasdorneles/51fc37dd4910fe07694a30ed6a70aa57 to your computer and use it in GitHub Desktop.
Alternative Option Parsing for Pudb
from __future__ import absolute_import, print_function, division
from collections import namedtuple
import sys
def exit_showing_usage():
print("""Usage: pudb [options] [SCRIPT-TO-RUN|-m MODULE] [ARGUMENTS]
Options:
-h, --help show this help message and exit
-s, --steal-output
-m MODULE, --module=MODULE
Debug module or package instead of script
(any arguments after this will be passed straight to the module)
--pre-run=COMMAND Run command before each program run
""")
sys.exit(1)
def parse_args():
option_kwargs = {
'pre_run': '',
'steal_output': False
}
args = sys.argv[1:]
module_name = None
while args and args[0].startswith('-'):
option = args[0]
if option == '-h' or option == '--help':
exit_showing_usage()
if option == '-s' or option == '--steal-output':
option_kwargs['steal_output'] = True
args = args[1:]
elif option == '--pre-run':
if len(args) < 2:
exit_showing_usage()
option_kwargs['pre_run'] = args[1]
args = args[2:]
elif option == '-m' or option == '--module':
if len(args) < 2:
exit_showing_usage()
module_name = args[1]
args = args[2:]
# we treat all args after -m module as arguments to the module itself
break
script_name = None
if module_name is None:
if not args:
exit_showing_usage()
script_name = args[0]
args = args[1:]
PudbOptions = namedtuple('PudbOptions', 'option_kwargs script module cmd_args')
return PudbOptions(option_kwargs, script_name, module_name, args)
print(parse_args())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment