I'm trying to write a GNU-style command-line parser for Go, since the flags
package doesn't handle all these yet:
program -aAtGc --long-option-1 argument-to-1 --long-option-2 -- real-argument
Obviously, I don't want to use the flags
package, since I'm trying to replace it. Is there any other way to get to the command line?
To access all command-line arguments in their raw format, we need to use Args variables imported from the os package . This type of variable is a string ( [] ) slice. Args is an argument that starts with the name of the program in the command-line. The first value in the Args slice is the name of our program, while os.
All the command line arguments are stored in a character pointer array called argv[ ].
Nevermind.
package main
import (
"fmt"
"os"
)
func main() {
args := os.Args
fmt.Printf("%d\n", len(args))
for i := 0; i<len(args); i++ {
fmt.Printf("%s\n", args[i])
}
}
The documentation is quite incomplete, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With