Skip to content

Commit

Permalink
Merge pull request WindomZ#23 from WindomZ/dev
Browse files Browse the repository at this point in the history
v1.1.2
  • Loading branch information
WindomZ authored Apr 21, 2017
2 parents ee699e9 + e3aef65 commit 40b1282
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 48 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The solution for building command shell programs,
drive by <[docopt](https://github.com/docopt/docopt.go)>,
inspired by <[commander.js](https://github.com/tj/commander.js)>

![v1.0.2](https://img.shields.io/badge/version-v1.0.2-blue.svg)
![v1.1.2](https://img.shields.io/badge/version-v1.1.2-blue.svg)
![status](https://img.shields.io/badge/status-stable-green.svg)

## Features
Expand Down Expand Up @@ -273,6 +273,13 @@ $ calculator_example sum 10 , 20 , 30 , 40
# output: 100
```

## UsingList

- [gitclone](https://github.com/WindomZ/gitclone)
- [gitinit](https://github.com/WindomZ/gitinit)
- [gitdate](https://github.com/WindomZ/gitdate)
- [gituser](https://github.com/WindomZ/gituser)

## License

The [MIT License](https://github.com/WindomZ/gitclone/blob/master/LICENSE)
27 changes: 19 additions & 8 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ func newCommand(root bool) *_Command {

func (c *_Command) init() *_Command {
if !c.Valid() {
if dir, err := os.Getwd(); err == nil {
c.Usage(path.Base(dir))
var name string
if len(os.Args) != 0 {
name = os.Args[0]
} else if dir, err := os.Getwd(); err == nil {
name = path.Base(dir)
}
if len(name) == 0 {
panicError("root command should not be empty")
}
c.Usage(name)
}
return c
}
Expand Down Expand Up @@ -122,7 +129,7 @@ func (c *_Command) addCommand(cmd *_Command) bool {
c.commands = append(c.commands, cmd)
return true
} else {
panicError("ERROR Command format:", cmd)
panicError("command invalid format:", cmd)
}
return false
}
Expand Down Expand Up @@ -178,7 +185,7 @@ func (c *_Command) addOption(line bool, usage string, args ...interface{}) (opt

func (c *_Command) Option(usage string, args ...interface{}) Commander {
if opt := c.addOption(false, usage, args...); !opt.Valid() {
panicError("ERROR Option format:", opt)
panicError("option invalid format:", opt)
}
return c
}
Expand Down Expand Up @@ -258,12 +265,15 @@ func (c _Command) UsagesString() (r []string) {
return
}

func (c _Command) OptionsString() (r []string) {
func (c _Command) OptionsString() (r map[string]string) {
if !c.Valid() {
return
}
r = append(r, c.options.OptionsString()...)
r = append(r, c.commands.OptionsString()...)
r = c.options.OptionsString()
opts := c.commands.OptionsString()
for k, v := range opts {
r[k] = v
}
return
}

Expand Down Expand Up @@ -308,7 +318,8 @@ func (c _Command) HelpMessage() string {
}

// Options
if strs := c.OptionsString(); len(strs) != 0 {
if opts := c.OptionsString(); len(opts) != 0 {
strs := sortStringMap(opts)
hm.Line().Title("Options")
for _, str := range strs {
hm.Subtitle(str)
Expand Down
46 changes: 31 additions & 15 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,13 @@ func TestCommand_HelpMessage(t *testing.T) {
"cmd cmd2 [-a|--about] [-t|--test]",
"cmd cmd3 [y] [-b=<kn>|--bold=<kn>] [-c|--count]",
})
assert.Equal(t, c.OptionsString(),
[]string{
"-c --config config description",
"-d --drop drop description",
"-a --about cmd2 about description",
"-t --test cmd2 test description",
"-b=<kn> --bold=<kn>\n cmd3 bold description",
"-c --count cmd3 count description",
})
opts := c.OptionsString()
assert.Equal(t, opts["-c|--config"], "-c --config config description")
assert.Equal(t, opts["-d|--drop"], "-d --drop drop description")
assert.Equal(t, opts["-a|--about"], "-a --about cmd2 about description")
assert.Equal(t, opts["-t|--test"], "-t --test cmd2 test description")
assert.Equal(t, opts["-b|--bold"], "-b=<kn> --bold=<kn>\n cmd3 bold description")
assert.Equal(t, opts["-c|--count"], "-c --count cmd3 count description")

assert.Equal(t, c.HelpMessage(),
` this is description
Expand All @@ -69,13 +67,13 @@ func TestCommand_HelpMessage(t *testing.T) {
cmd cmd3 [y] [-b=<kn>|--bold=<kn>] [-c|--count]
Options:
-c --config config description
-d --drop drop description
-a --about cmd2 about description
-t --test cmd2 test description
-b=<kn> --bold=<kn>
cmd3 bold description
-c --config config description
-c --count cmd3 count description
-d --drop drop description
-t --test cmd2 test description
`)

c.Annotation("Example", []string{
Expand All @@ -92,13 +90,13 @@ func TestCommand_HelpMessage(t *testing.T) {
cmd cmd3 [y] [-b=<kn>|--bold=<kn>] [-c|--count]
Options:
-c --config config description
-d --drop drop description
-a --about cmd2 about description
-t --test cmd2 test description
-b=<kn> --bold=<kn>
cmd3 bold description
-c --config config description
-c --count cmd3 count description
-d --drop drop description
-t --test cmd2 test description
Example:
cmd xxx -c
Expand Down Expand Up @@ -167,3 +165,21 @@ func TestCommand_ErrorHandling(t *testing.T) {
assert.Error(t, err)
}
}

func TestCommand_Command(t *testing.T) {
c := newCommand(true)

assert.Empty(t, c.Name())

c.Command("go-commander")
c.Version("0.0.1")

c.Command("test")

c.Line("[opt]").Command("cmd")

defer func() {
assert.NotEmpty(t, recover())
}()
c.Command("")
}
8 changes: 6 additions & 2 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ package commander
type _Commands []*_Command

// OptionsString
func (c _Commands) OptionsString() (r []string) {
func (c _Commands) OptionsString() (r map[string]string) {
r = make(map[string]string, len(c))
for _, cmd := range c {
r = append(r, cmd.OptionsString()...)
opts := cmd.OptionsString()
for k, v := range opts {
r[k] = v
}
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ func newError(a ...interface{}) error {
}

func panicError(a ...interface{}) {
panic(errors.New("\ngo-commander:\n " + fmt.Sprint(a...)))
panic(errors.New("\ngo-commander:\n ERROR >>> " + fmt.Sprint(a...)))
}
4 changes: 4 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func (o _Option) Valid() bool {
return len(o.names) != 0 && len(o.usage) != 0
}

func (o _Option) Name() string {
return strings.Join(o.names, "|")
}

func (o _Option) Names() []string {
return o.names
}
Expand Down
16 changes: 5 additions & 11 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package commander

import (
"fmt"
"strings"
)
import "strings"

// _Options
type _Options []*_Option
Expand All @@ -29,19 +26,16 @@ func (o _Options) UsagesString(ones ...bool) (r []string) {
}
}
if len(rs) != 0 {
if len(rs) == 1 {
r = append(r, fmt.Sprintf("[%s]", strings.Join(rs, " ")))
} else {
r = append(r, fmt.Sprintf("%s", strings.Join(rs, " ")))
}
r = append(r, strings.Join(rs, " "))
}
return
}

func (o _Options) OptionsString() (r []string) {
func (o _Options) OptionsString() (r map[string]string) {
r = make(map[string]string, len(o))
for _, opt := range o {
if s := opt.OptionString(); len(s) != 0 {
r = append(r, s)
r[opt.Name()] = s
}
}
return
Expand Down
12 changes: 5 additions & 7 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ func TestOptions_OptionsString(t *testing.T) {
newOption("-c, --config", "config description"),
newOption("-d, --drop", "drop description"),
}
assert.Equal(t, o.OptionsString(),
[]string{
"-a --about about description",
"-b=<kn> --bold=<kn>\n bold description",
"-c --config config description",
"-d --drop drop description",
})
opts := o.OptionsString()
assert.Equal(t, opts["-a|--about"], "-a --about about description")
assert.Equal(t, opts["-b|--bold"], "-b=<kn> --bold=<kn>\n bold description")
assert.Equal(t, opts["-c|--config"], "-c --config config description")
assert.Equal(t, opts["-d|--drop"], "-d --drop drop description")
}
13 changes: 10 additions & 3 deletions program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
func TestProgram_AutomaticHelp(t *testing.T) {
Program = newProgram()

Program.Version("0.0.1").
Program.Command("go-commander").
Version("0.0.1").
Description("this is a test cli.")

if _, err := Program.Parse([]string{}); err != nil {
Expand All @@ -23,6 +24,7 @@ func TestProgram_AutomaticHelp(t *testing.T) {
func TestProgram_ShowVersion(t *testing.T) {
Program = newProgram()

Program.Command("go-commander")
Program.Version("0.0.1").
Description("this is a test cli.")

Expand All @@ -34,7 +36,8 @@ func TestProgram_ShowVersion(t *testing.T) {
func TestProgram_ErrorHandling(t *testing.T) {
Program = newProgram()

Program.Version("0.0.1").
Program.Command("go-commander").
Version("0.0.1").
Description("this is a test cli.")

Program.ErrorHandling(func(err error) {
Expand Down Expand Up @@ -103,6 +106,8 @@ func TestProgram_LineOption(t *testing.T) {
func TestProgram_Aliases(t *testing.T) {
Program = newProgram()

Program.Command("go-commander")

Program.Command("-i --init")

Program.Command("-o").
Expand All @@ -123,6 +128,8 @@ func TestProgram_Aliases(t *testing.T) {
func TestProgram_CommandDescription(t *testing.T) {
Program = newProgram()

Program.Command("go-commander")

Program.Command("-i --init").
Description("this is init flag")

Expand All @@ -137,9 +144,9 @@ func TestProgram_CommandDescription(t *testing.T) {
go-commander -v|--version
Options:
-h --help output usage information
-i --init this is init flag
-o --origin this is origin flag
-h --help output usage information
-v --version output the version number
`)
}
Expand Down
19 changes: 19 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package commander

import "sort"

func sortStringMap(m map[string]string) []string {
if m == nil || len(m) == 0 {
return []string{}
}
var keys []string
for k := range m {
keys = append(keys, k)
}
r := make([]string, len(keys))
sort.Strings(keys)
for i, k := range keys {
r[i] = m[k]
}
return r
}
17 changes: 17 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package commander

import (
"github.com/WindomZ/testify/assert"
"testing"
)

func TestUtils_sortStringMap(t *testing.T) {
assert.Empty(t, sortStringMap(nil))
assert.Empty(t, sortStringMap(make(map[string]string)))

assert.Equal(t, sortStringMap(map[string]string{
"1": "1",
"3": "3",
"2": "2",
}), []string{"1", "2", "3"})
}

0 comments on commit 40b1282

Please sign in to comment.