Skip to content

Commit aacde1c

Browse files
committed
Fix issue click-contrib#17
1 parent aae753e commit aacde1c

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

click_default_group.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,37 @@ def set_default_command(self, command):
8080
def parse_args(self, ctx, args):
8181
if not args and self.default_if_no_args:
8282
args.insert(0, self.default_cmd_name)
83-
return super(DefaultGroup, self).parse_args(ctx, args)
84-
85-
def get_command(self, ctx, cmd_name):
86-
if cmd_name not in self.commands:
87-
# No command name matched.
88-
ctx.arg0 = cmd_name
89-
cmd_name = self.default_cmd_name
90-
return super(DefaultGroup, self).get_command(ctx, cmd_name)
91-
92-
def resolve_command(self, ctx, args):
93-
base = super(DefaultGroup, self)
94-
cmd_name, cmd, args = base.resolve_command(ctx, args)
95-
if hasattr(ctx, 'arg0'):
96-
args.insert(0, ctx.arg0)
97-
cmd_name = cmd.name
98-
return cmd_name, cmd, args
83+
84+
if ctx.resilient_parsing:
85+
return super(DefaultGroup, self).parse_args(ctx, args)
86+
87+
# fixup to allow help work for subcommands
88+
test_ctx = self.make_context(ctx.info_name, ctx.args, resilient_parsing=True)
89+
rest = super(DefaultGroup, self).parse_args(test_ctx, args[:])
90+
# print('parse_args:', (rest, test_ctx.protected_args, test_ctx.args))
91+
92+
help_options = self.get_help_option_names(ctx)
93+
if help_options and self.add_help_option and rest and any(s in help_options for s in rest):
94+
return super(DefaultGroup, self).parse_args(ctx, args)
95+
96+
save_allow_interspersed_args = ctx.allow_interspersed_args
97+
ctx.allow_interspersed_args = True
98+
rest = super(DefaultGroup, self).parse_args(ctx, args)
99+
ctx.allow_interspersed_args = save_allow_interspersed_args
100+
101+
if not rest and (ctx.protected_args or ['a'])[0][:1].isalnum() and not self.default_if_no_args:
102+
pass # Don't inject default_cmd_name if no command or command-specific options passed
103+
elif not ctx.protected_args:
104+
ctx.protected_args = [self.default_cmd_name]
105+
else:
106+
cmd_name = ctx.protected_args[0]
107+
cmd = self.get_command(ctx, cmd_name)
108+
if cmd is None and ctx.token_normalize_func is not None:
109+
cmd_name = ctx.token_normalize_func(cmd_name)
110+
cmd = self.get_command(ctx, cmd_name)
111+
if cmd is None:
112+
ctx.protected_args.insert(0, self.default_cmd_name)
113+
return rest
99114

100115
def format_commands(self, ctx, formatter):
101116
formatter = DefaultCommandFormatter(self, formatter, mark='*')

0 commit comments

Comments
 (0)