| 1 |
import itertools |
| 2 |
import click |
| 3 |
import json |
| 4 |
import sys |
| 5 |
|
| 6 |
def add_completion_spec_command(cli): |
| 7 |
if isinstance(cli, click.Group): |
| 8 |
command = click.Command("generate-fig-spec", callback=lambda: print(generate_completion_spec(cli))) |
| 9 |
cli.add_command(command) |
| 10 |
else: |
| 11 |
sys.exit(""" |
| 12 |
Cannot add the completion spec command to an object which is not a `click.Group` instance. |
| 13 |
- If you are tring to add the command to a `click.Command` instance use `generate_completion_spec` |
| 14 |
or convert `click.Command` to a `click.Group` instance. |
| 15 |
- Useful links: https://click.palletsprojects.com/en/8.0.x/commands/#commands-and-groups |
| 16 |
""") |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
def generate_completion_spec(cli): |
| 21 |
with click.Context(cli) as ctx: |
| 22 |
info = ctx.to_info_dict() |
| 23 |
out = generate_command(info["command"]) |
| 24 |
return get_template(out) |
| 25 |
|
| 26 |
def get_value(dict, keys): |
| 27 |
for key in keys: |
| 28 |
value = dict[key] |
| 29 |
if value: |
| 30 |
return value |
| 31 |
|
| 32 |
return None |
| 33 |
|
| 34 |
def flat_map(fn, iterables): |
| 35 |
return itertools.chain.from_iterable(map(fn, iterables)) |
| 36 |
|
| 37 |
def generate_options(option): |
| 38 |
|
| 39 |
opt = { |
| 40 |
"name": option["opts"], |
| 41 |
} |
| 42 |
|
| 43 |
if option["help"] and len(option["help"]) > 0: |
| 44 |
opt["description"] = option["help"] |
| 45 |
|
| 46 |
if option["required"]: |
| 47 |
opt["isRequired"] = True |
| 48 |
|
| 49 |
if option["multiple"]: |
| 50 |
opt["isRepeatable"] = True |
| 51 |
|
| 52 |
if not option["is_flag"]: |
| 53 |
opt["args"] = list(generate_args(option["type"], option["nargs"])) |
| 54 |
|
| 55 |
if len(option["secondary_opts"]) > 0: |
| 56 |
opt["exclusiveOn"] = option["secondary_opts"] |
| 57 |
inverse = opt.copy() |
| 58 |
inverse["name"] = option["secondary_opts"] |
| 59 |
inverse["exclusiveOn"] = opt["name"] |
| 60 |
return [ |
| 61 |
opt, |
| 62 |
inverse |
| 63 |
] |
| 64 |
|
| 65 |
return [ opt ] |
| 66 |
|
| 67 |
|
| 68 |
def generate_args(info, nargs): |
| 69 |
param_type = info["param_type"] |
| 70 |
|
| 71 |
arg = { |
| 72 |
"name": info.get("name", "argument") |
| 73 |
} |
| 74 |
|
| 75 |
if param_type == "Choice": |
| 76 |
arg["suggestions"] = info["choices"] |
| 77 |
elif param_type == "Tuple": |
| 78 |
return list(map(lambda raw: { "name": raw["name"] }, info["types"]) or []) |
| 79 |
elif param_type == "File": |
| 80 |
arg["template"] = "filepaths" |
| 81 |
elif param_type == "Path": |
| 82 |
|
| 83 |
templates = [] |
| 84 |
|
| 85 |
if info["dir_okay"]: |
| 86 |
templates.append("folders") |
| 87 |
if info["file_okay"]: |
| 88 |
templates.append("filepaths") |
| 89 |
|
| 90 |
if len(templates) > 0: |
| 91 |
arg["template"] = templates |
| 92 |
|
| 93 |
if not info["exists"]: |
| 94 |
arg["suggestCurrentToken"] = True |
| 95 |
|
| 96 |
|
| 97 |
if nargs == -1: |
| 98 |
arg["isVariadic"] = True |
| 99 |
nargs = 1 |
| 100 |
|
| 101 |
return [ arg ] * nargs |
| 102 |
|
| 103 |
|
| 104 |
def generate_command(command, integration_command_name="generate-fig-spec"): |
| 105 |
command_name = command.get("name", "") |
| 106 |
if command_name == integration_command_name: |
| 107 |
return None |
| 108 |
|
| 109 |
spec = { |
| 110 |
"name": command_name, |
| 111 |
} |
| 112 |
|
| 113 |
description = get_value(command, ["help", "short_help"]) |
| 114 |
if description and len(description) > 0: |
| 115 |
spec["description"] = description |
| 116 |
|
| 117 |
if command["deprecated"]: |
| 118 |
spec["deprecated"] = True |
| 119 |
|
| 120 |
if command["hidden"]: |
| 121 |
spec["hidden"] = True |
| 122 |
|
| 123 |
|
| 124 |
raw_options = filter(lambda param: param["param_type_name"] == "option", command.get("params", [])) |
| 125 |
|
| 126 |
options = list(flat_map(lambda raw: generate_options(raw), raw_options) or []) |
| 127 |
if len(options) > 0: |
| 128 |
spec["options"] = options |
| 129 |
|
| 130 |
|
| 131 |
raw_args = filter(lambda param: param["param_type_name"] == "argument", command.get("params", [])) |
| 132 |
args = list(flat_map(lambda raw: generate_args(raw["type"], raw["nargs"]), raw_args) or []) |
| 133 |
|
| 134 |
if len(args) > 0: |
| 135 |
spec["args"] = args |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
subcommands = list(map(lambda raw: generate_command(raw), (command.get("commands", {})).values())) |
| 140 |
if len(subcommands) > 0: |
| 141 |
spec["subcommands"] = list(filter(lambda sub: sub != None, subcommands)) |
| 142 |
|
| 143 |
return spec |
| 144 |
|
| 145 |
def get_template(spec): |
| 146 |
return """ |
| 147 |
// Autogenerated by click_complete_fig |
| 148 |
const completionSpec: Fig.Spec = {} |
| 149 |
|
| 150 |
export default completionSpec; |
| 151 |
""".format(json.dumps(spec, indent=2)) |