From 3a87cfce3eaebebace3d80586ec3a8fa6b5aa3b3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 17 Aug 2022 09:34:03 +0530 Subject: [PATCH] Get bool set flags ported --- gen-rc-go.py | 44 ++++++++++++++++++++++++++++++++-------- tools/cmd/at/main.go | 7 +++++++ tools/cmd/at/template.go | 10 +++++++-- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/gen-rc-go.py b/gen-rc-go.py index 05a67c1c4..600d93b5e 100755 --- a/gen-rc-go.py +++ b/gen-rc-go.py @@ -4,22 +4,49 @@ import os import subprocess import sys -from typing import Any +from typing import List -from kitty.rc.base import ( - RemoteCommand, all_command_names, command_for_name, parse_subcommand_cli -) import kitty.constants as kc +from kitty.cli import OptionSpecSeq, parse_option_spec +from kitty.rc.base import ( + RemoteCommand, all_command_names, command_for_name +) def serialize_as_go_string(x: str) -> str: return x.replace('\n', '\\n').replace('"', '\\"') -def build_go_code(name: str, cmd: RemoteCommand, opts: Any, template: str) -> str: +def replace(template: str, **kw: str) -> str: + for k, v in kw.items(): + template = template.replace(k, v) + return template + + +def build_go_code(name: str, cmd: RemoteCommand, seq: OptionSpecSeq, template: str) -> str: template = '\n' + template[len('//go:build exclude'):] - ans = template.replace('CMD_NAME', name).replace('__FILE__', __file__).replace('CLI_NAME', name.replace('_', '-')).replace( - 'SHORT_DESC', serialize_as_go_string(cmd.short_desc)).replace('LONG_DESC', serialize_as_go_string(cmd.desc.strip())) + NO_RESPONSE_BASE = 'true' if cmd.no_response else 'false' + af: List[str] = [] + a = af.append + for x in seq: + if isinstance(x, str): + continue + flags = sorted(x['aliases'], key=len) + short = '' + if len(flags) > 1 and not flags[0].startswith("--"): + short = flags[0][1:] + long = flags[-1][2:] + if not long: + raise SystemExit(f'No long flag for {x} with flags {flags}') + usage = serialize_as_go_string(x['help'].strip()) + if x['type'] == 'bool-set': + a(f'add_bool_set(ans, "{long}", "{short}", "{usage}")') + ans = replace( + template, + CMD_NAME=name, __FILE__=__file__, CLI_NAME=name.replace('_', '-'), + SHORT_DESC=serialize_as_go_string(cmd.short_desc), + LONG_DESC=serialize_as_go_string(cmd.desc.strip()), + NO_RESPONSE_BASE=NO_RESPONSE_BASE, ADD_FLAGS_CODE='\n'.join(af)) return ans @@ -48,8 +75,7 @@ var RC_ENCRYPTION_PROTOCOL_VERSION string = "{kc.RC_ENCRYPTION_PROTOCOL_VERSION} template = f.read() for name in all_command_names(): cmd = command_for_name(name) - args = ['xxx' for i in range((cmd.args_count or 0) + 1)] - opts = parse_subcommand_cli(cmd, args)[0] + opts = parse_option_spec(cmd.options_spec)[0] code = build_go_code(name, cmd, opts, template) dest = f'tools/cmd/at/{name}_generated.go' if os.path.exists(dest): diff --git a/tools/cmd/at/main.go b/tools/cmd/at/main.go index 9d709e86d..70848e4e3 100644 --- a/tools/cmd/at/main.go +++ b/tools/cmd/at/main.go @@ -19,6 +19,13 @@ import ( "kitty/tools/utils" ) +func add_bool_set(cmd *cobra.Command, name string, short string, usage string) *bool { + if short == "" { + return cmd.Flags().Bool(name, false, usage) + } + return cmd.Flags().BoolP(name, short, false, usage) +} + type GlobalOptions struct { to_address, password string to_address_is_from_env_var bool diff --git a/tools/cmd/at/template.go b/tools/cmd/at/template.go index e38b43d1f..40492bc20 100644 --- a/tools/cmd/at/template.go +++ b/tools/cmd/at/template.go @@ -13,8 +13,13 @@ import ( func run_CMD_NAME(cmd *cobra.Command, args []string) (err error) { rc := utils.RemoteControlCmd{ - Cmd: "CLI_NAME", - Version: [3]int{0, 20, 0}, + Cmd: "CLI_NAME", + Version: [3]int{0, 20, 0}, + NoResponse: NO_RESPONSE_BASE, + } + nrv, err := cmd.Flags().GetBool("no-response") + if err == nil { + rc.NoResponse = nrv } err = send_rc_command(&rc) return @@ -27,6 +32,7 @@ func setup_CMD_NAME(root *cobra.Command) *cobra.Command { Long: "LONG_DESC", RunE: run_CMD_NAME, }) + ADD_FLAGS_CODE return ans }