Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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