Skip to content

Commit

Permalink
update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Oct 21, 2024
1 parent 3ceb926 commit ce86932
Showing 1 changed file with 108 additions and 108 deletions.
216 changes: 108 additions & 108 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,122 +123,122 @@ A simple cli-app can be:
package main

import (
"context"
"io"
"os"
"context"
"io"
"os"

"gopkg.in/hedzr/errors.v3"
"gopkg.in/hedzr/errors.v3"

"github.com/hedzr/cmdr/v2"
"github.com/hedzr/cmdr/v2/cli"
"github.com/hedzr/cmdr/v2/pkg/dir"
logz "github.com/hedzr/logg/slog"
"github.com/hedzr/store"
"github.com/hedzr/cmdr/v2"
"github.com/hedzr/cmdr/v2/cli"
"github.com/hedzr/cmdr/v2/pkg/dir"
logz "github.com/hedzr/logg/slog"
"github.com/hedzr/store"
)

func main() {
ctx := context.Background()
app := prepareApp(
cmdr.WithStore(store.New()), // use an option store explicitly, or a dummy store by default

// cmdr.WithExternalLoaders(
// local.NewConfigFileLoader(), // import "github.com/hedzr/cmdr-loaders/local" to get in advanced external loading features
// local.NewEnvVarLoader(),
// ),

cmdr.WithTasksBeforeRun(func(ctx context.Context, cmd cli.Cmd, runner cli.Runner, extras ...any) (err error) {
logz.DebugContext(ctx, "command running...", "cmd", cmd, "runner", runner, "extras", extras)
return
}),

// true for debug in developing time, it'll disable onAction on each Cmd.
// for productive mode, comment this line.
cmdr.WithForceDefaultAction(true),
)
if err := app.Run(ctx); err != nil {
logz.ErrorContext(ctx, "Application Error:", "err", err)
os.Exit(app.SuggestRetCode())
}
ctx := context.Background()
app := prepareApp(
cmdr.WithStore(store.New()), // use an option store explicitly, or a dummy store by default

// cmdr.WithExternalLoaders(
// local.NewConfigFileLoader(), // import "github.com/hedzr/cmdr-loaders/local" to get in advanced external loading features
// local.NewEnvVarLoader(),
// ),

cmdr.WithTasksBeforeRun(func(ctx context.Context, cmd cli.Cmd, runner cli.Runner, extras ...any) (err error) {
logz.DebugContext(ctx, "command running...", "cmd", cmd, "runner", runner, "extras", extras)
return
}),

// true for debug in developing time, it'll disable onAction on each Cmd.
// for productive mode, comment this line.
cmdr.WithForceDefaultAction(true),
)
if err := app.Run(ctx); err != nil {
logz.ErrorContext(ctx, "Application Error:", "err", err)
os.Exit(app.SuggestRetCode())
}
}

func prepareApp(opts ...cli.Opt) (app cli.App) {
app = cmdr.New(opts...).
Info("tiny-app", "0.3.1").
Author("hedzr")

// another way to disable `cmdr.WithForceDefaultAction(true)` is using
// env-var FORCE_RUN=1 (builtin already).
app.Flg("no-default").
Description("disable force default action").
// Group(cli.UnsortedGroup).
OnMatched(func(f *cli.Flag, position int, hitState *cli.MatchState) (err error) {
if b, ok := hitState.Value.(bool); ok {
// disable/enable the final state about 'force default action'
f.Set().Set("app.force-default-action", b)
}
return
}).
Build()

app.Cmd("jump").
Description("jump command").
Examples(`jump example`).
Deprecated(`v1.1.0`).
// Group(cli.UnsortedGroup).
Hidden(false).
// Both With(cb) and Build() to end a building sequence
With(func(b cli.CommandBuilder) {
b.Cmd("to").
Description("to command").
Examples(``).
Deprecated(`v0.1.1`).
OnAction(func(ctx context.Context, cmd cli.Cmd, args []string) (err error) {
cmd.Set().Set("app.demo.working", dir.GetCurrentDir())
println()
println(dir.GetCurrentDir())
println()
println(app.Store().Dump())
app.SetSuggestRetCode(1) // ret code must be in 0-255
return // handling command action here
}).
With(func(b cli.CommandBuilder) {
b.Flg("full", "f").
Default(false).
Description("full command").
Build()
})
})

app.Flg("dry-run", "n").
Default(false).
Description("run all but without committing").
Build()

app.Flg("wet-run", "w").
Default(false).
Description("run all but with committing").
Build() // no matter even if you're adding the duplicated one.

app.Cmd("wrong").
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 := cmdr.CmdStore().MustDuration("wrong.duration")
println("the duration is:", dur.String())

ec := errors.New()
defer ec.Defer(&err) // store the collected errors in native err and return it
ec.Attach(io.ErrClosedPipe, errors.New("something's wrong"), os.ErrPermission)
// see the application error by running `go run ./tiny/tiny/main.go wrong`.
return
}).
With(func(b cli.CommandBuilder) {
b.Flg("duration", "d").
Default("5s").
Description("a duration var").
Build()
})
return
app = cmdr.New(opts...).
Info("tiny-app", "0.3.1").
Author("hedzr")

// another way to disable `cmdr.WithForceDefaultAction(true)` is using
// env-var FORCE_RUN=1 (builtin already).
app.Flg("no-default").
Description("disable force default action").
// Group(cli.UnsortedGroup).
OnMatched(func(f *cli.Flag, position int, hitState *cli.MatchState) (err error) {
if b, ok := hitState.Value.(bool); ok {
// disable/enable the final state about 'force default action'
f.Set().Set("app.force-default-action", b)
}
return
}).
Build()

app.Cmd("jump").
Description("jump command").
Examples(`jump example`).
Deprecated(`v1.1.0`).
// Group(cli.UnsortedGroup).
Hidden(false).
// Both With(cb) and Build() to end a building sequence
With(func(b cli.CommandBuilder) {
b.Cmd("to").
Description("to command").
Examples(``).
Deprecated(`v0.1.1`).
OnAction(func(ctx context.Context, cmd cli.Cmd, args []string) (err error) {
cmd.Set().Set("app.demo.working", dir.GetCurrentDir())
println()
println(dir.GetCurrentDir())
println()
println(app.Store().Dump())
app.SetSuggestRetCode(1) // ret code must be in 0-255
return // handling command action here
}).
With(func(b cli.CommandBuilder) {
b.Flg("full", "f").
Default(false).
Description("full command").
Build()
})
})

app.Flg("dry-run", "n").
Default(false).
Description("run all but without committing").
Build()

app.Flg("wet-run", "w").
Default(false).
Description("run all but with committing").
Build() // no matter even if you're adding the duplicated one.

app.Cmd("wrong").
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 := cmdr.CmdStore().MustDuration("wrong.duration")
println("the duration is:", dur.String())

ec := errors.New()
defer ec.Defer(&err) // store the collected errors in native err and return it
ec.Attach(io.ErrClosedPipe, errors.New("something's wrong"), os.ErrPermission)
// see the application error by running `go run ./tiny/tiny/main.go wrong`.
return
}).
With(func(b cli.CommandBuilder) {
b.Flg("duration", "d").
Default("5s").
Description("a duration var").
Build()
})
return
}
```

Expand Down

0 comments on commit ce86932

Please sign in to comment.