diff --git a/README.md b/README.md index 427324b..fe96be3 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,8 @@ A simple cli-app can be: ```go package main +// Simplest tiny app + import ( "context" "io" @@ -137,7 +139,7 @@ import ( ) func main() { - ctx := context.Background() + ctx := context.Background() // with cancel can be passed thru in your actions app := prepareApp( cmdr.WithStore(store.New()), // use an option store explicitly, or a dummy store by default @@ -153,10 +155,13 @@ func main() { // true for debug in developing time, it'll disable onAction on each Cmd. // for productive mode, comment this line. - cmdr.WithForceDefaultAction(true), + // The envvars FORCE_DEFAULT_ACTION & FORCE_RUN can override this. + // cmdr.WithForceDefaultAction(true), + + // cmdr.WithAutoEnvBindings(true), ) if err := app.Run(ctx); err != nil { - logz.ErrorContext(ctx, "Application Error:", "err", err) + logz.ErrorContext(ctx, "Application Error:", "err", err) // stacktrace if in debug mode/build os.Exit(app.SuggestRetCode()) } } @@ -164,7 +169,7 @@ func main() { func prepareApp(opts ...cli.Opt) (app cli.App) { app = cmdr.New(opts...). Info("tiny-app", "0.3.1"). - Author("hedzr") + Author("The Example Authors") // .Description(``).Header(``).Footer(``) // another way to disable `cmdr.WithForceDefaultAction(true)` is using // env-var FORCE_RUN=1 (builtin already). @@ -182,7 +187,7 @@ func prepareApp(opts ...cli.Opt) (app cli.App) { app.Cmd("jump"). Description("jump command"). - Examples(`jump example`). + Examples(`jump example`). // {{.AppName}}, {{.AppVersion}}, {{.DadCommands}}, {{.Commands}}, ... Deprecated(`v1.1.0`). // Group(cli.UnsortedGroup). Hidden(false). @@ -193,11 +198,20 @@ func prepareApp(opts ...cli.Opt) (app cli.App) { Examples(``). Deprecated(`v0.1.1`). OnAction(func(ctx context.Context, cmd cli.Cmd, args []string) (err error) { + // cmd.Set() == cmdr.Store(), cmd.Store() == cmdr.Store() cmd.Set().Set("app.demo.working", dir.GetCurrentDir()) println() - println(dir.GetCurrentDir()) - println() - println(app.Store().Dump()) + println(cmd.Set().WithPrefix("app.demo").MustString("working")) + + cs := cmdr.Store().WithPrefix("jump.to") + if cs.MustBool("full") { + println() + println(cmd.Set().Dump()) + } + cs2 := cmd.Store() + if cs2.MustBool("full") != cs.MustBool("full") { + logz.Panic("a bug found") + } app.SetSuggestRetCode(1) // ret code must be in 0-255 return // handling command action here }). @@ -223,7 +237,7 @@ func prepareApp(opts ...cli.Opt) (app cli.App) { Description("a wrong command to return error for testing"). // cmdline `FORCE_RUN=1 go run ./tiny wrong -d 8s` to verify this command to see the returned application error. OnAction(func(ctx context.Context, cmd cli.Cmd, args []string) (err error) { - dur := cmd.Store().MustDuration("wrong.duration") + dur := cmd.Store().MustDuration("duration") println("the duration is:", dur.String()) ec := errors.New() diff --git a/tiny/main.go b/tiny/main.go index 75ff3a9..1c265b4 100644 --- a/tiny/main.go +++ b/tiny/main.go @@ -40,6 +40,7 @@ func main() { // true for debug in developing time, it'll disable onAction on each Cmd. // for productive mode, comment this line. + // The envvars FORCE_DEFAULT_ACTION & FORCE_RUN can override this. cmdr.WithForceDefaultAction(true), cmdr.WithSortInHelpScreen(true), // default it's false @@ -54,7 +55,7 @@ func main() { // ) if err := app.Run(ctx); err != nil { - logz.ErrorContext(ctx, "Application Error:", "err", err) + logz.ErrorContext(ctx, "Application Error:", "err", err) // stacktrace if in debug mode/build os.Exit(app.SuggestRetCode()) } } @@ -62,7 +63,7 @@ func main() { func prepareApp(opts ...cli.Opt) (app cli.App) { app = cmdr.New(opts...). Info("tiny-app", "0.3.1"). - Author("hedzr") + Author("The Example Authors") // .Description(``).Header(``).Footer(``) // another way to disable `cmdr.WithForceDefaultAction(true)` is using // env-var FORCE_RUN=1 (builtin already). @@ -78,7 +79,7 @@ func prepareApp(opts ...cli.Opt) (app cli.App) { app.Cmd("jump"). Description("jump command"). - Examples(`jump example`). + Examples(`jump example`). // {{.AppName}}, {{.AppVersion}}, {{.DadCommands}}, {{.Commands}}, ... Deprecated(`v1.1.0`). // Group(cli.UnsortedGroup). Hidden(false). diff --git a/tiny/tiny/main.go b/tiny/tiny/main.go index ef9920d..163c22f 100644 --- a/tiny/tiny/main.go +++ b/tiny/tiny/main.go @@ -17,7 +17,7 @@ import ( ) func main() { - ctx := context.Background() + ctx := context.Background() // with cancel can be passed thru in your actions app := prepareApp( cmdr.WithStore(store.New()), // use an option store explicitly, or a dummy store by default @@ -33,10 +33,13 @@ func main() { // true for debug in developing time, it'll disable onAction on each Cmd. // for productive mode, comment this line. - cmdr.WithForceDefaultAction(true), + // The envvars FORCE_DEFAULT_ACTION & FORCE_RUN can override this. + // cmdr.WithForceDefaultAction(true), + + // cmdr.WithAutoEnvBindings(true), ) if err := app.Run(ctx); err != nil { - logz.ErrorContext(ctx, "Application Error:", "err", err) + logz.ErrorContext(ctx, "Application Error:", "err", err) // stacktrace if in debug mode/build os.Exit(app.SuggestRetCode()) } } @@ -44,7 +47,7 @@ func main() { func prepareApp(opts ...cli.Opt) (app cli.App) { app = cmdr.New(opts...). Info("tiny-app", "0.3.1"). - Author("hedzr") + Author("The Example Authors") // .Description(``).Header(``).Footer(``) // another way to disable `cmdr.WithForceDefaultAction(true)` is using // env-var FORCE_RUN=1 (builtin already). @@ -62,7 +65,7 @@ func prepareApp(opts ...cli.Opt) (app cli.App) { app.Cmd("jump"). Description("jump command"). - Examples(`jump example`). + Examples(`jump example`). // {{.AppName}}, {{.AppVersion}}, {{.DadCommands}}, {{.Commands}}, ... Deprecated(`v1.1.0`). // Group(cli.UnsortedGroup). Hidden(false). @@ -73,11 +76,20 @@ func prepareApp(opts ...cli.Opt) (app cli.App) { Examples(``). Deprecated(`v0.1.1`). OnAction(func(ctx context.Context, cmd cli.Cmd, args []string) (err error) { + // cmd.Set() == cmdr.Store(), cmd.Store() == cmdr.Store() cmd.Set().Set("app.demo.working", dir.GetCurrentDir()) println() - println(dir.GetCurrentDir()) - println() - println(app.Store().Dump()) + println(cmd.Set().WithPrefix("app.demo").MustString("working")) + + cs := cmdr.Store().WithPrefix("jump.to") + if cs.MustBool("full") { + println() + println(cmd.Set().Dump()) + } + cs2 := cmd.Store() + if cs2.MustBool("full") != cs.MustBool("full") { + logz.Panic("a bug found") + } app.SetSuggestRetCode(1) // ret code must be in 0-255 return // handling command action here }).