How do I get the command line arguments in Go without the "flags" package?

Viewed 5282

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?

2 Answers

The first argument of os.Args is the name of the go file, so to get only the command line arguments, you should do something like this

package main

import (
    "fmt"
    "os"
)

func main() {
    args := os.Args[1:]

    for i := 0; i<len(args); i++ {
        fmt.Println(args[i])
    }
}
Related