Skip to content

Commit

Permalink
-h 或者--help 显示默认注册的选项 (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong authored May 2, 2022
1 parent f1d2f68 commit 03d9af3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions clop.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,14 @@ func (c *Clop) genHelpMessage(h *Help) {
// shortAndLong多个key指向一个option,需要used map去重
used := make(map[*Option]struct{}, len(c.shortAndLong))

if c.shortAndLong["h"] == nil && c.shortAndLong["help"] == nil {
c.shortAndLong["h"] = &Option{usage: "print the help information", showShort: []string{"h"}, showLong: []string{"help"}}
}

if c.shortAndLong["V"] == nil && c.shortAndLong["version"] == nil {
c.shortAndLong["V"] = &Option{usage: "print version information", showShort: []string{"V"}, showLong: []string{"version"}}
}

saveHelp := func(options map[string]*Option) {
for _, v := range options {
if _, ok := used[v]; ok {
Expand Down
15 changes: 15 additions & 0 deletions clop_version_about_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ import (
"github.com/stretchr/testify/assert"
)

// 测试显示信息, 普通命令
func Test_Version_Show2(t *testing.T) {
type show struct {
Max int `clop:"short;long" usage:"max threads"`
}

var buf bytes.Buffer
cmd := New([]string{"-h"}).SetProcName("Test_Version_Show").SetVersion("v0.0.2").SetExit(false).SetOutput(&buf)
cmd.MustBind(&show{})

assert.NotEqual(t, strings.Index(buf.String(), "v0.0.2"), -1)
assert.NotEqual(t, strings.Index(buf.String(), "print version information"), -1)
assert.NotEqual(t, strings.Index(buf.String(), "print the help information"), -1)
}

// 测试显示信息
func Test_Version_Show(t *testing.T) {
var buf bytes.Buffer
Expand Down

0 comments on commit 03d9af3

Please sign in to comment.