Flag value same as subcommand name result in error
Here is another bug I found
Minimal example:
package main
import (
"log"
"github.com/devfacet/gocmd"
)
func main() {
flags := struct {
Command struct {} `command:"command"`
Flag string `short:"f" long:"flag" required:"true"`
}{}
_, err := gocmd.New(gocmd.Options{
Flags: &flags,
AnyError: true,
})
if err != nil {
log.Fatal(err)
}
}
When running:
# bug -f command
argument -f needs a nonempty value
# bug -f "command"
argument -f needs a nonempty value
# bug -f other
Yes, it seems a bug. Give me a few days, I'll fix it asap.
P.S. Thanks for taking your time and reporting issues. I appreciate it...
See os.Args;
package main
import (
"fmt"
"log"
"os"
"github.com/devfacet/gocmd"
)
func main() {
fmt.Printf("args: %#v\n", os.Args)
flags := struct {
Command struct{} `command:"command"`
Flag string `short:"f" long:"flag" required:"true"`
}{}
_, err := gocmd.New(gocmd.Options{
Flags: &flags,
AnyError: true,
})
if err != nil {
log.Fatal(err)
}
}
$ go run main.go -f command
args: []string{"/var/folders/.../exe/main", "-f", "command"}
2018/03/18 11:59:12 argument -f needs a nonempty value
exit status 1
$ go run main.go -f "command"
args: []string{"/var/folders/.../exe/main", "-f", "command"}
2018/03/18 11:59:17 argument -f needs a nonempty value
exit status 1
$ go run main.go -f 'command'
args: []string{"/var/folders/.../exe/main", "-f", "command"}
2018/03/18 12:07:14 argument -f needs a nonempty value
exit status 1
$ go run main.go -f "\"command\""
args: []string{"/var/folders/.../exe/main", "-f", "\"command\""}
As you can see " and ' are stripped from os.Args values. So gocmd's behavior seems correct ("command" and 'command' are interpreted as command). I need to check other libraries and see whether this is a correct behavior or not. If it's not then I need to figure out how to get raw command line arguments in Go. It may take sometime...
I'm not sure I follow you. Surely the Os args value should strip the citations, that's not the issue. However it should not be interpreted as command as the order of arguments is different. After a flag that takes a string, it should be parsed as that string (as for all other strings), not as a subcommand.
This would mean that it would be impossible to run git commit -m "log" just because git log is a command.
In this case how can I determine whether the command line argument is a "command" or an "argument value" since os.Args values are stripped? See;
# -f is an argument flag and bar is a command flag
$ go run main.go -f bar
args: []string{"/var/folders/.../exe/main", "-f", "bar"}
# -f is an argument flag and bar is an argument value
$ go run main.go -f "bar"
args: []string{"/var/folders/.../exe/main", "-f", "bar"}
In both cases os.Args returns bar and I don't know how to check whether it is just bar (command) or "bar" argument value.