-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgenerate_args.py
executable file
·57 lines (53 loc) · 2.36 KB
/
generate_args.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
#!/usr/bin/env python
from gotypes import types, imports
res = "// Code generated by generate_args.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 += "// %sArgVar defines a %s argument with specified name.\n" % (name, typ)
res += "// The argument p points to a %s variable in which to store the value of the argument.\n" % typ
res += "// The return value will be an error from the register.RegisterArg if it\n"
res += "// failed to register the argument.\n"
res += "//\n"
res += "// A usage may be set by passing a cli.Usage.\n"
res += "//\n"
res += "// _ = cli.%sArgVar(register, &p, \"name\", cli.Usage(\"The name of user\"))\n" % name
res += "//\n"
res += "// The argument is required by default.\n"
res += "// This may be changed by passing the cli.Optional.\n"
res += "//\n"
res += "// _ = cli.%sArgVar(register, &p, \"name\", cli.Optional)\n" % name
res += "//\n"
res += "// All options can be used together.\n"
res += "func %sArgVar(register Register, p *%s, name string, options ...ArgOptionApplyer) error {\n" % (name, typ)
res += "\treturn ArgVar(register, new%sValue(p), name, options...)\n" % name
res += "}\n"
res += "\n"
res += "// %sArg defines a %s argument with specified name.\n" % (name, typ)
res += "// The return value is the address of a %s variable that stores the value of the argument.\n" % typ
res += "//\n"
res += "// A usage may be set by passing a cli.Usage.\n"
res += "//\n"
res += "// _ = cli.%sArg(register, \"name\", cli.Usage(\"The name of user\"))\n" % name
res += "//\n"
res += "// The argument is required by default.\n"
res += "// This may be changed by passing the cli.Optional.\n"
res += "//\n"
res += "// _ = cli.%sArg(register, \"name\", cli.Optional)\n" % name
res += "//\n"
res += "// All options can be used together.\n"
res += "func %sArg(register Register, name string, options ...ArgOptionApplyer) *%s {\n" % (name, typ)
res += "\tp := new(%s)\n" % typ
res += "\t_ = %sArgVar(register, p, name, options...)\n" % name
res += "\treturn p\n"
res += "}\n"
with open("./args_gen.go", "w") as f:
f.write(res)