Handle aliased flags

This commit is contained in:
Kovid Goyal
2022-08-17 16:33:41 +05:30
parent 2ca8ae8e5f
commit a32251cab4
4 changed files with 50 additions and 12 deletions

View File

@@ -4,7 +4,7 @@
import os
import subprocess
import sys
from typing import List
from typing import Dict, List, Tuple
import kitty.constants as kc
from kitty.cli import OptionDict, OptionSpecSeq, parse_option_spec
@@ -21,22 +21,29 @@ def replace(template: str, **kw: str) -> str:
return template
go_type_map = {'bool-set': 'bool', 'int': 'int', 'float': 'float64', '': 'string', 'list': '[]string', 'choices': 'string'}
class Option:
def __init__(self, x: OptionDict) -> None:
flags = sorted(x['aliases'], key=len)
short = ''
self.aliases = []
if len(flags) > 1 and not flags[0].startswith("--"):
short = flags[0][1:]
long = flags[-1][2:]
if not long:
raise TypeError(f'No long flag for {x} with flags {flags}')
self.short, self.long = short, long
del flags[0]
self.short, self.long = short, x['name'].replace('_', '-')
for f in flags:
q = f[2:]
if q != self.long:
self.aliases.append(q)
self.usage = serialize_as_go_string(x['help'].strip())
self.type = x['type']
self.dest = x['dest']
self.default = x['default']
self.obj_dict = x
self.go_type = go_type_map[self.type]
def to_flag_definition(self, base: str = 'ans.Flags()') -> str:
if self.type == 'bool-set':
@@ -73,18 +80,33 @@ class Option:
raise TypeError(f'Unknown type of CLI option: {self.type} for {self.long}')
def render_alias_map(alias_map: Dict[str, Tuple[str, ...]]) -> str:
if not alias_map:
return ''
amap = 'switch name {\n'
for name, aliases in alias_map.items():
for alias in aliases:
amap += f'\ncase "{alias}":\nname = "{name}"\n'
amap += '}'
return amap
def build_go_code(name: str, cmd: RemoteCommand, seq: OptionSpecSeq, template: str) -> str:
template = '\n' + template[len('//go:build exclude'):]
NO_RESPONSE_BASE = 'true' if cmd.no_response else 'false'
af: List[str] = []
a = af.append
alias_map = {}
for x in seq:
if isinstance(x, str):
continue
o = Option(x)
if o.aliases:
alias_map[o.long] = tuple(o.aliases)
a(o.to_flag_definition())
if o.dest == 'no_response':
continue
ans = replace(
template,
CMD_NAME=name, __FILE__=__file__, CLI_NAME=name.replace('_', '-'),
@@ -92,6 +114,7 @@ def build_go_code(name: str, cmd: RemoteCommand, seq: OptionSpecSeq, template: s
LONG_DESC=serialize_as_go_string(cmd.desc.strip()),
NO_RESPONSE_BASE=NO_RESPONSE_BASE, ADD_FLAGS_CODE='\n'.join(af),
WAIT_TIMEOUT=str(cmd.response_timeout),
ALIAS_NORMALIZE_CODE=render_alias_map(alias_map)
)
return ans
@@ -122,7 +145,7 @@ var IsFrozenBuild bool = false
template = f.read()
for name in all_command_names():
cmd = command_for_name(name)
opts = parse_option_spec(cmd.options_spec)[0]
opts = parse_option_spec(cmd.options_spec or '\n\n')[0]
code = build_go_code(name, cmd, opts, template)
dest = f'tools/cmd/at/{name}_generated.go'
if os.path.exists(dest):