Skip to content

Commit

Permalink
update tiny app / sample codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Oct 31, 2024
1 parent eefbac3 commit 07060fa
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
32 changes: 23 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ A simple cli-app can be:
```go
package main

// Simplest tiny app

import (
"context"
"io"
Expand All @@ -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

Expand All @@ -153,18 +155,21 @@ 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())
}
}

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).
Expand All @@ -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).
Expand All @@ -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
}).
Expand All @@ -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()
Expand Down
7 changes: 4 additions & 3 deletions tiny/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -54,15 +55,15 @@ 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())
}
}

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).
Expand All @@ -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).
Expand Down
28 changes: 20 additions & 8 deletions tiny/tiny/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -33,18 +33,21 @@ 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())
}
}

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).
Expand All @@ -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).
Expand All @@ -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
}).
Expand Down

0 comments on commit 07060fa

Please sign in to comment.