Skip to content

Commit

Permalink
chore: update some comments and add some debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 13, 2022
1 parent b59c90b commit 690c080
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
22 changes: 11 additions & 11 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ func New(fns ...func(app *App)) *App {
// NewApp create new app instance.
//
// Usage:
// NewApp()
// // Or with a config func
// NewApp(func(a *App) {
// // do something before init ....
// a.Hooks[gcli.EvtInit] = func () {}
// })
//
// NewApp()
// // Or with a config func
// NewApp(func(a *App) {
// // do something before init ....
// a.Hooks[gcli.EvtInit] = func () {}
// })
func NewApp(fns ...func(app *App)) *App {
app := &App{
Name: "GCliApp",
Expand Down Expand Up @@ -431,11 +432,12 @@ func (app *App) RunLine(argsLine string) int {
// Run running application
//
// Usage:
//
// // run with os.Args
// app.Run(nil)
// app.Run(os.Args[1:])
// // custom args
// app.Run([]string{"cmd", ...})
// app.Run([]string{"cmd", "--name", "inhere"})
func (app *App) Run(args []string) (code int) {
// ensure application initialized
app.initialize()
Expand Down Expand Up @@ -473,6 +475,7 @@ func (app *App) Run(args []string) (code int) {
// RunCmd running an top command with custom args
//
// Usage:
//
// app.Exec("top")
// app.Exec("top", []string{"-a", "val0", "arg0"})
// // can add sub command on args
Expand All @@ -487,11 +490,7 @@ func (app *App) doRunCmd(name string, args []string) (code int) {

Debugf("will run app command '%s' with args: %v", name, args)

// parse command options
// args, err = cmd.parseOptions(args)

// do execute command
// if err := cmd.innerExecute(args, true); err != nil {
if err := cmd.innerDispatch(args); err != nil {
code = ERR
app.Fire(EvtAppRunError, err)
Expand Down Expand Up @@ -539,6 +538,7 @@ func (app *App) exitOnEnd(code int) int {
// - command path in the app. 'top sub'
//
// Usage:
//
// app.Exec("top")
// app.Exec("top:sub")
// app.Exec("top sub")
Expand Down
13 changes: 7 additions & 6 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ type Command struct {

// NewCommand create a new command instance.
// Usage:
// cmd := NewCommand("my-cmd", "description")
//
// cmd := NewCommand("my-cmd", "description")
// // OR with an config func
// cmd := NewCommand("my-cmd", "description", func(c *Command) { ... })
// app.Add(cmd) // OR cmd.AttachTo(app)
// cmd := NewCommand("my-cmd", "description", func(c *Command) { ... })
// app.Add(cmd) // OR cmd.AttachTo(app)
func NewCommand(name, desc string, fn ...func(c *Command)) *Command {
c := &Command{
Name: name,
Expand Down Expand Up @@ -249,7 +250,7 @@ func (c *Command) initialize() {

// check command name
cName := c.goodName()
Debugf("initialize the command '%s'", cName)
Debugf("initialize the command '%s': init flags, run config func", cName)

c.initialized = true
c.pathNames = append(c.pathNames, cName)
Expand Down Expand Up @@ -350,6 +351,7 @@ func (c *Command) Next() {
// MustRun Alone the current command, will panic on error
//
// Usage:
//
// // run with os.Args
// cmd.MustRun(nil)
// cmd.MustRun(os.Args[1:])
Expand All @@ -364,6 +366,7 @@ func (c *Command) MustRun(args []string) {
// Run standalone running the command
//
// Usage:
//
// // run with os.Args
// cmd.Run(nil)
// cmd.Run(os.Args[1:])
Expand Down Expand Up @@ -408,8 +411,6 @@ func (c *Command) Run(args []string) (err error) {

// dispatch execute the command
func (c *Command) innerDispatch(args []string) (err error) {
Debugf("cmd: %s - begin parse options by args: %v", c.Name, args)

// parse command flags
args, err = c.parseOptions(args)
if err != nil {
Expand Down
22 changes: 13 additions & 9 deletions gflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,10 @@ func (fs *Flags) WithConfigFn(fns ...func(cfg *FlagsConfig)) *Flags {
// Run flags parse and handle help render
//
// Usage:
// gf := gcli.NewFlags()
// ...
// gf.Run(os.Args)
//
// gf := gcli.NewFlags()
// ...
// gf.Run(os.Args)
func (fs *Flags) Run(args []string) {
if args == nil {
args = os.Args
Expand Down Expand Up @@ -202,11 +203,12 @@ func (fs *Flags) Run(args []string) {
// Parse given arguments
//
// Usage:
// gf := gcli.NewFlags()
// gf.BoolOpt(&debug, "debug", "", defDebug, "open debug mode")
// gf.UintOpt(&port, "port", "p", 18081, "the http server port")
//
// err := gf.Parse(os.Args[1:])
// gf := gcli.NewFlags()
// gf.BoolOpt(&debug, "debug", "", defDebug, "open debug mode")
// gf.UintOpt(&port, "port", "p", 18081, "the http server port")
//
// err := gf.Parse(os.Args[1:])
func (fs *Flags) Parse(args []string) (err error) {
defer func() {
if err := recover(); err != nil {
Expand All @@ -224,6 +226,7 @@ func (fs *Flags) Parse(args []string) (err error) {

if len(fs.shorts) > 0 && len(args) > 0 {
args = cflag.ReplaceShorts(args, fs.shorts)
Debugf("replace shortcuts. now, args: %v", args)
}

// do parsing
Expand Down Expand Up @@ -591,8 +594,9 @@ func (fs *Flags) Var(p flag.Value, meta *FlagMeta) { fs.varOpt(p, meta) }
// VarOpt binding a custom var option
//
// Usage:
// var names gcli.Strings
// cmd.VarOpt(&names, "tables", "t", "description ...")
//
// var names gcli.Strings
// cmd.VarOpt(&names, "tables", "t", "description ...")
func (fs *Flags) VarOpt(p flag.Value, name, shorts, desc string) {
fs.varOpt(p, newFlagMeta(name, desc, nil, shorts))
}
Expand Down

0 comments on commit 690c080

Please sign in to comment.