Make AsCommandLine() more parsimonious
This commit is contained in:
parent
6ac1ccd378
commit
24aa1f171c
2 changed files with 21 additions and 11 deletions
|
|
@ -530,7 +530,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())
|
||||
ser.append('\n'.join(opt.as_string_for_commandline()))
|
||||
if ac is not None:
|
||||
print(''.join(ac.as_go_code('ans.ArgCompleter', ' = ')))
|
||||
if not kcd:
|
||||
|
|
@ -544,8 +544,10 @@ def kitten_clis() -> None:
|
|||
print('\n'.join(od))
|
||||
print('}')
|
||||
print('func (opts Options) AsCommandLine() (ans []string) {')
|
||||
for x in ser:
|
||||
print('\t' + x)
|
||||
if ser:
|
||||
print('\t sval := ""')
|
||||
for x in ser:
|
||||
print('\t' + x)
|
||||
print('return')
|
||||
print('}')
|
||||
|
||||
|
|
|
|||
24
kitty/cli.py
24
kitty/cli.py
|
|
@ -85,21 +85,29 @@ def as_option(self, cmd_name: str = 'cmd', depth: int = 0, group: str = '') -> s
|
|||
ans += f'\n\tDefault: "{serialize_as_go_string(self.default)}",\n'
|
||||
return ans + '})'
|
||||
|
||||
def as_string_for_commandline(self) -> str:
|
||||
def as_string_for_commandline(self) -> Iterator[str]:
|
||||
# }}}}}}}}}}}]]]]]]]]]]]]]]]]]
|
||||
flag = self.flags[0]
|
||||
val = f'opts.{self.go_var_name}'
|
||||
if self.go_type == '[]string':
|
||||
yield f'\tfor _, x := range {val} {{ ans = append(ans, `{flag}=` + x) }}'
|
||||
return
|
||||
match self.go_type:
|
||||
case 'bool':
|
||||
val = f'fmt.Sprintf(`%#v`, {val})'
|
||||
yield f'sval = fmt.Sprintf(`%#v`, {val})'
|
||||
godef = '`true`' if self.type != 'bool-set' else '`false`'
|
||||
case 'int':
|
||||
val = f'fmt.Sprintf(`%d`, {val})'
|
||||
yield f'sval = fmt.Sprintf(`%d`, {val})'
|
||||
godef = f"`{self.default or '0'}`"
|
||||
case 'string':
|
||||
return f'if {val} != "" {{ ans = append(ans, `{flag}=` + {val}) }}'
|
||||
yield f'sval = {val}'
|
||||
godef = f'''"{serialize_as_go_string(self.default or '')}"'''
|
||||
case 'float64':
|
||||
val = f'fmt.Sprintf(`%f`, {val})'
|
||||
case '[]string':
|
||||
return f'for _, x := range {val} {{ ans = append(ans, `{flag}=` + x) }}'
|
||||
return f'ans = append(ans, `{flag}=` + {val})'
|
||||
yield f'sval = fmt.Sprintf(`%f`, {val})'
|
||||
godef = f"`{self.default or '0'}`"
|
||||
case _:
|
||||
raise ValueError(f'Unknown type: {self.go_type}')
|
||||
yield f'\tif (sval != {godef}) {{ ans = append(ans, `{flag}=` + sval)}}'
|
||||
|
||||
@property
|
||||
def sorted_choices(self) -> list[str]:
|
||||
|
|
|
|||
Loading…
Reference in a new issue