From patchwork Fri Oct 16 23:12:07 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Norris X-Patchwork-Id: 531583 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 97EF21402B2 for ; Sat, 17 Oct 2015 10:12:47 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=lf1EuzVY; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 712881A0A09 for ; Sat, 17 Oct 2015 10:12:47 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=lf1EuzVY; dkim-atps=neutral X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Received: from mail-pa0-x22c.google.com (mail-pa0-x22c.google.com [IPv6:2607:f8b0:400e:c03::22c]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id D03DC1A08F1 for ; Sat, 17 Oct 2015 10:12:21 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=lf1EuzVY; dkim-atps=neutral Received: by pabws5 with SMTP id ws5so2566684pab.1 for ; Fri, 16 Oct 2015 16:12:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=GNKCYTg/QvJEJvLeCo/GQtw/MipcvJtM8SM18Q1czz8=; b=lf1EuzVYKjFi6402Y2TDg+1zNAA9XM4WL+mk3icHYRhfcvM06l1+b0Y5V57wfrROyN u/FIRg9JDw6+t4SCIi0Ssyd9ZigLW2RKpJElmB9fJyg/aFnd+p+zU2WYDXaIHIPqjkIv rSVHqnpjhpSzLQ3dubQ7e4zWxy/eXDdgVusHFC4R1BMCSQHMEFgaZBKUqo+DuK76Oxz5 h6Asjvf5XI4pjjgDbmPn8OHC4ET8whS2/MNMVFosRJZiOHxgeZqxSKKrLyfJuhUBsovM x+PaVjJoJP3t2s0vq9XhjhXnpXlDErkSsMMJRh0SZs8kIqT2OQ9aIGaep2jVcxZJ5FNy 1ySw== X-Received: by 10.68.197.196 with SMTP id iw4mr19348432pbc.153.1445037140035; Fri, 16 Oct 2015 16:12:20 -0700 (PDT) Received: from ban.mtv.corp.google.com ([172.22.64.120]) by smtp.gmail.com with ESMTPSA id rz7sm23145314pbc.7.2015.10.16.16.12.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 16 Oct 2015 16:12:19 -0700 (PDT) From: Brian Norris To: Subject: [PATCH 3/3] pwclient: rework multi-command help text Date: Fri, 16 Oct 2015 16:12:07 -0700 Message-Id: <1445037127-45750-3-git-send-email-computersforpeace@gmail.com> X-Mailer: git-send-email 2.6.0.rc2.230.g3dd15c0 In-Reply-To: <1445037127-45750-1-git-send-email-computersforpeace@gmail.com> References: <1445037127-45750-1-git-send-email-computersforpeace@gmail.com> X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mike Frysinger MIME-Version: 1.0 Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" 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 --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 --- patchwork/bin/pwclient | 67 +++++++------------------------------------------- 1 file changed, 9 insertions(+), 58 deletions(-) diff --git a/patchwork/bin/pwclient b/patchwork/bin/pwclient index 596a26d8aa9d..92e54f3c4a34 100755 --- a/patchwork/bin/pwclient +++ b/patchwork/bin/pwclient @@ -346,31 +346,6 @@ def patch_id_from_hash(rpc, project, hash): auth_actions = ['update'] -# unfortunately we currently have to revert to this ugly hack.. -class _RecursiveHelpAction(argparse._HelpAction): - - def __call__(self, parser, namespace, values, option_string=None): - parser.print_help() - print - - subparsers_actions = [ - action for action in parser._actions - if isinstance(action, argparse._SubParsersAction) - ] - hash_n_id_actions = set(['hash', 'id', 'help']) - for subparsers_action in subparsers_actions: - for choice, subparser in subparsers_action.choices.items(): - # gross but the whole thing is.. - if (len(subparser._actions) == 3 \ - and set([a.dest for a in subparser._actions]) \ - == hash_n_id_actions) \ - or len(subparser._actions) == 0: - continue - print("command '{}'".format(choice)) - print(subparser.format_help()) - - parser.exit() - def main(): hash_parser = argparse.ArgumentParser(add_help=False) hash_parser.add_argument( @@ -425,38 +400,22 @@ def main(): 'patch_name', metavar='STR', nargs='?', help='substring to search for patches by name', ) - help_parser = argparse.ArgumentParser(add_help=False) - help_parser.add_argument( - '--help', action='help', help=argparse.SUPPRESS, - #help='''show this help message and exit''' - ) action_parser = argparse.ArgumentParser( prog='pwclient', - add_help=False, - formatter_class=argparse.RawDescriptionHelpFormatter, - epilog='''(apply | get | info | view | update) (-h HASH | ID [ID ...])''', - ) - action_parser.add_argument( - '--help', - #action='help', - action=_RecursiveHelpAction, - help='''Print this help text''' + epilog='Use \'pwclient --help\' for more info', ) subparsers = action_parser.add_subparsers( title='Commands', - metavar='' ) apply_parser = subparsers.add_parser( - 'apply', parents=[hash_parser, help_parser], - add_help=False, + 'apply', parents=[hash_parser], conflict_handler='resolve', help='''Apply a patch (in the current dir, using -p1)''' ) apply_parser.set_defaults(subcmd='apply') git_am_parser = subparsers.add_parser( - 'git-am', parents=[hash_parser, help_parser], - add_help=False, + 'git-am', parents=[hash_parser], conflict_handler='resolve', help='''Apply a patch to current git branch using "git am".''' ) git_am_parser.set_defaults(subcmd='git_am') @@ -466,38 +425,32 @@ def main(): help='''pass --signoff to git-am''' ) get_parser = subparsers.add_parser( - 'get', parents=[hash_parser, help_parser], - add_help=False, + 'get', parents=[hash_parser], conflict_handler='resolve', help='''Download a patch and save it locally''' ) get_parser.set_defaults(subcmd='get') info_parser = subparsers.add_parser( - 'info', parents=[hash_parser, help_parser], - add_help=False, + 'info', parents=[hash_parser], conflict_handler='resolve', help='''Display patchwork info about a given patch ID''' ) info_parser.set_defaults(subcmd='info') projects_parser = subparsers.add_parser( 'projects', - add_help=False, help='''List all projects''' ) projects_parser.set_defaults(subcmd='projects') states_parser = subparsers.add_parser( 'states', - add_help=False, help='''Show list of potential patch states''' ) states_parser.set_defaults(subcmd='states') view_parser = subparsers.add_parser( - 'view', parents=[hash_parser, help_parser], - add_help=False, + 'view', parents=[hash_parser], conflict_handler='resolve', help='''View a patch''' ) view_parser.set_defaults(subcmd='view') update_parser = subparsers.add_parser( - 'update', parents=[hash_parser, help_parser], - add_help=False, + 'update', parents=[hash_parser], conflict_handler='resolve', help='''Update patch''', epilog='''Using a COMMIT-REF allows for only one ID to be specified''', ) @@ -515,17 +468,15 @@ def main(): ) update_parser.set_defaults(subcmd='update') list_parser = subparsers.add_parser("list", - add_help=False, #aliases=['search'], - parents=[filter_parser, help_parser], + parents=[filter_parser], help='''List patches, using the optional filters specified below and an optional substring to search for patches by name''' ) list_parser.set_defaults(subcmd='list') search_parser = subparsers.add_parser("search", - add_help=False, - parents=[filter_parser, help_parser], + parents=[filter_parser], help='''Alias for "list"''' ) # Poor man's argparse aliases: