Skip to content

Commit 5e3e55c

Browse files
committed
Fix bugs
1 parent 7d00db5 commit 5e3e55c

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

djclick/adapter.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@ def parse_args(self, args):
1414

1515
class CommandAdapter(click.Command):
1616
use_argparse = False
17+
option_list = []
1718

1819
def run_from_argv(self, argv):
1920
"""
2021
Called when run from the command line.
2122
"""
22-
return self.main(args=argv[2:])
23+
return self.main(args=argv[2:], standalone_mode=False)
2324

2425
def create_parser(self, progname, subcommand):
2526
"""
2627
Called when run through `call_command`.
2728
"""
2829
return ParserAdapter()
2930

31+
def print_help(self, prog_name, subcommand):
32+
self.main(['--help'], standalone_mode=False)
33+
3034
def map_names(self):
3135
for param in self.params:
3236
for opt in param.opts:
@@ -40,9 +44,9 @@ def execute(self, *args, **kwargs):
4044
`call_command`.
4145
"""
4246
# Remove internal Django command handling machinery
43-
kwargs.pop('skip_checks')
44-
45-
with self.make_context('', list(args)) as ctx:
47+
kwargs.pop('skip_checks', None)
48+
parent_ctx = click.get_current_context(silent=True)
49+
with self.make_context('', list(args), parent=parent_ctx) as ctx:
4650
# Rename kwargs to to the appropriate destination argument name
4751
opt_mapping = dict(self.map_names())
4852
arg_options = {opt_mapping.get(key, key): value
@@ -61,8 +65,10 @@ def register_on_context(ctx, param, value):
6165

6266

6367
def suppress_colors(ctx, param, value):
64-
if value:
65-
ctx.color = False
68+
# Only set the value if a flag was provided, otherwise we would override
69+
# the default set by the parent context if one was available.
70+
if value is not None:
71+
ctx.color = value
6672
return value
6773

6874

@@ -99,8 +105,8 @@ class CommandRegistrator(object):
99105
help='Raise on CommandError exceptions.',
100106
),
101107
click.option(
102-
'--no-color',
103-
is_flag=True,
108+
'--color/--no-color',
109+
default=None,
104110
expose_value=False,
105111
callback=suppress_colors,
106112
help='Do not colorize the command output.',
@@ -139,12 +145,15 @@ def __call__(self, func):
139145
click.command(name=self.name, cls=CommandAdapter, **self.kwargs),
140146
] + self.get_params(self.name)
141147

148+
command = func
142149
for decorator in reversed(decorators):
143-
func = decorator(func)
150+
command = decorator(command)
144151

145152
# Django expects the command to be callable (it instantiates the class
146153
# pointed at by the `Command` module-level property)...
147154
# ...let's make it happy.
148-
module.Command = lambda: func
155+
module.Command = lambda: command
149156

150-
return func
157+
# Return the execute method, as this allows us to call the command
158+
# directly (similarly as with `call_command`)
159+
return command.execute

0 commit comments

Comments
 (0)