Skip to content

Commit 252844f

Browse files
computersforpeacestephenfin
authored andcommitted
pwclient: rework multi-command help text
Our --help handling is convoluted and confusing, since we're hacking around using some of argparse's built-in features (like generating --help arguments for us). It seems like we were hacking around the conflict between -h used for hashes and -h used for automatic help flags. Fortunately, Python's argparse provides us with a 'conflict_handler' which will resolve these conflicts for us. Altogether, this patch means that 'pwclient --help' will not generate a full recursive print of all subcommand helps (arguably a good thing), but it provides better automatic formatting of all the supported subcommands and eliminates some awkward code. Sample runs: $ pwclient usage: pwclient [-h] {apply,git-am,get,info,projects,states,view,update,list,search} ... optional arguments: -h, --help show this help message and exit Commands: {apply,git-am,get,info,projects,states,view,update,list,search} apply Apply a patch (in the current dir, using -p1) git-am Apply a patch to current git branch using "git am". get Download a patch and save it locally info Display patchwork info about a given patch ID projects List all projects states Show list of potential patch states view View a patch update Update patch list List patches, using the optional filters specified below and an optional substring to search for patches by name search Alias for "list" Use 'pwclient <command> --help' for more info $ pwclient info --help usage: pwclient info [--help] [-h HASH] [-p PROJECT] [ID [ID ...]] positional arguments: ID Patch ID optional arguments: --help show this help message and exit -h HASH Lookup by patch hash -p PROJECT Lookup patch in project Signed-off-by: Brian Norris <[email protected]> Acked-by: Mike Frysinger <[email protected]> Reviewed-by: Stephen Finucane <[email protected]>
1 parent a466ebb commit 252844f

File tree

1 file changed

+9
-58
lines changed

1 file changed

+9
-58
lines changed

patchwork/bin/pwclient

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -346,31 +346,6 @@ def patch_id_from_hash(rpc, project, hash):
346346

347347
auth_actions = ['update']
348348

349-
# unfortunately we currently have to revert to this ugly hack..
350-
class _RecursiveHelpAction(argparse._HelpAction):
351-
352-
def __call__(self, parser, namespace, values, option_string=None):
353-
parser.print_help()
354-
print
355-
356-
subparsers_actions = [
357-
action for action in parser._actions
358-
if isinstance(action, argparse._SubParsersAction)
359-
]
360-
hash_n_id_actions = set(['hash', 'id', 'help'])
361-
for subparsers_action in subparsers_actions:
362-
for choice, subparser in subparsers_action.choices.items():
363-
# gross but the whole thing is..
364-
if (len(subparser._actions) == 3 \
365-
and set([a.dest for a in subparser._actions]) \
366-
== hash_n_id_actions) \
367-
or len(subparser._actions) == 0:
368-
continue
369-
print("command '{}'".format(choice))
370-
print(subparser.format_help())
371-
372-
parser.exit()
373-
374349
def main():
375350
hash_parser = argparse.ArgumentParser(add_help=False)
376351
hash_parser.add_argument(
@@ -425,38 +400,22 @@ def main():
425400
'patch_name', metavar='STR', nargs='?',
426401
help='substring to search for patches by name',
427402
)
428-
help_parser = argparse.ArgumentParser(add_help=False)
429-
help_parser.add_argument(
430-
'--help', action='help', help=argparse.SUPPRESS,
431-
#help='''show this help message and exit'''
432-
)
433403

434404
action_parser = argparse.ArgumentParser(
435405
prog='pwclient',
436-
add_help=False,
437-
formatter_class=argparse.RawDescriptionHelpFormatter,
438-
epilog='''(apply | get | info | view | update) (-h HASH | ID [ID ...])''',
439-
)
440-
action_parser.add_argument(
441-
'--help',
442-
#action='help',
443-
action=_RecursiveHelpAction,
444-
help='''Print this help text'''
406+
epilog='Use \'pwclient <command> --help\' for more info',
445407
)
446408

447409
subparsers = action_parser.add_subparsers(
448410
title='Commands',
449-
metavar=''
450411
)
451412
apply_parser = subparsers.add_parser(
452-
'apply', parents=[hash_parser, help_parser],
453-
add_help=False,
413+
'apply', parents=[hash_parser], conflict_handler='resolve',
454414
help='''Apply a patch (in the current dir, using -p1)'''
455415
)
456416
apply_parser.set_defaults(subcmd='apply')
457417
git_am_parser = subparsers.add_parser(
458-
'git-am', parents=[hash_parser, help_parser],
459-
add_help=False,
418+
'git-am', parents=[hash_parser], conflict_handler='resolve',
460419
help='''Apply a patch to current git branch using "git am".'''
461420
)
462421
git_am_parser.set_defaults(subcmd='git_am')
@@ -466,38 +425,32 @@ def main():
466425
help='''pass --signoff to git-am'''
467426
)
468427
get_parser = subparsers.add_parser(
469-
'get', parents=[hash_parser, help_parser],
470-
add_help=False,
428+
'get', parents=[hash_parser], conflict_handler='resolve',
471429
help='''Download a patch and save it locally'''
472430
)
473431
get_parser.set_defaults(subcmd='get')
474432
info_parser = subparsers.add_parser(
475-
'info', parents=[hash_parser, help_parser],
476-
add_help=False,
433+
'info', parents=[hash_parser], conflict_handler='resolve',
477434
help='''Display patchwork info about a given patch ID'''
478435
)
479436
info_parser.set_defaults(subcmd='info')
480437
projects_parser = subparsers.add_parser(
481438
'projects',
482-
add_help=False,
483439
help='''List all projects'''
484440
)
485441
projects_parser.set_defaults(subcmd='projects')
486442
states_parser = subparsers.add_parser(
487443
'states',
488-
add_help=False,
489444
help='''Show list of potential patch states'''
490445
)
491446
states_parser.set_defaults(subcmd='states')
492447
view_parser = subparsers.add_parser(
493-
'view', parents=[hash_parser, help_parser],
494-
add_help=False,
448+
'view', parents=[hash_parser], conflict_handler='resolve',
495449
help='''View a patch'''
496450
)
497451
view_parser.set_defaults(subcmd='view')
498452
update_parser = subparsers.add_parser(
499-
'update', parents=[hash_parser, help_parser],
500-
add_help=False,
453+
'update', parents=[hash_parser], conflict_handler='resolve',
501454
help='''Update patch''',
502455
epilog='''Using a COMMIT-REF allows for only one ID to be specified''',
503456
)
@@ -515,17 +468,15 @@ def main():
515468
)
516469
update_parser.set_defaults(subcmd='update')
517470
list_parser = subparsers.add_parser("list",
518-
add_help=False,
519471
#aliases=['search'],
520-
parents=[filter_parser, help_parser],
472+
parents=[filter_parser],
521473
help='''List patches, using the optional filters specified
522474
below and an optional substring to search for patches
523475
by name'''
524476
)
525477
list_parser.set_defaults(subcmd='list')
526478
search_parser = subparsers.add_parser("search",
527-
add_help=False,
528-
parents=[filter_parser, help_parser],
479+
parents=[filter_parser],
529480
help='''Alias for "list"'''
530481
)
531482
# Poor man's argparse aliases:

0 commit comments

Comments
 (0)