Automap a bunch of json fields from identically named options

This commit is contained in:
Kovid Goyal
2022-11-14 15:41:53 +05:30
parent b33a684357
commit ef71b071db
11 changed files with 42 additions and 28 deletions
+9 -4
View File
@@ -49,8 +49,7 @@ go_getter_map = {
class GoOption:
def __init__(self, cmd_name: str, x: OptionDict) -> None:
self.cmd_name = cmd_name
def __init__(self, x: OptionDict) -> None:
flags = sorted(x['aliases'], key=len)
short = ''
self.aliases = []
@@ -107,13 +106,19 @@ class GoOption:
else:
raise TypeError(f'Unknown type of CLI option: {self.type} for {self.long}')
def set_flag_value(self, cmd: str = 'cmd') -> str:
def set_flag_value(self, struct_name: str, cmd: str = 'cmd') -> str:
func = go_getter_map[self.type]
ans = f'{self.go_var_name}_temp, err := {cmd}.Flags().{func}("{self.long}")\n if err != nil {{ return err }}'
ans += f'\noptions_{self.cmd_name}.{self.go_var_name} = {self.go_var_name}_temp'
ans += f'\n{struct_name}.{self.go_var_name} = {self.go_var_name}_temp'
return ans
def go_options_for_seq(seq: 'OptionSpecSeq') -> Iterator[GoOption]:
for x in seq:
if not isinstance(x, str):
yield GoOption(x)
CONFIG_HELP = '''\
Specify a path to the configuration file(s) to use. All configuration files are
merged onto the builtin :file:`{conf_name}.conf`, overriding the builtin values.