Store parsed multi option values on the config object

This commit is contained in:
Kovid Goyal 2023-02-17 20:31:47 +05:30
parent 1470b11024
commit 32aa580984
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 30 additions and 8 deletions

View File

@ -44,7 +44,7 @@ The location on the remote host where the files needed for this kitten are
installed. Relative paths are resolved with respect to :code:`$HOME`. installed. Relative paths are resolved with respect to :code:`$HOME`.
''') ''')
opt('+copy', '', add_to_default=False, long_text=f''' opt('+copy', '', add_to_default=False, ctype='CopyInstruction', long_text=f'''
{copy_message} For example:: {copy_message} For example::
copy .vimrc .zshrc .config/some-dir copy .vimrc .zshrc .config/some-dir
@ -80,7 +80,7 @@ The login shell to execute on the remote host. By default, the remote user
account's login shell is used. account's login shell is used.
''') ''')
opt('+env', '', add_to_default=False, long_text=''' opt('+env', '', add_to_default=False, ctype='EnvInstruction', long_text='''
Specify the environment variables to be set on the remote host. Using the Specify the environment variables to be set on the remote host. Using the
name with an equal sign (e.g. :code:`env VAR=`) will set it to the empty string. name with an equal sign (e.g. :code:`env VAR=`) will set it to the empty string.
Specifying only the name (e.g. :code:`env VAR`) will remove the variable from Specifying only the name (e.g. :code:`env VAR`) will remove the variable from

View File

@ -442,7 +442,9 @@ def write_output(loc: str, defn: Definition) -> None:
f.write(f'{c}\n') f.write(f'{c}\n')
def go_type_data(parser_func: ParserFuncType, type_defs: List[str]) -> Tuple[str, str]: def go_type_data(parser_func: ParserFuncType, ctype: str) -> Tuple[str, str]:
if ctype:
return f'*{ctype}', f'New{ctype}(val)'
p = parser_func.__name__ p = parser_func.__name__
if p == 'int': if p == 'int':
return 'int64', 'strconv.ParseInt(val, 10, 64)' return 'int64', 'strconv.ParseInt(val, 10, 64)'
@ -465,11 +467,10 @@ def gen_go_code(defn: Definition) -> str:
go_parsers = {} go_parsers = {}
defaults = {} defaults = {}
multiopts = {''} multiopts = {''}
type_defs = ['']
for option in sorted(defn.iter_all_options(), key=lambda a: natural_keys(a.name)): for option in sorted(defn.iter_all_options(), key=lambda a: natural_keys(a.name)):
name = option.name.capitalize() name = option.name.capitalize()
if isinstance(option, MultiOption): if isinstance(option, MultiOption):
go_types[name], go_parsers[name] = go_type_data(option.parser_func, type_defs) go_types[name], go_parsers[name] = go_type_data(option.parser_func, option.ctype)
multiopts.add(name) multiopts.add(name)
else: else:
defaults[name] = option.parser_func(option.defval_as_string) defaults[name] = option.parser_func(option.defval_as_string)
@ -478,12 +479,10 @@ def gen_go_code(defn: Definition) -> str:
go_types[name] = f'{name}_Choice_Type' go_types[name] = f'{name}_Choice_Type'
go_parsers[name] = f'Parse_{name}(val)' go_parsers[name] = f'Parse_{name}(val)'
continue continue
go_types[name], go_parsers[name] = go_type_data(option.parser_func, type_defs) go_types[name], go_parsers[name] = go_type_data(option.parser_func, option.ctype)
for oname in choices: for oname in choices:
a(f'type {go_types[oname]} int') a(f'type {go_types[oname]} int')
for td in type_defs:
a(td)
a('type Config struct {') a('type Config struct {')
for name, gotype in go_types.items(): for name, gotype in go_types.items():
if name in multiopts: if name in multiopts:

23
tools/cmd/ssh/config.go Normal file
View File

@ -0,0 +1,23 @@
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
package ssh
import (
"fmt"
)
var _ = fmt.Print
type EnvInstruction struct {
}
type CopyInstruction struct {
}
func NewEnvInstruction(spec string) (ei *EnvInstruction, err error) {
return
}
func NewCopyInstruction(spec string) (ci *CopyInstruction, err error) {
return
}