Skip to content

Commit

Permalink
Merge pull request #1 from wy-z/dev
Browse files Browse the repository at this point in the history
[cli] Optimize flags
  • Loading branch information
wy-z authored Sep 27, 2017
2 parents 754d6ec + 3d8bf58 commit 835a798
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,26 @@ USAGE:
tproto [global options] command [command options] [arguments...]
VERSION:
1.0.0
1.0.1
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--package PKG, --pkg PKG (required) package url PKG
--expression EXPR, --expr EXPR (required) type expression EXPR
--proto-package PP, --pp PP (required) proto package PP
--proto-file PF, --pf PF load messages from proto file PF
--ignore-json-tag ignore json tag (default: true)
--help, -h show help
--version, -v print the version
--package PKG, -p PKG package path PKG (default: ".")
--expressions EXPRS, --exprs EXPRS (required) type expressions, seperated by ',', EXPRS
--proto-package PP, --pp PP (required) proto package PP
--proto-file PF, --pf PF load messages from proto file PF
--json-tag, --jt don't ignore json tag
--help, -h show help
--version, -v print the version
```

## QuickStart

`tproto -pkg github.com/wy-z/tproto/samples -expr BasicTypes -pp samples`
`tproto -p github.com/wy-z/tproto/samples -exprs BasicTypes,NormalStruct -pp samples`
Or
`tproto -p github.com/wy-z/tproto/samples -pp samples BasicTypes NormalStruct`

## Samples

Expand Down
56 changes: 31 additions & 25 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package cli
import (
"fmt"
"os"
"strings"

"github.com/urfave/cli"
"github.com/wy-z/tproto/tproto"
)

type cliOpts struct {
PkgURL string
TypeExpr string
ProtoPkg string
ProtoFile string
IgnoreJSONTag *bool
PkgPath string
TypeExprs string
ProtoPkg string
ProtoFile string
JSONTag bool
}

//Run runs tproto
Expand All @@ -26,14 +27,15 @@ func Run(version string) {
opts := new(cliOpts)
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "package, pkg",
Usage: "(required) package url `PKG`",
Destination: &opts.PkgURL,
Name: "package, p",
Usage: "package path `PKG`",
Value: ".",
Destination: &opts.PkgPath,
},
cli.StringFlag{
Name: "expression, expr",
Usage: "(required) type expression `EXPR`",
Destination: &opts.TypeExpr,
Name: "expressions, exprs",
Usage: "(required) type expressions, seperated by ',', `EXPRS`",
Destination: &opts.TypeExprs,
},
cli.StringFlag{
Name: "proto-package, pp",
Expand All @@ -46,24 +48,23 @@ func Run(version string) {
Destination: &opts.ProtoFile,
},
cli.BoolFlag{
Name: "ignore-json-tag",
Usage: "ignore json tag (default: true)",
Destination: opts.IgnoreJSONTag,
Name: "json-tag, jt",
Usage: "don't ignore json tag",
Destination: &opts.JSONTag,
},
}
app.Action = func(c *cli.Context) (err error) {
if opts.PkgURL == "" || opts.TypeExpr == "" || opts.ProtoPkg == "" {
if c.NArg() > 0 {
opts.TypeExprs = strings.Join(c.Args(), ",")
}
if opts.TypeExprs == "" || opts.ProtoPkg == "" {
cli.ShowAppHelp(c)
return
}

parser := tproto.NewParser()
parserOpts := tproto.DefaultParserOptions
if opts.IgnoreJSONTag != nil {
parserOpts.IgnoreJSONTag = *opts.IgnoreJSONTag
} else {
parserOpts.IgnoreJSONTag = true
}
parserOpts.IgnoreJSONTag = !opts.JSONTag
parser.Options(parserOpts)

if opts.ProtoFile != "" {
Expand All @@ -74,12 +75,17 @@ func Run(version string) {
return
}
}
_, err = parser.Parse(opts.PkgURL, opts.TypeExpr)
if err != nil {
msg := fmt.Sprintf("failed to parse type expr %s: %+v", opts.TypeExpr, err)
err = cli.NewExitError(msg, 1)
return

for _, expr := range strings.Split(opts.TypeExprs, ",") {
expr = strings.TrimSpace(expr)
_, err = parser.Parse(opts.PkgPath, expr)
if err != nil {
msg := fmt.Sprintf("failed to parse type expr %s: %s", expr, err)
err = cli.NewExitError(msg, 1)
return
}
}

fmt.Println(parser.RenderProto(opts.ProtoPkg).String())
return
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/wy-z/tproto/cli"
)

const version = "1.0.0"
const version = "1.0.1"

func main() {
cli.Run(version)
Expand Down

0 comments on commit 835a798

Please sign in to comment.