Get bool set flags ported
This commit is contained in:
parent
15ffd8e6d8
commit
3a87cfce3e
44
gen-rc-go.py
44
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):
|
||||
|
||||
@ -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
|
||||
|
||||
@ -15,6 +15,11 @@ func run_CMD_NAME(cmd *cobra.Command, args []string) (err error) {
|
||||
rc := utils.RemoteControlCmd{
|
||||
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
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user