-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate_flags.py
executable file
·75 lines (71 loc) · 3.21 KB
/
generate_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_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 += "// %sVar defines a %s flag with specified name.\n" % (name, typ)
res += "// The argument p points to a %s variable in which to store the value 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.%sVar(register, &p, \"name\", cli.WithShort(\"n\"))\n" % name
res += "//\n"
res += "// To set a long name, you pass a cli.WithLong.\n"
res += "//\n"
res += "// _ = cli.%sVar(register, &p, \"n\", cli.WithLong(\"name\"))\n" % name
res += "//\n"
res += "// A usage may be set by passing a cli.Usage.\n"
res += "//\n"
res += "// _ = cli.%sVar(register, &p, \"name\", cli.Usage(\"The name of user\"))\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.%sVar(register, &p, \"name\", cli.Required)\n" % name
res += "//\n"
res += "// All options can be used together.\n"
res += "func %sVar(register Register, p *%s, name string, options ...FlagOptionApplyer) error {\n" % (name, typ)
res += "\treturn Var(register, new%sValue(p), name, options...)\n" % name
res += "}\n"
res += "\n"
res += "// %s defines a %s flag with specified name.\n" % (name, typ)
res += "// The return value is the address of a %s variable that stores the value 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.%s(register, \"name\", cli.WithShort(\"n\"))\n" % name
res += "//\n"
res += "// To set a long name, you pass a cli.WithLong.\n"
res += "//\n"
res += "// _ = cli.%s(register, \"n\", cli.WithLong(\"name\"))\n" % name
res += "//\n"
res += "// A usage may be set by passing a cli.Usage.\n"
res += "//\n"
res += "// _ = cli.%s(register, \"name\", cli.Usage(\"The name of user\"))\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.%s(register, \"name\", cli.Required)\n" % name
res += "//\n"
res += "// All options can be used together.\n"
res += "func %s(register Register, name string, options ...FlagOptionApplyer) *%s {\n" % (name, typ)
res += "\tp := new(%s)\n" % typ
res += "\t_ = %sVar(register, p, name, options...)\n" % name
res += "\treturn p\n"
res += "}\n"
with open("./flags_gen.go", "w") as f:
f.write(res)