Skip to content

Commit

Permalink
update tiny app
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Oct 21, 2024
1 parent 939a1fc commit b9d42f7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 31 deletions.
86 changes: 58 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,23 @@ A simple cli-app can be:
package main

import (
logz "github.com/hedzr/logg/slog"
"context"
"io"
"os"

"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"
)

func main() {
ctx := context.Background()
app := prepareApp(
cmdr.WithStore(store.New()), // use a option store explicitly, or a dummy store by default
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
Expand All @@ -156,45 +161,54 @@ func main() {
}
}

func prepareApp() (app cli.App) {
app = cmdr.New().
Info("demo-app", "0.3.1").
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").
OnMatched(func(f *cli.Flag, position int, hitState *cli.MatchState) (err error) {
app.Store().Set("app.force-default-action", false)
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()

b := app.Cmd("jump").
app.Cmd("jump").
Description("jump command").
Examples(`jump example`).
Deprecated(`jump is a demo command`).
Hidden(false)

b1 := b.Cmd("to").
Description("to command").
Examples(``).
Deprecated(`v0.1.1`).
Deprecated(`v1.1.0`).
// Group(cli.UnsortedGroup).
Hidden(false).
OnAction(func(cmd *cli.Command, args []string) (err error) {
app.Store().Set("app.demo.working", dir.GetCurrentDir())
println()
println(dir.GetCurrentDir())
println()
println(app.Store().Dump())
return // handling command action here
// 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`).
// Group(cli.UnsortedGroup).
Hidden(false).
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()
})
})
b1.Flg("full", "f").
Default(false).
Description("full command").
Build()
b1.Build()

b.Build()

app.Flg("dry-run", "n").
Default(false).
Expand All @@ -205,6 +219,22 @@ func prepareApp() (app cli.App) {
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").
OnAction(func(ctx context.Context, cmd cli.Cmd, args []string) (err error) {
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("full", "f").
Default(false).
Description("full command").
Build()
})
return
}
```
Expand Down
4 changes: 2 additions & 2 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ type App interface {
// // After the closure invoked, Build() will be called implicitly.
// AddFlg(cb func(b FlagBuilder)) App

// NewCmdFrom creates a CommandBuilder from 'from' CmdS.
// NewCmdFrom creates a CommandBuilder from 'from' Cmd.
NewCmdFrom(from *CmdS, cb func(b CommandBuilder)) App
// NewFlgFrom creates a CommandBuilder from 'from' CmdS.
// NewFlgFrom creates a CommandBuilder from 'from' Cmd.
NewFlgFrom(from *CmdS, defaultValue any, cb func(b FlagBuilder)) App

Runner
Expand Down
3 changes: 3 additions & 0 deletions tiny/loop/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

func main() {}
2 changes: 1 addition & 1 deletion tiny/tiny/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func prepareApp(opts ...cli.Opt) (app cli.App) {
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").
Expand All @@ -85,7 +86,6 @@ func prepareApp(opts ...cli.Opt) (app cli.App) {
b.Flg("full", "f").
Default(false).
Description("full command").
// Group(cli.UnsortedGroup).
Build()
})
})
Expand Down

0 comments on commit b9d42f7

Please sign in to comment.