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

cmd/go: "--" after the first "--" is not considered as file #39392

Closed
cuonglm opened this issue Jun 4, 2020 · 3 comments
Closed

cmd/go: "--" after the first "--" is not considered as file #39392

cuonglm opened this issue Jun 4, 2020 · 3 comments

Comments

@cuonglm
Copy link
Member

cuonglm commented Jun 4, 2020

What version of Go are you using (go version)?

$ go version
go version devel +429d2c548d Wed Jun 3 22:07:42 2020 +0000 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

$ cat main.go
package main

import (
	"fmt"
)

func main() {
	fmt.Println("Hello, playground")
}
$ go run -- main.go -- anything

What did you expect to see?

Run fails, error reports like any *nix commands:

$ cat -- main.go -- anything
...
cat: --: No such file or directory
cat: anything: No such file or directory

What did you see instead?

Run success.

@davecheney
Copy link
Contributor

davecheney commented Jun 4, 2020

--, in gnu flags speak, says to the flag parser, anything after -- is not a flag, even if it starts with a -. It's still up the program what it wants to do with those arguments.

(~/src) % cat main.go
package main

import (
        "fmt"
        "os"
)

func main() {
        fmt.Println(os.Args)
}
(~/src) % go run -- main.go -- anything
[/var/folders/wh/k4ydm2jx2tb5tgfvdcddl3ym0000gn/T/go-build725222586/b001/exe/main -- anything]

In the case of the cat example you gave, cat expects each of the arguments provided it to be files, so the second -- is treated as a file named --. I don't see why go run should follow those semantics

@cuonglm
Copy link
Member Author

cuonglm commented Jun 4, 2020

--, in gnu flags speak, says to the flag parser, anything after -- is not a flag, even if it starts with a -. It's still up the program what it wants to do with those arguments.


(~/src) % cat main.go

package main



import (

        "fmt"

        "os"

)



func main() {

        fmt.Println(os.Args)

}

(~/src) % go run -- main.go -- anything

[/var/folders/wh/k4ydm2jx2tb5tgfvdcddl3ym0000gn/T/go-build725222586/b001/exe/main -- anything]

In the case of the cat example you gave, cat expects each of the arguments provided it to be files, so the second -- is treated as a file named --. I don't see why go run should follow those semantics

IIRC, it’s POSIX.

Maybe at least we should add documentation for that?

@davecheney
Copy link
Contributor

davecheney commented Jun 4, 2020

IIRC, it’s POSIX.

Yes, you are completely correct. I realised after I wrote this that -- probably isn't just a GNUism.

Maybe at least we should add documentation for that?

I don't understand what there is to document, the second -- isn't interpreted as a file because its passed to main.go as os.Args. If anything its the operation of the first -- that is confusing to me because of the history of the go tools not using GNU style flags.

@cuonglm cuonglm closed this as completed Jun 4, 2020
@golang golang locked and limited conversation to collaborators Jun 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants