2016-06-23 04:56:44 +00:00
|
|
|
#!/usr/bin/env python
|
2016-07-10 16:26:09 +00:00
|
|
|
"""
|
|
|
|
The flag types that ship with the cli library have many things in common, and
|
|
|
|
so we can take advantage of the `go generate` command to create much of the
|
|
|
|
source code from a list of definitions. These definitions attempt to cover
|
|
|
|
the parts that vary between flag types, and should evolve as needed.
|
|
|
|
|
|
|
|
An example of the minimum definition needed is:
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "SomeType",
|
|
|
|
"type": "sometype",
|
|
|
|
"context_default": "nil"
|
|
|
|
}
|
|
|
|
|
2016-07-11 10:01:08 +00:00
|
|
|
In this example, the code generated for the `cli` package will include a type
|
|
|
|
named `SomeTypeFlag` that is expected to wrap a value of type `sometype`.
|
|
|
|
Fetching values by name via `*cli.Context` will default to a value of `nil`.
|
2016-07-10 16:26:09 +00:00
|
|
|
|
|
|
|
A more complete, albeit somewhat redundant, example showing all available
|
|
|
|
definition keys is:
|
|
|
|
|
|
|
|
{
|
|
|
|
"name": "VeryMuchType",
|
|
|
|
"type": "*VeryMuchType",
|
|
|
|
"value": true,
|
|
|
|
"dest": false,
|
|
|
|
"doctail": " which really only wraps a []float64, oh well!",
|
|
|
|
"context_type": "[]float64",
|
2016-07-10 16:50:57 +00:00
|
|
|
"context_default": "nil",
|
|
|
|
"parser": "parseVeryMuchType(f.Value.String())",
|
|
|
|
"parser_cast": "[]float64(parsed)"
|
2016-07-10 16:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
The meaning of each field is as follows:
|
|
|
|
|
|
|
|
name (string) - The type "name", which will be suffixed with
|
|
|
|
`Flag` when generating the type definition
|
2016-07-11 10:01:08 +00:00
|
|
|
for `cli` and the wrapper type for `altsrc`
|
2016-07-15 12:45:46 +00:00
|
|
|
type (string) - The type that the generated `Flag` type for
|
2016-07-16 01:49:55 +00:00
|
|
|
`cli` is expected to "contain" as its `.Value`
|
|
|
|
member
|
2016-07-11 10:01:08 +00:00
|
|
|
value (bool) - Should the generated `cli` type have a `Value`
|
|
|
|
member?
|
|
|
|
dest (bool) - Should the generated `cli` type support a
|
|
|
|
destination pointer?
|
|
|
|
doctail (string) - Additional docs for the `cli` flag type comment
|
2016-07-10 16:26:09 +00:00
|
|
|
context_type (string) - The literal type used in the `*cli.Context`
|
|
|
|
reader func signature
|
|
|
|
context_default (string) - The literal value used as the default by the
|
|
|
|
`*cli.Context` reader funcs when no value is
|
|
|
|
present
|
2016-07-10 16:50:57 +00:00
|
|
|
parser (string) - Literal code used to parse the flag `f`,
|
|
|
|
expected to have a return signature of
|
|
|
|
(value, error)
|
|
|
|
parser_cast (string) - Literal code used to cast the `parsed` value
|
|
|
|
returned from the `parser` code
|
2016-07-10 16:26:09 +00:00
|
|
|
"""
|
2016-06-23 04:56:44 +00:00
|
|
|
|
|
|
|
from __future__ import print_function, unicode_literals
|
|
|
|
|
2016-07-10 16:26:09 +00:00
|
|
|
import argparse
|
2016-06-23 04:56:44 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2016-07-10 16:26:09 +00:00
|
|
|
import tempfile
|
2016-06-23 04:56:44 +00:00
|
|
|
import textwrap
|
|
|
|
|
|
|
|
|
2016-07-10 16:26:09 +00:00
|
|
|
class _FancyFormatter(argparse.ArgumentDefaultsHelpFormatter,
|
|
|
|
argparse.RawDescriptionHelpFormatter):
|
|
|
|
pass
|
2016-06-23 04:56:44 +00:00
|
|
|
|
|
|
|
|
2016-07-10 16:26:09 +00:00
|
|
|
def main(sysargs=sys.argv[:]):
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Generate flag type code!',
|
|
|
|
formatter_class=_FancyFormatter)
|
2016-07-11 10:01:08 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'package',
|
2016-07-11 10:02:15 +00:00
|
|
|
type=str, default='cli', choices=_WRITEFUNCS.keys(),
|
2016-07-11 10:01:08 +00:00
|
|
|
help='Package for which flag types will be generated'
|
|
|
|
)
|
2016-07-10 16:26:09 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'-i', '--in-json',
|
|
|
|
type=argparse.FileType('r'),
|
|
|
|
default=sys.stdin,
|
|
|
|
help='Input JSON file which defines each type to be generated'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-o', '--out-go',
|
|
|
|
type=argparse.FileType('w'),
|
|
|
|
default=sys.stdout,
|
|
|
|
help='Output file/stream to which generated source will be written'
|
|
|
|
)
|
|
|
|
parser.epilog = __doc__
|
|
|
|
|
|
|
|
args = parser.parse_args(sysargs[1:])
|
2016-07-11 10:01:08 +00:00
|
|
|
_generate_flag_types(_WRITEFUNCS[args.package], args.out_go, args.in_json)
|
2016-07-10 16:26:09 +00:00
|
|
|
return 0
|
2016-06-23 04:56:44 +00:00
|
|
|
|
|
|
|
|
2016-07-11 10:01:08 +00:00
|
|
|
def _generate_flag_types(writefunc, output_go, input_json):
|
2016-07-10 16:26:09 +00:00
|
|
|
types = json.load(input_json)
|
2016-06-23 04:56:44 +00:00
|
|
|
|
2016-07-10 16:26:09 +00:00
|
|
|
tmp = tempfile.NamedTemporaryFile(suffix='.go', delete=False)
|
2016-07-11 10:01:08 +00:00
|
|
|
writefunc(tmp, types)
|
2016-07-10 16:26:09 +00:00
|
|
|
tmp.close()
|
2016-06-23 04:56:44 +00:00
|
|
|
|
2016-07-10 16:26:09 +00:00
|
|
|
new_content = subprocess.check_output(
|
|
|
|
['goimports', tmp.name]
|
|
|
|
).decode('utf-8')
|
2016-06-23 04:56:44 +00:00
|
|
|
|
2016-07-10 16:26:09 +00:00
|
|
|
print(new_content, file=output_go, end='')
|
|
|
|
output_go.flush()
|
|
|
|
os.remove(tmp.name)
|
2016-06-23 04:56:44 +00:00
|
|
|
|
|
|
|
|
2016-07-11 10:01:08 +00:00
|
|
|
def _set_typedef_defaults(typedef):
|
|
|
|
typedef.setdefault('doctail', '')
|
|
|
|
typedef.setdefault('context_type', typedef['type'])
|
|
|
|
typedef.setdefault('dest', True)
|
|
|
|
typedef.setdefault('parser', 'f.Value, error(nil)')
|
|
|
|
typedef.setdefault('parser_cast', 'parsed')
|
|
|
|
|
|
|
|
|
|
|
|
def _write_cli_flag_types(outfile, types):
|
2016-06-23 04:56:44 +00:00
|
|
|
_fwrite(outfile, """\
|
|
|
|
package cli
|
|
|
|
|
|
|
|
// WARNING: This file is generated!
|
|
|
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
for typedef in types:
|
2016-07-11 10:01:08 +00:00
|
|
|
_set_typedef_defaults(typedef)
|
2016-06-23 04:56:44 +00:00
|
|
|
|
2016-07-10 16:26:09 +00:00
|
|
|
_fwrite(outfile, """\
|
|
|
|
// {name}Flag is a flag with type {type}{doctail}
|
|
|
|
type {name}Flag struct {{
|
|
|
|
Name string
|
2016-07-14 21:50:16 +00:00
|
|
|
Aliases []string
|
2016-07-10 16:26:09 +00:00
|
|
|
Usage string
|
2016-07-14 21:50:16 +00:00
|
|
|
EnvVars []string
|
2016-07-10 16:26:09 +00:00
|
|
|
Hidden bool
|
|
|
|
Value {type}
|
2016-07-14 21:50:16 +00:00
|
|
|
""".format(**typedef))
|
2016-06-23 04:56:44 +00:00
|
|
|
|
2016-07-10 16:26:09 +00:00
|
|
|
if typedef['dest']:
|
|
|
|
_fwrite(outfile, """\
|
|
|
|
Destination *{type}
|
|
|
|
""".format(**typedef))
|
2016-06-23 04:56:44 +00:00
|
|
|
|
2016-07-10 16:26:09 +00:00
|
|
|
_fwrite(outfile, "\n}\n\n")
|
2016-06-23 04:56:44 +00:00
|
|
|
|
|
|
|
_fwrite(outfile, """\
|
|
|
|
// String returns a readable representation of this value
|
|
|
|
// (for usage defaults)
|
2016-07-14 21:50:16 +00:00
|
|
|
func (f *{name}Flag) String() string {{
|
2016-06-23 04:56:44 +00:00
|
|
|
return FlagStringer(f)
|
|
|
|
}}
|
|
|
|
|
2016-07-14 21:50:16 +00:00
|
|
|
// Names returns the names of the flag
|
|
|
|
func (f *{name}Flag) Names() []string {{
|
|
|
|
return flagNames(f)
|
2016-06-23 04:56:44 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
// {name} looks up the value of a local {name}Flag, returns
|
|
|
|
// {context_default} if not found
|
|
|
|
func (c *Context) {name}(name string) {context_type} {{
|
2016-07-24 01:53:55 +00:00
|
|
|
if fs := lookupFlagSet(name, c); fs != nil {{
|
|
|
|
return lookup{name}(name, fs)
|
|
|
|
}}
|
|
|
|
return {context_default}
|
2016-06-23 04:56:44 +00:00
|
|
|
}}
|
|
|
|
|
2016-07-10 16:50:57 +00:00
|
|
|
func lookup{name}(name string, set *flag.FlagSet) {context_type} {{
|
|
|
|
f := set.Lookup(name)
|
|
|
|
if f != nil {{
|
|
|
|
parsed, err := {parser}
|
|
|
|
if err != nil {{
|
|
|
|
return {context_default}
|
|
|
|
}}
|
|
|
|
return {parser_cast}
|
|
|
|
}}
|
|
|
|
return {context_default}
|
|
|
|
}}
|
2016-06-23 04:56:44 +00:00
|
|
|
""".format(**typedef))
|
|
|
|
|
|
|
|
|
2016-07-11 10:01:08 +00:00
|
|
|
def _write_altsrc_flag_types(outfile, types):
|
|
|
|
_fwrite(outfile, """\
|
|
|
|
package altsrc
|
|
|
|
|
2016-08-28 05:36:49 +00:00
|
|
|
import "gopkg.in/urfave/cli.v2"
|
|
|
|
|
2016-07-11 10:01:08 +00:00
|
|
|
// WARNING: This file is generated!
|
|
|
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
for typedef in types:
|
|
|
|
_set_typedef_defaults(typedef)
|
|
|
|
|
|
|
|
_fwrite(outfile, """\
|
|
|
|
// {name}Flag is the flag type that wraps cli.{name}Flag to allow
|
|
|
|
// for other values to be specified
|
|
|
|
type {name}Flag struct {{
|
2016-07-14 21:50:16 +00:00
|
|
|
*cli.{name}Flag
|
2016-07-11 10:01:08 +00:00
|
|
|
set *flag.FlagSet
|
|
|
|
}}
|
|
|
|
|
|
|
|
// New{name}Flag creates a new {name}Flag
|
2016-07-14 21:50:16 +00:00
|
|
|
func New{name}Flag(fl *cli.{name}Flag) *{name}Flag {{
|
2016-07-11 10:01:08 +00:00
|
|
|
return &{name}Flag{{{name}Flag: fl, set: nil}}
|
|
|
|
}}
|
|
|
|
|
|
|
|
// Apply saves the flagSet for later usage calls, then calls the
|
|
|
|
// wrapped {name}Flag.Apply
|
|
|
|
func (f *{name}Flag) Apply(set *flag.FlagSet) {{
|
|
|
|
f.set = set
|
|
|
|
f.{name}Flag.Apply(set)
|
|
|
|
}}
|
|
|
|
""".format(**typedef))
|
|
|
|
|
|
|
|
|
2016-06-23 04:56:44 +00:00
|
|
|
def _fwrite(outfile, text):
|
|
|
|
print(textwrap.dedent(text), end='', file=outfile)
|
|
|
|
|
|
|
|
|
2016-07-11 10:01:08 +00:00
|
|
|
_WRITEFUNCS = {
|
|
|
|
'cli': _write_cli_flag_types,
|
|
|
|
'altsrc': _write_altsrc_flag_types
|
|
|
|
}
|
|
|
|
|
2016-06-23 04:56:44 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|