Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print usage for positional argument with Go's flag package?

Tags:

go

Given this simple Go program, which requires exactly one command line argument, how can I improve it so that flag.Usage() gives useful output?

package main

import (
    "flag"
    "fmt"
    "os"
)

func main() {
    flag.Parse()
    if len(flag.Args()) == 0 { 
        flag.Usage()
        os.Exit(1)
    }

    args := flag.Args()
    fmt.Println(args[0])
}

Current output with no arguments given:

$ ./args
Usage of ./args:

(i.e. usage is empty, as I can find no way to tell the usage() function which parameters are required).

I can remove flag.Usage() and replace it with something like this:

fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "<argument>")

However, I don't want to reinvent the wheel if there's already a good way with flag.Usage(). Especially as it's already handling optional arguments:

$ ./args -foo
flag provided but not defined: -foo
Usage of ./args:
like image 790
fazy Avatar asked Aug 07 '15 08:08

fazy


People also ask

How do you use the flag package in go?

The flag package automatically converts the input associated with the respective function flag to its corresponding value – such as converting input associated with flag.Int to an integer value. Also, it makes sure that an integer value is provided, otherwise it flags an error message at runtime.

How do you read 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.

What does flag parse do in go?

The flag. Parse function on the next line uses this pointer to set the bool variable based on the flags passed in by the user. We are then able to check the value of this bool pointer by dereferencing the pointer. More information about pointer variables can be found in the tutorial on pointers.

How do you pass the flag in Golang?

Any tag not defined in the program can be stored and accessed using the flag. Args() method. You can specify the flag by passing the index value i to flag. Args(i) .


1 Answers

flag.Usage() will only give you useful information about the defined flags. So you either define your arguments as flags via var foo = flag.Int(...).

Another option would be to define your own usage handler. see below for a simple example which will print a custom message and the defaults for all defined flags. This way you custom Usage will be printed in case flag.Parse() fails.

package main
import (
    "flag"
    "fmt"
    "os"
)

func myUsage() {
     fmt.Printf("Usage: %s [OPTIONS] argument ...\n", os.Args[0])
     flag.PrintDefaults()
}

func main() {
     flag.Usage = myUsage
     /* ... */
}
like image 183
dwalter Avatar answered Oct 16 '22 05:10

dwalter