Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 635
György Andrasek Avatar asked Nov 11 '09 15:11

György Andrasek


People also ask

How do I get command line arguments in Golang?

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.

Where does command line arguments are get stored?

All the command line arguments are stored in a character pointer array called argv[ ].


1 Answers

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.

like image 52
György Andrasek Avatar answered Oct 15 '22 09:10

György Andrasek