I'm writing some code in go and i want to pass some inputs with docopt. With the first version, I pass via command line only a string, and everything works fine. With the second one, I want to pass two strings and it doesn't work and I get this error:
panic: runtime error: slice bounds out of range [:5] with length 0
goroutine 1 [running]:
main.main()
/home/user/go/src/github.com/keygen/cmd/keygen/main.go:45 +0xa56
It seems the first string passed has lenght 0.
I think the problem is in the line where I call the Default.Parser, but I can't find the solution. Could you help me?
This is the code not working:
package main
import (
"fmt"
"github.com/docopt/docopt.go"
"github.com/keygen"
"os"
"strconv"
)
const usage = `Keygen
Usage:
keygen (-m|-n|-c) [-l <length>] <serialStart> <serialStop>
keygen -h | --help
Options:
-l <length> Output key length [default: 10].
-h --help Show this screen.`
func main() {
var count keygen.Count
var Serial string
var args struct {
SerialStart string `docopt:"<serialStart>"`
SerialStop string `docopt:"<serialStop>"`
Length int `docopt:"-l"`
First bool `docopt:"-m"`
Second bool `docopt:"-n"`
Third bool `docopt:"-c"`
}
opts, err := docopt.DefaultParser.ParseArgs(usage, os.Args[1:], "")
if err != nil {
return
}
prefix := args.SerialStart[:5]
..........................................
This is the code working:
package main
import (
"fmt"
"github.com/docopt/docopt.go"
"github.com/keygen"
"os"
)
const usage = `Keygen
Usage:
keygen (-m|-n|-c) [-l <length>] <serial>
keygen -h | --help
Options:
-l <length> Output key length [default: 10].
-h --help Show this screen.`
func main() {
var count keygen.Count
var Serial string
var args struct {
Serial string `docopt:"<serial>"`
Length int `docopt:"-l"`
First bool `docopt:"-m"`
Second bool `docopt:"-n"`
Third bool `docopt:"-c"`
}
opts, err := docopt.DefaultParser.ParseArgs(usage, os.Args[1:], "")
if err != nil {
return
}
prefix := args.Serial[:5]
........................................