github.com/WindomZ/go-commander@v1.2.2/README.md (about) 1 # go-commander 2 3 [![Build Status](https://travis-ci.org/WindomZ/go-commander.svg?branch=master)](https://travis-ci.org/WindomZ/go-commander) 4 [![License](https://img.shields.io/badge/license-MIT-green.svg)](https://opensource.org/licenses/MIT) 5 [![Go Report Card](https://goreportcard.com/badge/github.com/WindomZ/go-commander)](https://goreportcard.com/report/github.com/WindomZ/go-commander) 6 [![Coverage Status](https://coveralls.io/repos/github/WindomZ/go-commander/badge.svg?branch=master)](https://coveralls.io/github/WindomZ/go-commander?branch=master) 7 8 > The solution for building command shell programs, 9 drive by <[docopt](https://github.com/docopt/docopt.go)>, 10 inspired by <[commander.js](https://github.com/tj/commander.js)> 11 12 ![v1.2.2](https://img.shields.io/badge/version-v1.2.2-blue.svg) 13 ![status](https://img.shields.io/badge/status-stable-green.svg) 14 15 ## Features 16 17 - [x] Has all the features of [docopt](https://github.com/docopt/docopt.go). 18 - [x] Usage like [commander.js](https://github.com/tj/commander.js) as simple and readable. 19 - [x] Automatic generated a help message, easy to use, or advanced usage see [documents](http://docopt.org/) of docopt. 20 - [x] Automatically execute the correct action function, don't worry about conflict. 21 - [x] Can customize the action function, better with context. 22 - [x] Code colloquial use, from top to bottom. 23 24 ## Installation 25 26 To get the package, execute: 27 28 ```bash 29 go get gopkg.in/WindomZ/go-commander.v1 30 ``` 31 32 To import this package, add the following line to your code: 33 34 ```go 35 import "gopkg.in/WindomZ/go-commander.v1" 36 ``` 37 38 ## Examples 39 40 ### [Quick example](https://github.com/WindomZ/go-commander/blob/master/examples/quick_example/quick_example.go) 41 42 Such as the following help message 43 44 ```markdown 45 Usage: 46 quick_example tcp <host> <port> [--timeout=<seconds>] 47 quick_example serial <port> [--baud=9600] [--timeout=<seconds>] 48 quick_example -h|--help 49 quick_example -v|--version 50 51 Options: 52 -h --help output usage information 53 -v --version output the version number 54 ``` 55 56 To coding with `go-commander` just like this: 57 58 ```go 59 import "github.com/WindomZ/go-commander" 60 ... 61 62 // quick_example 63 commander.Program. 64 Command("quick_example"). 65 Version("0.1.1rc") 66 67 // quick_example tcp <host> <port> [--timeout=<seconds>] 68 commander.Program. 69 Command("tcp <host> <port>"). 70 Option("--timeout=<seconds>"). 71 Action(func() { 72 fmt.Printf("tcp %s %s %s\n", 73 commander.Program.MustString("<host>"), 74 commander.Program.MustString("<port>"), 75 commander.Program.MustString("--timeout"), 76 ) 77 }) 78 79 // quick_example serial <port> [--baud=9600] [--timeout=<seconds>] 80 commander.Program. 81 Command("serial <port>"). 82 Option("--baud=9600"). 83 Option("--timeout=<seconds>"). 84 Action(func() { 85 fmt.Printf("serial %s %s %s\n", 86 commander.Program.MustString("<port>"), 87 commander.Program.MustString("--baud"), 88 commander.Program.MustString("--timeout"), 89 ) 90 }) 91 92 commander.Program.Parse() 93 ``` 94 95 Get the terminal output: 96 97 ```bash 98 $ quick_example --version 99 # output: 0.1.1rc 100 101 $ quick_example tcp 127.0.0.1 1080 --timeout=110 102 # output: tcp 127.0.0.1 1080 110 103 104 $ quick_example serial 80 --baud=5800 --timeout=120 105 # output: serial 80 5800 120 106 ``` 107 108 ### [Counted example](https://github.com/WindomZ/go-commander/blob/master/examples/counted_example/counted_example.go) 109 110 Such as the following help message 111 112 ```markdown 113 Usage: 114 counted_example -v... 115 counted_example go [go] 116 counted_example (--path=<path>)... 117 counted_example <file> <file> 118 counted_example -h|--help 119 counted_example -v|--version 120 121 Options: 122 -h --help output usage information 123 -v --version output the version number 124 ``` 125 126 To coding with `go-commander` just like this: 127 128 ```go 129 import "github.com/WindomZ/go-commander" 130 ... 131 132 // counted_example -v... 133 commander.Program. 134 Command("counted_example"). 135 Option("-v...", "", func(c commander.Context) { 136 fmt.Println("-v =", c.Get("-v")) 137 }) 138 139 // counted_example go [go] 140 commander.Program. 141 Command("go [go]"). 142 Action(func(c commander.Context) { 143 fmt.Println("go =", c.Get("go")) 144 }) 145 146 // counted_example (--path=<path>)... 147 commander.Program. 148 Command("(--path=<path>)...", "", func(c commander.Context) { 149 fmt.Printf("--path = %q\n", 150 c.MustStrings("--path")) 151 }) 152 153 // counted_example <file> <file> 154 commander.Program. 155 Command("<file> <file>", "", func(c commander.Context) { 156 fmt.Printf("<file> = %q\n", 157 c.MustStrings("<file>")) 158 }) 159 160 commander.Program.Parse() 161 ``` 162 163 Get the terminal output: 164 165 ```bash 166 $ counted_example -vvvvvvvvvv 167 # output: -v = 10 168 169 $ counted_example go go 170 # output: go = 2 171 172 $ counted_example --path ./here --path ./there 173 # output: --path = ["./here" "./there"] 174 175 $ counted_example this.txt that.txt 176 # output: <file> = ["this.txt" "that.txt"] 177 ``` 178 179 ### [Calculator example](https://github.com/WindomZ/go-commander/blob/master/examples/calculator_example/calculator_example.go) 180 181 Such as the following help message 182 183 ```markdown 184 simple calculator example 185 186 Usage: 187 calculator_example <value> ( ( + | - | * | / ) <value> )... 188 calculator_example <function> <value> [( , <value> )]... 189 calculator_example -h|--help 190 calculator_example -v|--version 191 192 Options: 193 -h --help output usage information 194 -v --version output the version number 195 196 Examples: 197 calculator_example 1 + 2 + 3 + 4 + 5 198 calculator_example 1 + 2 '*' 3 / 4 - 5 # note quotes around '*' 199 calculator_example sum 10 , 20 , 30 , 40 200 ``` 201 202 To coding with `go-commander` just like this: 203 204 ```go 205 import . "github.com/WindomZ/go-commander" 206 ... 207 208 // calculator_example 209 Program.Command("calculator_example"). 210 Version("0.0.1"). 211 Description("Simple calculator example") 212 213 // calculator_example <value> ( ( + | - | * | / ) <value> )... 214 Program.Command("<value> ( ( + | - | * | / ) <value> )...", "", func() { 215 var result int 216 values := Program.MustStrings("<value>") 217 for index, value := range values { 218 if i, err := strconv.Atoi(value); err != nil { 219 } else if index == 0 { 220 result = i 221 } else { 222 switch Program.GetArg(index*2 - 1) { 223 case "+": 224 result += i 225 case "-": 226 result -= i 227 case "*": 228 result *= i 229 case "/": 230 result /= i 231 } 232 } 233 } 234 fmt.Println(Program.ArgsString(), "=", result) 235 }) 236 237 // calculator_example <function> <value> [( , <value> )]... 238 Program.Command("<function> <value> [( , <value> )]...", "", func() { 239 var result int 240 switch Program.MustString("<function>") { 241 case "sum": 242 values := Program.MustStrings("<value>") 243 for _, value := range values { 244 if i, err := strconv.Atoi(value); err == nil { 245 result += i 246 } 247 } 248 } 249 fmt.Println(Program.ArgsString(), "=", result) 250 }) 251 252 // Examples: ... 253 Program.Annotation("Examples", 254 []string{ 255 "calculator_example 1 + 2 + 3 + 4 + 5", 256 "calculator_example 1 + 2 '*' 3 / 4 - 5 # note quotes around '*'", 257 "calculator_example sum 10 , 20 , 30 , 40", 258 }, 259 ) 260 261 commander.Program.Parse() 262 ``` 263 264 Get the terminal output: 265 266 ```bash 267 $ calculator_example 1 + 2 + 3 + 4 + 5 268 # output: 15 269 270 $ calculator_example 1 + 2 '*' 3 / 4 - 5 271 # output: -3 272 273 $ calculator_example sum 10 , 20 , 30 , 40 274 # output: 100 275 ``` 276 277 ## UsingList 278 279 - [gitclone](https://github.com/WindomZ/gitclone) 280 - [gitinit](https://github.com/WindomZ/gitinit) 281 - [gitdate](https://github.com/WindomZ/gitdate) 282 - [gituser](https://github.com/WindomZ/gituser) 283 284 ## Development 285 286 I would **love** to hear what you think about `go-commander` on [issues page](https://github.com/WindomZ/go-commander/issues) 287 288 Make pull requests, report bugs, suggest ideas and discuss `go-commander`. 289 290 ## License 291 292 The [MIT License](https://github.com/WindomZ/go-commander/blob/master/LICENSE)