1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
from cement import Controller
from argparse_complete_fig import generate_completion_spec
class GenerateFigSpecController(Controller):
class Meta:
label = 'generate_fig_spec'
stacked_on = 'base'
stacked_type = 'nested'
description = 'Generate a fig completion spec for this CLI tool'
hide = True
def _pre_argument_parsing(self):
controller = self.app.controller
meta_by_parser = {}
for child in controller._controllers:
commands = child._collect_commands()
if commands:
parent = controller._get_parser_parent_by_controller(child)
for command in commands:
command_parser = parent._name_parser_map.get(
command['label']
)
meta_by_parser[command_parser] = {
"meta": command,
"type": "command"
}
if child._meta.stacked_type == "embedded":
continue
parser = controller._get_parser_by_controller(child)
meta_by_parser[parser] = {
"meta": child._meta.__dict__,
"type": "controller"
}
def subcommand_hook(subcommand, parser):
parser_meta = meta_by_parser.get(parser)
if parser_meta is not None:
meta = parser_meta["meta"]
if meta.get("hide", False):
subcommand["hidden"] = True
if meta.get("description"):
subcommand["description"] = meta["description"]
if subcommand["name"] == "generate_fig_spec":
subcommand["priority"] = 0
def arg_filter(arg):
return arg.dest in ["__dispatch__", "__controller_namespace__"]
hooks = {"subcommand": subcommand_hook}
spec = generate_completion_spec(
controller._parser,
hooks,
arg_filter,
"cement_complete_fig"
)
print(spec)
def _default(self):
pass
|