From 2d466f343d162730c34e3e597ef5e2bec556ce0e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 17 Aug 2022 15:38:24 +0530 Subject: [PATCH] Finish mapping of python cli opt to go cli opt --- gen-rc-go.py | 37 ++++++++++++++++++++++++++++---- kitty/cli.py | 5 ++++- kitty/rc/set_background_image.py | 1 + tools/cli/infrastructure.go | 14 ++++++++---- 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/gen-rc-go.py b/gen-rc-go.py index 87e8cd28b..17bfa7c6f 100755 --- a/gen-rc-go.py +++ b/gen-rc-go.py @@ -35,13 +35,42 @@ class Option: 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 def to_flag_definition(self, base: str = 'ans.Flags()') -> str: if self.type == 'bool-set': if self.short: return f'{base}.BoolP("{self.long}", "{self.short}", false, "{self.usage}")' return f'{base}.Bool("{self.long}", false, "{self.usage}")' - return '' + elif not self.type: + defval = f'''"{serialize_as_go_string(self.default or '')}"''' + if self.short: + return f'{base}.StringP("{self.long}", "{self.short}", {defval}, "{self.usage}")' + return f'{base}.String("{self.long}", {defval}, "{self.usage}")' + elif self.type == 'int': + if self.short: + return f'{base}.IntP("{self.long}", "{self.short}", {self.default or 0}, "{self.usage}")' + return f'{base}.Int("{self.long}", {self.default or 0}, "{self.usage}")' + elif self.type == 'float': + if self.short: + return f'{base}.Float64P("{self.long}", "{self.short}", {self.default or 0}, "{self.usage}")' + return f'{base}.Float64("{self.long}", {self.default or 0}, "{self.usage}")' + elif self.type == 'list': + defval = f'[]string{{"{serialize_as_go_string(self.default)}"}}' if self.default else '[]string{}' + if self.short: + return f'{base}.StringArrayP("{self.long}", "{self.short}", {defval}, "{self.usage}")' + return f'{base}.StringArray("{self.long}", {defval}, "{self.usage}")' + elif self.type == 'choices': + choices = sorted(self.obj_dict['choices']) + choices.remove(self.default or '') + choices.insert(0, self.default or '') + cx = ', '.join(f'"{serialize_as_go_string(x)}"' for x in choices) + if self.short: + return f'cli.ChoicesP({base}, "{self.long}", "{self.short}", "{self.usage}", {cx})' + return f'cli.Choices({base}, "{self.long}", "{self.usage}", {cx})' + else: + raise KeyError(f'Unknown type of CLI option: {self.type}') def build_go_code(name: str, cmd: RemoteCommand, seq: OptionSpecSeq, template: str) -> str: @@ -100,9 +129,9 @@ var IsFrozenBuild bool = false os.remove(dest) with open(dest, 'w') as f: f.write(code) - cp = subprocess.run('gofmt -s -w tools/cmd/at'.split()) - if cp.returncode != 0: - raise SystemExit(cp.returncode) + cp = subprocess.run('gofmt -s -w tools/cmd/at'.split()) + if cp.returncode != 0: + raise SystemExit(cp.returncode) if __name__ == '__main__': diff --git a/kitty/cli.py b/kitty/cli.py index 3e990ced1..e4a4ddfdb 100644 --- a/kitty/cli.py +++ b/kitty/cli.py @@ -277,7 +277,10 @@ def parse_option_spec(spec: Optional[str] = None) -> Tuple[OptionSpecSeq, Option else: k, v = m.group(1), m.group(2) if k == 'choices': - current_cmd['choices'] = frozenset(x.strip() for x in v.split(',')) + vals = tuple(x.strip() for x in v.split(',')) + current_cmd['choices'] = frozenset(vals) + if current_cmd['default'] is None: + current_cmd['default'] = vals[0] else: if k == 'default': current_cmd['default'] = v diff --git a/kitty/rc/set_background_image.py b/kitty/rc/set_background_image.py index dcec84f29..38b74319c 100644 --- a/kitty/rc/set_background_image.py +++ b/kitty/rc/set_background_image.py @@ -53,6 +53,7 @@ Change the configured background image which is used for new OS windows. --layout type=choices choices={layout_choices} +default=configured How the image should be displayed. A value of :code:`configured` will use the configured value. diff --git a/tools/cli/infrastructure.go b/tools/cli/infrastructure.go index b852798aa..54787080d 100644 --- a/tools/cli/infrastructure.go +++ b/tools/cli/infrastructure.go @@ -46,7 +46,7 @@ type ChoicesVal struct { type choicesVal ChoicesVal func (i *choicesVal) String() string { return ChoicesVal(*i).Choice } -func (i *choicesVal) Type() string { return "choices" } +func (i *choicesVal) Type() string { return "string" } func (i *choicesVal) Set(s string) error { (*i).Choice = s return nil @@ -56,14 +56,20 @@ func newChoicesVal(val ChoicesVal, p *ChoicesVal) *choicesVal { return (*choicesVal)(p) } -func add_choices(flags *pflag.FlagSet, p *ChoicesVal, choices []string, name string, usage string) { +func add_choices(flags *pflag.FlagSet, p *ChoicesVal, choices []string, name string, short string, usage string) { value := ChoicesVal{Choice: choices[0], allowed: choices} - flags.VarP(newChoicesVal(value, p), name, "", usage) + flags.VarP(newChoicesVal(value, p), name, short, usage) } func Choices(flags *pflag.FlagSet, name string, usage string, choices ...string) *ChoicesVal { p := new(ChoicesVal) - add_choices(flags, p, choices, name, usage) + add_choices(flags, p, choices, name, "", usage) + return p +} + +func ChoicesP(flags *pflag.FlagSet, name string, short string, usage string, choices ...string) *ChoicesVal { + p := new(ChoicesVal) + add_choices(flags, p, choices, name, short, usage) return p }