Closed suntong closed 8 years ago
So easy to read json file:
func (p JSONFileParser) Parse(s string) error {
f, err := os.Open(s)
if err != nil {
return err
}
return json.NewDecoder(f).Decode(p.ptr)
}
Hmm..., agree, it's easy to read json files. I'm just thinking, from the completeness point of view, CLI should expose how to update the CLI structure from json files, giving it a standard so that everyone can following, not everyone doing it in their own ways.
BTW, you demo code only deals with json file, but you've left out how to update the CLI structure from json file right?
I add functions: ReadJSON and ReadJSONFromFile
func ReadJSON(r io.Reader, argv interface{}) error {
return json.NewDecoder(r).Decode(argv)
}
func ReadJSONFromFile(filename string, argv interface{}) error {
file, err := os.Open(filename)
if err == nil {
err = ReadJSON(file, argv)
}
return err
}
Both two can update argv
from json data.
See commit 617d581
Thank you VERY MUCH!
It doesn't really matter but it is better to close the file in ReadJSONFromFile()
.
Ref: https://github.com/go-jsonfile/jsonfile/blob/master/jsonfile.go
Note the line,
// Credits: Mkideal Wang https://github.com/mkideal/cli/issues/22
Thanks!
Hiya,
Please expose the json conf reading function, i.e., the function behind the
Self ... json:"-" parser:"jsonfile"...
support.I found myself need to deal with different json conf files for exactly the same functionality. So far, I've duplicated the CLI twice and now three times, just in order to to take advantage of the auto-magical "Self" reading feature.
Now I need to do more. So, once I can use the function to read different json conf files, I can consolidate those three (and more) sub commands into one.
Thanks