From 4554d092c59de7dd4cb0163b4e59ef920b755b67 Mon Sep 17 00:00:00 2001 From: anatoly techtonik Date: Sun, 27 Dec 2015 14:29:00 +0300 Subject: [PATCH 1/6] Uniform handling of 'help ' and ' --help' cases --- src/pip/_internal/cli/base_command.py | 9 +++++++++ src/pip/_internal/cli/cmdoptions.py | 2 +- src/pip/_internal/commands/help.py | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/pip/_internal/cli/base_command.py b/src/pip/_internal/cli/base_command.py index dac4b053d09..a6b2a1160c5 100644 --- a/src/pip/_internal/cli/base_command.py +++ b/src/pip/_internal/cli/base_command.py @@ -109,6 +109,11 @@ def parse_args(self, args): # factored out for testability return self.parser.parse_args(args) + def print_help(self): + # factored out for uniform handling + # of 'help ' and ' --help' cases + self.parser.print_help() + def main(self, args): options, args = self.parse_args(args) @@ -121,6 +126,10 @@ def main(self, args): user_log_file=options.log, ) + if options.help: + self.print_help() + sys.exit(0) + # TODO: Try to get these passing down from the command? # without resorting to os.environ to hold these. # This also affects isolated builds and it should. diff --git a/src/pip/_internal/cli/cmdoptions.py b/src/pip/_internal/cli/cmdoptions.py index 3033cd4b5e6..64216fd9806 100644 --- a/src/pip/_internal/cli/cmdoptions.py +++ b/src/pip/_internal/cli/cmdoptions.py @@ -106,7 +106,7 @@ def check_dist_restriction(options, check_target=False): Option, '-h', '--help', dest='help', - action='help', + action='store_true', help='Show help.', ) # type: Any diff --git a/src/pip/_internal/commands/help.py b/src/pip/_internal/commands/help.py index 49a81cbb074..4ad45c41410 100644 --- a/src/pip/_internal/commands/help.py +++ b/src/pip/_internal/commands/help.py @@ -32,6 +32,6 @@ def run(self, options, args): raise CommandError(' - '.join(msg)) command = commands_dict[cmd_name]() - command.parser.print_help() + command.print_help() return SUCCESS From e012c8414746438ec4305477db51c93b6914c8fd Mon Sep 17 00:00:00 2001 From: anatoly techtonik Date: Sun, 27 Dec 2015 14:46:46 +0300 Subject: [PATCH 2/6] `list -h' help is too long to fit one screen This omits global options for subcommands help unless explicitly requested with -v --verbose --- src/pip/_internal/cli/base_command.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/pip/_internal/cli/base_command.py b/src/pip/_internal/cli/base_command.py index a6b2a1160c5..c1baf48deef 100644 --- a/src/pip/_internal/cli/base_command.py +++ b/src/pip/_internal/cli/base_command.py @@ -63,11 +63,11 @@ def __init__(self, isolated=False): self.cmd_opts = optparse.OptionGroup(self.parser, optgroup_name) # Add the general options - gen_opts = cmdoptions.make_option_group( + self.gen_opts = cmdoptions.make_option_group( cmdoptions.general_group, self.parser, ) - self.parser.add_option_group(gen_opts) + self.parser.add_option_group(self.gen_opts) def _build_session(self, options, retries=None, timeout=None): session = PipSession( @@ -127,6 +127,15 @@ def main(self, args): ) if options.help: + if not options.verbose: + # hide General Options + self.parser.epilog = "\nAdd '-v' flag to show general "\ + "options.\n" + self.parser.option_groups.remove(self.gen_opts) + + # 'pip --help' is handled by pip.__init__.parseopt(), + # so it is not affected by absence of --verbose + self.print_help() sys.exit(0) From 0400c7295b859a89403e0c75cc8eb8c49ed973ca Mon Sep 17 00:00:00 2001 From: anatoly techtonik Date: Sun, 9 Apr 2017 15:38:18 +0300 Subject: [PATCH 3/6] Add feature file --- news/4433.feature | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 news/4433.feature diff --git a/news/4433.feature b/news/4433.feature new file mode 100644 index 00000000000..29b16a168f2 --- /dev/null +++ b/news/4433.feature @@ -0,0 +1,2 @@ +Global options are not shown for subcommands unless explicitly requested with +`--verbose`. \ No newline at end of file From d468a8ea9612a834c4a7625872443a6237f2f4f4 Mon Sep 17 00:00:00 2001 From: anatoly techtonik Date: Sat, 15 Apr 2017 10:44:12 +0300 Subject: [PATCH 4/6] Restore parser state after hiding general options --- src/pip/_internal/cli/base_command.py | 34 ++++++++++++++++----------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/pip/_internal/cli/base_command.py b/src/pip/_internal/cli/base_command.py index c1baf48deef..c0de8386338 100644 --- a/src/pip/_internal/cli/base_command.py +++ b/src/pip/_internal/cli/base_command.py @@ -109,10 +109,25 @@ def parse_args(self, args): # factored out for testability return self.parser.parse_args(args) - def print_help(self): - # factored out for uniform handling - # of 'help ' and ' --help' cases - self.parser.print_help() + def print_help(self, verbose=False): + """ + Process 'help ' and ' --help' to hide global + options unless --verbose flag is specified. + + 'pip --help' is handled separately by pip.__init__.parseopt() + """ + if verbose: + self.parser.print_help() + else: + # remove General Options group and restore after print + saveepy = self.parser.epilog + saveogr = self.parser.option_groups # it is a list + self.parser.epilog = "\nAdd '-v' flag to show general "\ + "options.\n" + self.parser.option_groups.remove(self.gen_opts) + self.parser.print_help() + self.parser.epilog = saveepy + self.parser.option_groups = saveogr def main(self, args): options, args = self.parse_args(args) @@ -127,16 +142,7 @@ def main(self, args): ) if options.help: - if not options.verbose: - # hide General Options - self.parser.epilog = "\nAdd '-v' flag to show general "\ - "options.\n" - self.parser.option_groups.remove(self.gen_opts) - - # 'pip --help' is handled by pip.__init__.parseopt(), - # so it is not affected by absence of --verbose - - self.print_help() + self.print_help(options.verbose) sys.exit(0) # TODO: Try to get these passing down from the command? From 5782a30e25fab5cb2c7fdfe06456d2cd116bb450 Mon Sep 17 00:00:00 2001 From: anatoly techtonik Date: Sat, 1 Sep 2018 21:55:08 +0300 Subject: [PATCH 5/6] Pass verbose option to `help ` --- src/pip/_internal/commands/help.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pip/_internal/commands/help.py b/src/pip/_internal/commands/help.py index 4ad45c41410..347c5f82f3b 100644 --- a/src/pip/_internal/commands/help.py +++ b/src/pip/_internal/commands/help.py @@ -32,6 +32,6 @@ def run(self, options, args): raise CommandError(' - '.join(msg)) command = commands_dict[cmd_name]() - command.print_help() + command.print_help(options.verbose) return SUCCESS From 1b75b014a6da0826f0ba65528b79b352d28b8b66 Mon Sep 17 00:00:00 2001 From: anatoly techtonik Date: Mon, 24 Sep 2018 11:40:56 +0300 Subject: [PATCH 6/6] argparse still prints newline if there are no options --- src/pip/_internal/cli/base_command.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/pip/_internal/cli/base_command.py b/src/pip/_internal/cli/base_command.py index c0de8386338..d8296723fbc 100644 --- a/src/pip/_internal/cli/base_command.py +++ b/src/pip/_internal/cli/base_command.py @@ -122,7 +122,11 @@ def print_help(self, verbose=False): # remove General Options group and restore after print saveepy = self.parser.epilog saveogr = self.parser.option_groups # it is a list - self.parser.epilog = "\nAdd '-v' flag to show general "\ + self.parser.epilog = "" + if len(saveogr) > 1: + # need newline for commands with own options + self.parser.epilog = "\n" + self.parser.epilog += "Add '-v' flag to show general "\ "options.\n" self.parser.option_groups.remove(self.gen_opts) self.parser.print_help()