1
0
mirror of https://github.com/craigerl/aprsd.git synced 2024-11-10 10:33:31 -05:00

Ensure common params are honored

This commit is contained in:
Hemna 2021-11-05 16:26:24 -04:00
parent 8287c09ce5
commit 9187b9781a
2 changed files with 48 additions and 3 deletions

View File

@ -34,9 +34,9 @@ import click_completion
# local imports here
import aprsd
from aprsd import cli_helper
from aprsd import config as aprsd_config
from aprsd import messaging, packets, stats, threads, utils
from aprsd.cli_helper import AliasedGroup
# setup the global logger
@ -56,9 +56,10 @@ def custom_startswith(string, incomplete):
click_completion.core.startswith = custom_startswith
click_completion.init()
cli_initialized = 0
@click.group(cls=AliasedGroup, context_settings=CONTEXT_SETTINGS)
@click.group(cls=cli_helper.GroupWithCommandOptions, context_settings=CONTEXT_SETTINGS)
@click.option(
"--loglevel",
default="INFO",
@ -87,12 +88,17 @@ click_completion.init()
@click.version_option()
@click.pass_context
def cli(ctx, loglevel, config_file, quiet):
global cli_initialized
# Have to do the global crap because the cli_helper GroupWithCommandOptions
# ends up calling this twice.
ctx.ensure_object(dict)
ctx.obj["loglevel"] = loglevel
ctx.obj["config_file"] = config_file
ctx.obj["quiet"] = quiet
ctx.obj["config"] = aprsd_config.parse_config(config_file)
setup_logging(ctx.obj["config"], loglevel, quiet)
if not cli_initialized:
setup_logging(ctx.obj["config"], loglevel, quiet)
cli_initialized = 1
def main():

View File

@ -33,3 +33,42 @@ class AliasedGroup(click.Group):
self.add_command(cmd, name=alias)
return cmd
return decorator
class GroupWithCommandOptions(click.Group):
""" Allow application of options to group with multi command """
def add_command(self, cmd, name=None):
click.Group.add_command(self, cmd, name=name)
# add the group parameters to the command
for param in self.params:
cmd.params.append(param)
# hook the commands invoke with our own
cmd.invoke = self.build_command_invoke(cmd.invoke)
self.invoke_without_command = True
def build_command_invoke(self, original_invoke):
def command_invoke(ctx):
""" insert invocation of group function """
# separate the group parameters
ctx.obj = dict(_params={})
for param in self.params:
name = param.name
if name in ctx.params:
ctx.obj["_params"][name] = ctx.params[name]
del ctx.params[name]
# call the group function with its parameters
params = ctx.params
ctx.params = ctx.obj["_params"]
self.invoke(ctx)
ctx.params = params
# now call the original invoke(the command)
original_invoke(ctx)
return command_invoke