- Notifications
You must be signed in to change notification settings - Fork1
🍭 The solution for building command shell programs, drive by <docopt>, inspired by <commander.js>
License
WindomZ/go-commander
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The solution for building command shell programs,drive by <docopt>,inspired by <commander.js>
- Has all the features ofdocopt.
- Usage likecommander.js as simple and readable.
- Automatic generated a help message, easy to use, or advanced usage seedocuments of docopt.
- Automatically execute the correct action function, don't worry about conflict.
- Can customize the action function, better with context.
- Code colloquial use, from top to bottom.
To get the package, execute:
go get gopkg.in/WindomZ/go-commander.v1
To import this package, add the following line to your code:
import"gopkg.in/WindomZ/go-commander.v1"
Such as the following help message
Usage: quick_example tcp <host> <port>[--timeout=<seconds>] quick_example serial <port>[--baud=9600][--timeout=<seconds>] quick_example -h|--help quick_example -v|--versionOptions: -h --help output usage information -v --version output the version number
To coding withgo-commander
just like this:
import"github.com/WindomZ/go-commander"...// quick_examplecommander.Program.Command("quick_example").Version("0.1.1rc")// quick_example tcp <host> <port> [--timeout=<seconds>]commander.Program.Command("tcp <host> <port>").Option("--timeout=<seconds>").Action(func() {fmt.Printf("tcp %s %s %s\n",commander.Program.MustString("<host>"),commander.Program.MustString("<port>"),commander.Program.MustString("--timeout"),)})// quick_example serial <port> [--baud=9600] [--timeout=<seconds>]commander.Program.Command("serial <port>").Option("--baud=9600").Option("--timeout=<seconds>").Action(func() {fmt.Printf("serial %s %s %s\n",commander.Program.MustString("<port>"),commander.Program.MustString("--baud"),commander.Program.MustString("--timeout"),)})commander.Program.Parse()
Get the terminal output:
$ quick_example --version# output: 0.1.1rc$ quick_example tcp 127.0.0.1 1080 --timeout=110# output: tcp 127.0.0.1 1080 110$ quick_example serial 80 --baud=5800 --timeout=120# output: serial 80 5800 120
Such as the following help message
Usage: counted_example -v... counted_example go[go] counted_example (--path=<path>)... counted_example <file> <file> counted_example -h|--help counted_example -v|--versionOptions: -h --help output usage information -v --version output the version number
To coding withgo-commander
just like this:
import"github.com/WindomZ/go-commander"...// counted_example -v...commander.Program.Command("counted_example").Option("-v...","",func(c commander.Context) {fmt.Println("-v =",c.Get("-v"))})// counted_example go [go]commander.Program.Command("go [go]").Action(func(c commander.Context) {fmt.Println("go =",c.Get("go"))})// counted_example (--path=<path>)...commander.Program.Command("(--path=<path>)...","",func(c commander.Context) {fmt.Printf("--path = %q\n",c.MustStrings("--path"))})// counted_example <file> <file>commander.Program.Command("<file> <file>","",func(c commander.Context) {fmt.Printf("<file> = %q\n",c.MustStrings("<file>"))})commander.Program.Parse()
Get the terminal output:
$ counted_example -vvvvvvvvvv# output: -v = 10$ counted_example go go# output: go = 2$ counted_example --path ./here --path ./there# output: --path = ["./here" "./there"]$ counted_example this.txt that.txt# output: <file> = ["this.txt" "that.txt"]
Such as the following help message
simple calculator exampleUsage: calculator_example <value> ( ( + | - | * | / ) <value> )... calculator_example <function> <value>[( , <value> )]... calculator_example -h|--help calculator_example -v|--versionOptions: -h --help output usage information -v --version output the version numberExamples: calculator_example 1 + 2 + 3 + 4 + 5 calculator_example 1 + 2 '*' 3 / 4 - 5 # note quotes around '*' calculator_example sum 10 , 20 , 30 , 40
To coding withgo-commander
just like this:
import ."github.com/WindomZ/go-commander"...// calculator_exampleProgram.Command("calculator_example").Version("0.0.1").Description("Simple calculator example")// calculator_example <value> ( ( + | - | * | / ) <value> )...Program.Command("<value> ( ( + | - | * | / ) <value> )...","",func() {varresultintvalues:=Program.MustStrings("<value>")forindex,value:=rangevalues {ifi,err:=strconv.Atoi(value);err!=nil {}elseifindex==0 {result=i}else {switchProgram.GetArg(index*2-1) {case"+":result+=icase"-":result-=icase"*":result*=icase"/":result/=i}}}fmt.Println(Program.ArgsString(),"=",result)})// calculator_example <function> <value> [( , <value> )]...Program.Command("<function> <value> [( , <value> )]...","",func() {varresultintswitchProgram.MustString("<function>") {case"sum":values:=Program.MustStrings("<value>")for_,value:=rangevalues {ifi,err:=strconv.Atoi(value);err==nil {result+=i}}}fmt.Println(Program.ArgsString(),"=",result)})// Examples: ...Program.Annotation("Examples",[]string{"calculator_example 1 + 2 + 3 + 4 + 5","calculator_example 1 + 2 '*' 3 / 4 - 5 # note quotes around '*'","calculator_example sum 10 , 20 , 30 , 40",},)commander.Program.Parse()
Get the terminal output:
$ calculator_example 1 + 2 + 3 + 4 + 5# output: 15$ calculator_example 1 + 2'*' 3 / 4 - 5# output: -3$ calculator_example sum 10 , 20 , 30 , 40# output: 100
I wouldlove to hear what you think aboutgo-commander
onissues page
Make pull requests, report bugs, suggest ideas and discussgo-commander
.
TheMIT License
About
🍭 The solution for building command shell programs, drive by <docopt>, inspired by <commander.js>
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.