Add support for parsers that can be used bare, no required args.
This commit is contained in:
parent
abe73ae24d
commit
87ab04abab
1 changed files with 28 additions and 5 deletions
|
@ -3,6 +3,18 @@ import functools
|
|||
|
||||
HELPSTRINGS = {'', 'help', '-h', '--help'}
|
||||
|
||||
def can_use_bare(parser):
|
||||
has_func = bool(parser.get_default('func'))
|
||||
has_required_args = any(action.required for action in parser._actions)
|
||||
return has_func and not has_required_args
|
||||
|
||||
def can_use_bare_subparsers(subparser_action):
|
||||
can_bares = set(
|
||||
sp_name for (sp_name, sp) in subparser_action.choices.items()
|
||||
if can_use_bare(sp)
|
||||
)
|
||||
return can_bares
|
||||
|
||||
def docstring_preview(text):
|
||||
'''
|
||||
This function assumes that your docstring is formatted with a single blank
|
||||
|
@ -49,12 +61,15 @@ def betterhelp(parser, docstring):
|
|||
lesser of two evils. Plus, maybe someday I'll find a need for it and won't
|
||||
have to make any changes to do it.
|
||||
'''
|
||||
can_bare = can_use_bare(parser)
|
||||
def wrapper(main):
|
||||
@functools.wraps(main)
|
||||
def wrapped(argv):
|
||||
argument = listget(argv, 0, '').lower()
|
||||
|
||||
if argument in HELPSTRINGS:
|
||||
if argument == '' and can_bare:
|
||||
pass
|
||||
elif argument in HELPSTRINGS:
|
||||
print(docstring)
|
||||
return 1
|
||||
|
||||
|
@ -68,7 +83,6 @@ def get_subparser_action(parser):
|
|||
return action
|
||||
raise TypeError('Couldn\'t locate the SubParsersAction.')
|
||||
|
||||
|
||||
def set_alias_docstrings(sub_docstrings, subparser_action):
|
||||
'''
|
||||
When using subparser aliases:
|
||||
|
@ -102,22 +116,31 @@ def set_alias_docstrings(sub_docstrings, subparser_action):
|
|||
def subparser_betterhelp(parser, main_docstring, sub_docstrings):
|
||||
subparser_action = get_subparser_action(parser)
|
||||
sub_docstrings = set_alias_docstrings(sub_docstrings, subparser_action)
|
||||
can_bare = can_use_bare(parser)
|
||||
can_bares = can_use_bare_subparsers(subparser_action)
|
||||
|
||||
def wrapper(main):
|
||||
@functools.wraps(main)
|
||||
def wrapped(argv):
|
||||
command = listget(argv, 0, '').lower()
|
||||
|
||||
if command == '' and can_bare:
|
||||
return main(argv)
|
||||
|
||||
if command not in sub_docstrings:
|
||||
print(main_docstring)
|
||||
if command == '':
|
||||
print('You are seeing the default help text because you did not choose a command.')
|
||||
because = 'you did not choose a command.'
|
||||
print(f'You are seeing the default help text because {because}')
|
||||
elif command not in HELPSTRINGS:
|
||||
print(f'You are seeing the default help text because "{command}" was not recognized')
|
||||
because = f'"{command}" was not recognized'
|
||||
print(f'You are seeing the default help text because {because}')
|
||||
return 1
|
||||
|
||||
argument = listget(argv, 1, '').lower()
|
||||
if argument in HELPSTRINGS:
|
||||
if argument == '' and command in can_bares:
|
||||
pass
|
||||
elif argument in HELPSTRINGS:
|
||||
print(sub_docstrings[command])
|
||||
return 1
|
||||
|
||||
|
|
Loading…
Reference in a new issue