-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate_multi_flags.py
executable file
·75 lines (71 loc) · 3.25 KB
/
generate_multi_flags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
from gotypes import types, imports
res = "// Code generated by generate_multi_flags.py; DO NOT EDIT.\n"
res += "\n"
res += "package cli\n"
res += "\n"
res += "import (\n"
for pkg in imports:
res += "\t\"%s\"\n" % pkg
res += ")\n"
for (typ, name, _, _) in types:
res += "\n"
res += "// []%s\n" % typ
res += "\n"
res += "// %ssVar defines a []%s flag with specified name.\n" % (name, typ)
res += "// The argument p points to a []%s variable in which to store values of the flag.\n" % typ
res += "// The return value will be an error from the register.RegisterFlag if it\n"
res += "// failed to register the flag.\n"
res += "//\n"
res += "// If a name contains only one rune, it will be a short name, otherwise a long name.\n"
res += "// To set a short name, you pass a cli.WithShort.\n"
res += "//\n"
res += "// _ = cli.%ssVar(register, &p, \"names\", cli.WithShort(\"n\"))\n" % name
res += "//\n"
res += "// To set a long name, you pass a cli.WithLong.\n"
res += "//\n"
res += "// _ = cli.%ssVar(register, &p, \"n\", cli.WithLong(\"names\"))\n" % name
res += "//\n"
res += "// A usage may be set by passing a cli.Usage.\n"
res += "//\n"
res += "// _ = cli.%ssVar(register, &p, \"names\", cli.Usage(\"Names of users\"))\n" % name
res += "//\n"
res += "// The flag is optional by default.\n"
res += "// This may be changed by passing the cli.Required.\n"
res += "//\n"
res += "// _ = cli.%ssVar(register, &p, \"names\", cli.Required)\n" % name
res += "//\n"
res += "// All options can be used together.\n"
res += "func %ssVar(register Register, p *[]%s, name string, options ...FlagOptionApplyer) error {\n" % (name, typ)
res += "\treturn Var(register, new%sValues(p), name, options...)\n" % name
res += "}\n"
res += "\n"
res += "// %ss defines a []%s flag with specified name.\n" % (name, typ)
res += "// The return value is the address of a []%s variable that stores values of the flag.\n" % typ
res += "//\n"
res += "// If a name contains only one rune, it will be a short name, otherwise a long name.\n"
res += "// To set a short name, you pass a cli.WithShort.\n"
res += "//\n"
res += "// _ = cli.%ss(register, \"names\", cli.WithShort(\"n\"))\n" % name
res += "//\n"
res += "// To set a long name, you pass a cli.WithLong.\n"
res += "//\n"
res += "// _ = cli.%ss(register, \"n\", cli.WithLong(\"names\"))\n" % name
res += "//\n"
res += "// A usage may be set by passing a cli.Usage.\n"
res += "//\n"
res += "// _ = cli.%ss(register, \"names\", cli.Usage(\"Names of users\"))\n" % name
res += "//\n"
res += "// The flag is optional by default.\n"
res += "// This may be changed by passing the cli.Required.\n"
res += "//\n"
res += "// _ = cli.%ss(register, \"names\", cli.Required)\n" % name
res += "//\n"
res += "// All options can be used together.\n"
res += "func %ss(register Register, name string, options ...FlagOptionApplyer) *[]%s {\n" % (name, typ)
res += "\tp := new([]%s)\n" % typ
res += "\t_ = %ssVar(register, p, name, options...)\n" % name
res += "\treturn p\n"
res += "}\n"
with open("./flags_multi_gen.go", "w") as f:
f.write(res)