Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flag value same as subcommand name result in error #2

Open
sirmar opened this issue Mar 11, 2018 · 4 comments
Open

Flag value same as subcommand name result in error #2

sirmar opened this issue Mar 11, 2018 · 4 comments

Comments

@sirmar
Copy link

sirmar commented Mar 11, 2018

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
@devfacet
Copy link
Owner

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...

@devfacet devfacet added bug Something isn't working checking and removed bug Something isn't working labels Mar 11, 2018
@devfacet
Copy link
Owner

devfacet commented Mar 18, 2018

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...

@sirmar
Copy link
Author

sirmar commented Mar 23, 2018

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.

@devfacet
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants