refactor command options
This commit is contained in:
parent
19c308fa33
commit
9f541e527a
@ -1,63 +0,0 @@
|
||||
package main
|
||||
|
||||
import "flag"
|
||||
|
||||
type syntaxOptions struct {
|
||||
usage string
|
||||
example string
|
||||
args []string
|
||||
positional []string
|
||||
syntax string
|
||||
syntaxFile string
|
||||
flagSet *flag.FlagSet
|
||||
}
|
||||
|
||||
func initOptions(usage, example string, args []string) *syntaxOptions {
|
||||
var o syntaxOptions
|
||||
o.usage = usage
|
||||
o.example = example
|
||||
o.args = args
|
||||
o.flagSet = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
o.flagSet.Usage = func() {}
|
||||
o.flagSet.SetOutput(werr)
|
||||
o.flagSet.StringVar(&o.syntax, "syntax-string", "", syntaxStringUsage)
|
||||
o.flagSet.StringVar(&o.syntaxFile, "syntax", "", syntaxFileUsage)
|
||||
return &o
|
||||
}
|
||||
|
||||
func flagError(fs *flag.FlagSet) {
|
||||
stderr()
|
||||
stderr("Options:")
|
||||
fs.PrintDefaults()
|
||||
}
|
||||
|
||||
func (o *syntaxOptions) parse() (exit int) {
|
||||
if err := o.flagSet.Parse(o.args); err != nil {
|
||||
flagError(o.flagSet)
|
||||
exit = -1
|
||||
}
|
||||
|
||||
o.positional = o.flagSet.Args()
|
||||
return
|
||||
}
|
||||
|
||||
func (o *syntaxOptions) help() {
|
||||
stdout(o.usage)
|
||||
stdout()
|
||||
stdout("Options:")
|
||||
o.flagSet.SetOutput(wout)
|
||||
o.flagSet.PrintDefaults()
|
||||
stdout()
|
||||
stdout(o.example)
|
||||
stdout()
|
||||
stdout(docRef)
|
||||
}
|
||||
|
||||
func (o *syntaxOptions) checkHelp() bool {
|
||||
if len(o.args) == 0 || o.args[0] != "-help" {
|
||||
return false
|
||||
}
|
||||
|
||||
o.help()
|
||||
return true
|
||||
}
|
@ -1,15 +1,27 @@
|
||||
package main
|
||||
|
||||
type checkOptions struct {
|
||||
command *commandOptions
|
||||
syntax *fileOptions
|
||||
}
|
||||
|
||||
func checkSyntax(args []string) int {
|
||||
options := initOptions(checkSyntaxUsage, checkSyntaxExample, args)
|
||||
if options.checkHelp() {
|
||||
var o checkOptions
|
||||
o.command = initOptions(checkSyntaxUsage, checkSyntaxExample, args)
|
||||
o.syntax = &fileOptions{flagSet: o.command.flagSet}
|
||||
|
||||
o.command.flagSet.StringVar(&o.syntax.inline, "syntax-string", "", syntaxStringUsage)
|
||||
o.command.flagSet.StringVar(&o.syntax.fileName, "syntax", "", syntaxFileUsage)
|
||||
|
||||
if o.command.checkHelp() {
|
||||
return 0
|
||||
}
|
||||
|
||||
if code := options.parse(); code != 0 {
|
||||
if code := o.command.parseArgs(); code != 0 {
|
||||
return code
|
||||
}
|
||||
|
||||
_, code := openSyntax(options)
|
||||
o.syntax.positional = o.command.flagSet.Args()
|
||||
_, code := openSyntax(o.syntax)
|
||||
return code
|
||||
}
|
||||
|
@ -3,35 +3,41 @@ package main
|
||||
import "github.com/aryszka/treerack"
|
||||
|
||||
type generateOptions struct {
|
||||
*syntaxOptions
|
||||
command *commandOptions
|
||||
syntax *fileOptions
|
||||
packageName string
|
||||
export bool
|
||||
}
|
||||
|
||||
func generate(args []string) int {
|
||||
var options generateOptions
|
||||
options.syntaxOptions = initOptions(generateUsage, generateExample, args)
|
||||
options.flagSet.BoolVar(&options.export, "export", false, exportUsage)
|
||||
options.flagSet.StringVar(&options.packageName, "package-name", "", packageNameUsage)
|
||||
var o generateOptions
|
||||
o.command = initOptions(generateUsage, generateExample, args)
|
||||
o.syntax = &fileOptions{flagSet: o.command.flagSet}
|
||||
|
||||
if options.checkHelp() {
|
||||
o.command.flagSet.BoolVar(&o.export, "export", false, exportUsage)
|
||||
o.command.flagSet.StringVar(&o.packageName, "package-name", "", packageNameUsage)
|
||||
o.command.flagSet.StringVar(&o.syntax.inline, "syntax-string", "", syntaxStringUsage)
|
||||
o.command.flagSet.StringVar(&o.syntax.fileName, "syntax", "", syntaxFileUsage)
|
||||
|
||||
if o.command.checkHelp() {
|
||||
return 0
|
||||
}
|
||||
|
||||
if code := options.parse(); code != 0 {
|
||||
if code := o.command.parseArgs(); code != 0 {
|
||||
return code
|
||||
}
|
||||
|
||||
var goptions treerack.GeneratorOptions
|
||||
goptions.PackageName = options.packageName
|
||||
goptions.Export = options.export
|
||||
|
||||
s, code := openSyntax(options.syntaxOptions)
|
||||
o.syntax.positional = o.command.flagSet.Args()
|
||||
s, code := openSyntax(o.syntax)
|
||||
if code != 0 {
|
||||
return code
|
||||
}
|
||||
|
||||
if err := s.Generate(goptions, wout); err != nil {
|
||||
var g treerack.GeneratorOptions
|
||||
g.PackageName = o.packageName
|
||||
g.Export = o.export
|
||||
|
||||
if err := s.Generate(g, wout); err != nil {
|
||||
stderr(err)
|
||||
return -1
|
||||
}
|
||||
|
@ -4,12 +4,20 @@ import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/aryszka/treerack"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
|
||||
type fileOptions struct {
|
||||
inline string
|
||||
fileName string
|
||||
positional []string
|
||||
flagSet *flag.FlagSet
|
||||
}
|
||||
|
||||
func multipleSyntaxesError(fs *flag.FlagSet) {
|
||||
stderr("only one of syntax file or syntax string is allowed")
|
||||
stderr()
|
||||
@ -24,7 +32,7 @@ func missingSyntaxError(fs *flag.FlagSet) {
|
||||
fs.PrintDefaults()
|
||||
}
|
||||
|
||||
func getSource(options *syntaxOptions) (hasInput bool, fileName string, syntax string, code int) {
|
||||
func getSource(options *fileOptions) (hasInput bool, fileName string, syntax string, code int) {
|
||||
if len(options.positional) > 1 {
|
||||
multipleSyntaxesError(options.flagSet)
|
||||
code = -1
|
||||
@ -32,8 +40,8 @@ func getSource(options *syntaxOptions) (hasInput bool, fileName string, syntax s
|
||||
}
|
||||
|
||||
hasPositional := len(options.positional) == 1
|
||||
hasFile := options.syntaxFile != ""
|
||||
hasSyntax := options.syntax != ""
|
||||
hasFile := options.fileName != ""
|
||||
hasSyntax := options.inline != ""
|
||||
|
||||
var has bool
|
||||
for _, h := range []bool{hasPositional, hasFile, hasSyntax} {
|
||||
@ -51,10 +59,10 @@ func getSource(options *syntaxOptions) (hasInput bool, fileName string, syntax s
|
||||
fileName = options.positional[0]
|
||||
return
|
||||
case hasFile:
|
||||
fileName = options.syntaxFile
|
||||
fileName = options.fileName
|
||||
return
|
||||
case hasSyntax:
|
||||
syntax = options.syntax
|
||||
syntax = options.inline
|
||||
return
|
||||
}
|
||||
|
||||
@ -69,15 +77,15 @@ func getSource(options *syntaxOptions) (hasInput bool, fileName string, syntax s
|
||||
return
|
||||
}
|
||||
|
||||
func openSyntax(options *syntaxOptions) (*treerack.Syntax, int) {
|
||||
func open(options *fileOptions) (io.ReadCloser, int) {
|
||||
hasInput, fileName, syntax, code := getSource(options)
|
||||
if code != 0 {
|
||||
return nil, code
|
||||
}
|
||||
|
||||
var input io.Reader
|
||||
var r io.ReadCloser
|
||||
if hasInput {
|
||||
input = rin
|
||||
r = ioutil.NopCloser(rin)
|
||||
} else if fileName != "" {
|
||||
f, err := os.Open(fileName)
|
||||
if err != nil {
|
||||
@ -85,10 +93,18 @@ func openSyntax(options *syntaxOptions) (*treerack.Syntax, int) {
|
||||
return nil, -1
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
input = f
|
||||
r = f
|
||||
} else {
|
||||
input = bytes.NewBufferString(syntax)
|
||||
r = ioutil.NopCloser(bytes.NewBufferString(syntax))
|
||||
}
|
||||
|
||||
return r, 0
|
||||
}
|
||||
|
||||
func openSyntax(options *fileOptions) (*treerack.Syntax, int) {
|
||||
input, code := open(options)
|
||||
if code != 0 {
|
||||
return nil, code
|
||||
}
|
||||
|
||||
s := &treerack.Syntax{}
|
||||
|
62
cmd/treerack/options.go
Normal file
62
cmd/treerack/options.go
Normal file
@ -0,0 +1,62 @@
|
||||
package main
|
||||
|
||||
import "flag"
|
||||
|
||||
type commandOptions struct {
|
||||
usage string
|
||||
example string
|
||||
args []string
|
||||
flagSet *flag.FlagSet
|
||||
}
|
||||
|
||||
func initOptions(usage, example string, args []string) *commandOptions {
|
||||
var o commandOptions
|
||||
|
||||
o.usage = usage
|
||||
o.example = example
|
||||
o.args = args
|
||||
|
||||
o.flagSet = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
o.flagSet.Usage = func() {}
|
||||
o.flagSet.SetOutput(werr)
|
||||
|
||||
return &o
|
||||
}
|
||||
|
||||
func (o *commandOptions) flagError() {
|
||||
stderr()
|
||||
stderr("Options:")
|
||||
o.flagSet.PrintDefaults()
|
||||
}
|
||||
|
||||
func (o *commandOptions) parseArgs() (exit int) {
|
||||
if err := o.flagSet.Parse(o.args); err != nil {
|
||||
o.flagError()
|
||||
exit = -1
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (o *commandOptions) help() {
|
||||
stdout(o.usage)
|
||||
stdout()
|
||||
|
||||
stdout("Options:")
|
||||
o.flagSet.SetOutput(wout)
|
||||
o.flagSet.PrintDefaults()
|
||||
|
||||
stdout()
|
||||
stdout(o.example)
|
||||
stdout()
|
||||
stdout(docRef)
|
||||
}
|
||||
|
||||
func (o *commandOptions) checkHelp() bool {
|
||||
if len(o.args) == 0 || o.args[0] != "-help" {
|
||||
return false
|
||||
}
|
||||
|
||||
o.help()
|
||||
return true
|
||||
}
|
@ -1,45 +1,5 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"io"
|
||||
)
|
||||
|
||||
type parseOptions struct {
|
||||
syntaxOptions
|
||||
input string
|
||||
inputFile string
|
||||
pretty bool
|
||||
indent string
|
||||
}
|
||||
|
||||
func flagSetParse(o *parseOptions, output io.Writer) *flag.FlagSet {
|
||||
fs := flag.NewFlagSet("", flag.ContinueOnError)
|
||||
fs.Usage = func() {}
|
||||
fs.SetOutput(output)
|
||||
fs.StringVar(&o.syntax, "syntax-string", "", syntaxStringUsage)
|
||||
fs.StringVar(&o.syntaxFile, "syntax", "", syntaxFileUsage)
|
||||
return fs
|
||||
}
|
||||
|
||||
func flagErrorParse(fs *flag.FlagSet) {
|
||||
stderr()
|
||||
stderr("Options:")
|
||||
fs.PrintDefaults()
|
||||
}
|
||||
|
||||
func helpParse() {
|
||||
stdout(parseUsage)
|
||||
stdout()
|
||||
stdout("Options:")
|
||||
fs := flagSetParse(&parseOptions{}, wout)
|
||||
fs.PrintDefaults()
|
||||
stdout()
|
||||
stdout(parseExample)
|
||||
stdout()
|
||||
stdout(docRef)
|
||||
}
|
||||
|
||||
func parse(args []string) int {
|
||||
return 0
|
||||
}
|
||||
|
1152
self/self.go
1152
self/self.go
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user