Skip to content

Commit

Permalink
chore: update the cflag pkg readme, add more usage
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 23, 2022
1 parent d4d8f51 commit 4b5e559
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 25 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ func IsZeroValue(opt *flag.Flag, value string) (bool, bool)
func AddPrefix(name string) string
func AddPrefixes(name string, shorts []string) string
```
#### `cflag` Usage

`cflag` usage please see [cflag/README.md](cflag/README.md)

### CLI/Console

Expand Down
4 changes: 4 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func AddPrefix(name string) string
func AddPrefixes(name string, shorts []string) string
```

#### `cflag` Usage

`cflag` usage please see [cflag/README.md](cflag/README.md)

### CLI/Console

> Package `github.com/gookit/goutil/cliutil`
Expand Down
76 changes: 65 additions & 11 deletions cflag/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ go get github.com/gookit/goutil/cflag

## Usage

Examples, code please see [_example/cmd.go](_example/cmd.go)

```go
package main

Expand All @@ -24,18 +26,18 @@ import (
"github.com/gookit/goutil/cliutil"
)

var opts = struct {
age int
name string
str1 string
lOpt string
bol bool
}{}

// go run ./_example/cmd.go
// go run ./cflag/_example/cmd.go -h
// go run ./cflag/_example/cmd.go --name inhere ab cd
// go run ./cflag/_example/cmd.go --name inhere --lo val ab cd
func main() {
opts := struct {
age int
name string
str1 string
lOpt string
bol bool
}{}

c := cflag.New(func(c *cflag.CFlags) {
c.Desc = "this is a demo command"
c.Version = "0.5.1"
Expand All @@ -49,13 +51,14 @@ func main() {
c.AddArg("arg2", "this is arg2", true, nil)

c.Func = func(c *cflag.CFlags) error {
cliutil.Infoln("hello, this is command:", c.Name())
cliutil.Magentaln("hello, this is command:", c.Name())
cliutil.Infoln("option.age =", opts.age)
cliutil.Infoln("option.name =", opts.name)
cliutil.Infoln("option.str1 =", opts.str1)
cliutil.Infoln("option.lOpt =", opts.lOpt)
cliutil.Infoln("arg1 =", c.Arg("arg1").String())
cliutil.Infoln("arg2 =", c.Arg("arg2").String())
cliutil.Infoln("remain args =", c.RemainArgs())

return nil
}
Expand All @@ -65,6 +68,57 @@ func main() {
}
```

Show help:
### Set required and shorts

Can be set required and shorts on option usage string.

**Format**:

- format1: `desc;required`
- format2: `desc;required;shorts`
- required: a bool string. mark option is required
- True: `true,on,yes`
- False: `false,off,no,''`
- shorts: shortcut names for option, allow multi values, split by comma `,`

**Examples**:

```go
// set option 'name' is required
c.StringVar(&opts.name, "name", "", "this is a string option and required;true")
// set option 'str1' shorts: s
c.StringVar(&opts.str1, "str1", "def-val", "this is a string option with default value;;s")
```

## Show help

```shell
go run ./cflag/_example/cmd.go -h
```

Output:

![cmd-help](_example/cmd-help.png)

## Run command

```shell
go run ./cflag/_example/cmd.go --name inhere -a 12 --lo val ab cd
go run ./cflag/_example/cmd.go --name inhere -a 12 --lo val ab cd de fg
```

Output:

![cmd-run](_example/cmd-run.png)

## Check required

```shell
go run ./cflag/_example/cmd.go -a 22
go run ./cflag/_example/cmd.go --name inhere
```

Output:

![cmd-required.png](_example/cmd-required.png)

Binary file added cflag/_example/cmd-required.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cflag/_example/cmd-run.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 18 additions & 14 deletions cflag/_example/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (
"github.com/gookit/goutil/cliutil"
)

var opts = struct {
age int
name string
str1 string
lOpt string
bol bool
}{}

// go run ./_example/cmd.go
// go run ./cflag/_example/cmd.go -h
// go run ./cflag/_example/cmd.go --name inhere ab cd
// go run ./cflag/_example/cmd.go --name inhere -a 12 --lo val ab cd
func main() {
opts := struct {
age int
name string
str1 string
lOpt string
bol bool
}{}

c := cflag.New(func(c *cflag.CFlags) {
c.Desc = "this is a demo command"
c.Version = "0.5.1"
Expand All @@ -33,14 +33,18 @@ func main() {
// c.AddArg("arg2", "this is arg2", false, "def-val")

c.Func = func(c *cflag.CFlags) error {
cliutil.Infoln("hello, this is command:", c.Name())
cliutil.Infoln("option.age =", opts.age)
cliutil.Infoln("option.name =", opts.name)
cliutil.Infoln("option.str1 =", opts.str1)
cliutil.Infoln("option.lOpt =", opts.lOpt)
cliutil.Magentaln("hello, this is command:", c.Name())
cliutil.Yellowln("option values:")
cliutil.Infoln("opts.age =", opts.age)
cliutil.Infoln("opts.name =", opts.name)
cliutil.Infoln("opts.str1 =", opts.str1)
cliutil.Infoln("opts.lOpt =", opts.lOpt)
cliutil.Yellowln("argument values:")
cliutil.Infoln("arg1 =", c.Arg("arg1").String())
cliutil.Infoln("arg2 =", c.Arg("arg2").String())

cliutil.Infoln("\nremain args =", c.RemainArgs())

return nil
}

Expand Down
3 changes: 3 additions & 0 deletions internal/template/part-cflag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#### `cflag` Usage

`cflag` usage please see [cflag/README.md](cflag/README.md)

0 comments on commit 4b5e559

Please sign in to comment.