Skip to content

Commit

Permalink
优化出错提示信息 (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong authored Oct 25, 2022
1 parent 3d9cd3d commit 93dd7c8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ clop (Command Line Option Parse)是一款基于struct的命令行解析器,麻
- [Parsing flag code to generate clop code](#Parsing-flag-code-to-generate-clop-code)
- [Implementing linux command options](#Implementing-linux-command-options)
- [cat](#cat)
- [faq](#faq)
- [The subcommand or option does not take effect](#The-subcommand-or-option-does-not-take-effect)

## Installation

Expand Down Expand Up @@ -708,3 +710,8 @@ Args:
<files>
*/
```

## faq
### The subcommand or option does not take effect
q: 关于子命令或者命令行命令没有生效
a: 可以检查下结构体的字段是否是大写开头。
4 changes: 2 additions & 2 deletions clop.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var (
ErrDuplicateOptions = errors.New("is already in use")
//ErrUsageEmpty = errors.New("usage cannot be empty")
ErrUnsupported = errors.New("unsupported command")
ErrUnsupported = errors.New("unsupported clop command")
ErrNotFoundName = errors.New("no command line options found")
ErrOptionName = errors.New("Illegal option name")
)
Expand Down Expand Up @@ -841,7 +841,7 @@ func (c *Clop) parseTagAndSetOption(clop string, usage string, def string, field
c.envAndArgs = append(c.envAndArgs, option)

default:
return fmt.Errorf("%s:(%s) clop(%s)", ErrUnsupported, opt, clop)
return fmt.Errorf(`%s:(%s) clop:"%s", Maybe you need to clop:"short;long"`, ErrUnsupported, opt, clop)
}

if strings.HasPrefix(opt, "-") && len(name) == 0 {
Expand Down
17 changes: 16 additions & 1 deletion clop_usage_must_have_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"github.com/stretchr/testify/assert"
)

func Test_Issue(t *testing.T) {
// 该文件定位
// 各种出错的提示信息自测函数

func Test_Usage_MustHaveValue(t *testing.T) {

type test struct {
Long int `clop:"short;long" valid:"required"`
Expand Down Expand Up @@ -36,3 +39,15 @@ func Test_Issue(t *testing.T) {
assert.NotEqual(t, strings.Index(tc, "must have a value!"), -1)
}
}

func Test_Usage_UnknownCommand(t *testing.T) {
type cmd struct {
C string `clop:"C" usage:""`
}

c0 := cmd{}
var b bytes.Buffer
c := New([]string{}).SetOutput(&b).SetExit(false)
c.Bind(&c0)
assert.NotEqual(t, strings.Index(b.String(), `clop:"short;long"`), -1)
}

0 comments on commit 93dd7c8

Please sign in to comment.