Skip to content

Do not show global options for subcommands by default #4433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/4433.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Global options are not shown for subcommands unless explicitly requested with
`--verbose`.
28 changes: 26 additions & 2 deletions pip/basecommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,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(
Expand Down Expand Up @@ -105,6 +105,26 @@ def parse_args(self, args):
# factored out for testability
return self.parser.parse_args(args)

def print_help(self, verbose=False):
"""
Process 'help <command>' and '<command> --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)

Expand All @@ -126,6 +146,10 @@ def main(self, args):
if options.log:
root_level = "DEBUG"

if options.help:
self.print_help(options.verbose)
sys.exit(0)

logging.config.dictConfig({
"version": 1,
"disable_existing_loggers": False,
Expand Down
2 changes: 1 addition & 1 deletion pip/cmdoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def getname(n):
Option,
'-h', '--help',
dest='help',
action='help',
action='store_true',
help='Show help.')

isolated_mode = partial(
Expand Down
2 changes: 1 addition & 1 deletion pip/commands/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ def run(self, options, args):
raise CommandError(' - '.join(msg))

command = commands_dict[cmd_name]()
command.parser.print_help()
command.print_help()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe pass verbose=True here? or even options.verbose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. but I forgot how to run pip from checkout. Rebased here - #5746


return SUCCESS