Work on improving CLI parsing

Can now set bool values explicitly with =
Handle multi short flag args like -abc
Add unit tests for CLI parsing
Generate go code to serialize CLI options as a cmdline

TODO: Implement setting of bool vals in C and Go parsing code
TODO: Help/rst output should somehow indicate this feature
This commit is contained in:
Kovid Goyal
2025-04-26 09:01:54 +05:30
parent 2093fb1310
commit 3f00dc1c9e
4 changed files with 182 additions and 29 deletions

View File

@@ -514,10 +514,13 @@ def kitten_clis() -> None:
with replace_if_needed(f'kittens/{kitten}/cli_generated.go'):
od = []
ser = []
kcd = kitten_cli_docs(kitten)
has_underscore = '_' in kitten
print(f'package {kitten}')
print('import "fmt"')
print('import "kitty/tools/cli"')
print('var _ = fmt.Sprintf')
print('func create_cmd(root *cli.Command, run_func func(*cli.Command, *Options, []string)(int, error)) {')
print('ans := root.AddSubCommand(&cli.Command{')
print(f'Name: "{kitten}",')
@@ -538,6 +541,7 @@ def kitten_clis() -> None:
for opt in gopts:
print(opt.as_option('ans'))
od.append(opt.struct_declaration())
ser.append(opt.as_string_for_commandline())
if ac is not None:
print(''.join(ac.as_go_code('ans.ArgCompleter', ' = ')))
if not kcd:
@@ -550,6 +554,11 @@ def kitten_clis() -> None:
print('type Options struct {')
print('\n'.join(od))
print('}')
print('func (opts Options) AsCommandLine() (ans []string) {')
for x in ser:
print('\t' + x)
print('return')
print('}')
# }}}