Finish mapping of python cli opt to go cli opt

This commit is contained in:
Kovid Goyal 2022-08-17 15:38:24 +05:30
parent 4596dc39ce
commit 2d466f343d
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 48 additions and 9 deletions

View File

@ -35,13 +35,42 @@ class Option:
self.usage = serialize_as_go_string(x['help'].strip())
self.type = x['type']
self.dest = x['dest']
self.default = x['default']
self.obj_dict = x
def to_flag_definition(self, base: str = 'ans.Flags()') -> str:
if self.type == 'bool-set':
if self.short:
return f'{base}.BoolP("{self.long}", "{self.short}", false, "{self.usage}")'
return f'{base}.Bool("{self.long}", false, "{self.usage}")'
return ''
elif not self.type:
defval = f'''"{serialize_as_go_string(self.default or '')}"'''
if self.short:
return f'{base}.StringP("{self.long}", "{self.short}", {defval}, "{self.usage}")'
return f'{base}.String("{self.long}", {defval}, "{self.usage}")'
elif self.type == 'int':
if self.short:
return f'{base}.IntP("{self.long}", "{self.short}", {self.default or 0}, "{self.usage}")'
return f'{base}.Int("{self.long}", {self.default or 0}, "{self.usage}")'
elif self.type == 'float':
if self.short:
return f'{base}.Float64P("{self.long}", "{self.short}", {self.default or 0}, "{self.usage}")'
return f'{base}.Float64("{self.long}", {self.default or 0}, "{self.usage}")'
elif self.type == 'list':
defval = f'[]string{{"{serialize_as_go_string(self.default)}"}}' if self.default else '[]string{}'
if self.short:
return f'{base}.StringArrayP("{self.long}", "{self.short}", {defval}, "{self.usage}")'
return f'{base}.StringArray("{self.long}", {defval}, "{self.usage}")'
elif self.type == 'choices':
choices = sorted(self.obj_dict['choices'])
choices.remove(self.default or '')
choices.insert(0, self.default or '')
cx = ', '.join(f'"{serialize_as_go_string(x)}"' for x in choices)
if self.short:
return f'cli.ChoicesP({base}, "{self.long}", "{self.short}", "{self.usage}", {cx})'
return f'cli.Choices({base}, "{self.long}", "{self.usage}", {cx})'
else:
raise KeyError(f'Unknown type of CLI option: {self.type}')
def build_go_code(name: str, cmd: RemoteCommand, seq: OptionSpecSeq, template: str) -> str:
@ -100,9 +129,9 @@ var IsFrozenBuild bool = false
os.remove(dest)
with open(dest, 'w') as f:
f.write(code)
cp = subprocess.run('gofmt -s -w tools/cmd/at'.split())
if cp.returncode != 0:
raise SystemExit(cp.returncode)
cp = subprocess.run('gofmt -s -w tools/cmd/at'.split())
if cp.returncode != 0:
raise SystemExit(cp.returncode)
if __name__ == '__main__':

View File

@ -277,7 +277,10 @@ def parse_option_spec(spec: Optional[str] = None) -> Tuple[OptionSpecSeq, Option
else:
k, v = m.group(1), m.group(2)
if k == 'choices':
current_cmd['choices'] = frozenset(x.strip() for x in v.split(','))
vals = tuple(x.strip() for x in v.split(','))
current_cmd['choices'] = frozenset(vals)
if current_cmd['default'] is None:
current_cmd['default'] = vals[0]
else:
if k == 'default':
current_cmd['default'] = v

View File

@ -53,6 +53,7 @@ Change the configured background image which is used for new OS windows.
--layout
type=choices
choices={layout_choices}
default=configured
How the image should be displayed. A value of :code:`configured` will use the configured value.

View File

@ -46,7 +46,7 @@ type ChoicesVal struct {
type choicesVal ChoicesVal
func (i *choicesVal) String() string { return ChoicesVal(*i).Choice }
func (i *choicesVal) Type() string { return "choices" }
func (i *choicesVal) Type() string { return "string" }
func (i *choicesVal) Set(s string) error {
(*i).Choice = s
return nil
@ -56,14 +56,20 @@ func newChoicesVal(val ChoicesVal, p *ChoicesVal) *choicesVal {
return (*choicesVal)(p)
}
func add_choices(flags *pflag.FlagSet, p *ChoicesVal, choices []string, name string, usage string) {
func add_choices(flags *pflag.FlagSet, p *ChoicesVal, choices []string, name string, short string, usage string) {
value := ChoicesVal{Choice: choices[0], allowed: choices}
flags.VarP(newChoicesVal(value, p), name, "", usage)
flags.VarP(newChoicesVal(value, p), name, short, usage)
}
func Choices(flags *pflag.FlagSet, name string, usage string, choices ...string) *ChoicesVal {
p := new(ChoicesVal)
add_choices(flags, p, choices, name, usage)
add_choices(flags, p, choices, name, "", usage)
return p
}
func ChoicesP(flags *pflag.FlagSet, name string, short string, usage string, choices ...string) *ChoicesVal {
p := new(ChoicesVal)
add_choices(flags, p, choices, name, short, usage)
return p
}