alexflint / go-arg

Struct-based argument parsing in Go
https://pkg.go.dev/github.com/alexflint/go-arg
BSD 2-Clause "Simplified" License
2.04k stars 100 forks source link

Positional argument env variable name and default not shown in help text #261

Closed hhromic closed 2 months ago

hhromic commented 3 months ago

If a positional argument is configured with an environment variable, the name is not shown in the help text like other arguments.

Consider the following MRE:

package main

import (
    "fmt"

    "github.com/alexflint/go-arg"
)

type args struct {
    Numbers []int `arg:"positional,required,env:NUMBERS" placeholder:"NUMBER" help:"one or more numbers to process"`
    Verbose bool  `arg:"--verbose,env:VERBOSE" help:"show more about the processing"`
}

func main() {
    var args args
    arg.MustParse(&args)

    fmt.Printf("%+v\n", args)
}

And consider the following interactions with the program:

$ go run main.go
Usage: main [--verbose] NUMBER [NUMBER ...]
error: numbers is required (or environment variable NUMBERS)
exit status 255

$ NUMBERS=1,2,3 go run main.go
{Numbers:[1 2 3] Verbose:false}

$ go run main.go -h
Usage: main [--verbose] NUMBER [NUMBER ...]

Positional arguments:
  NUMBER                 one or more numbers to process

Options:
  --verbose              show more about the processing [env: VERBOSE]
  --help, -h             display this help and exit

As it can be seen, when the positional argument is missing, go-arg correctly mentions that it can be provided using the NUMBERS environment variable. However this is not mentioned in the help text, like the --verbose argument.

I will prepare a PR to amend this soon.

hhromic commented 3 months ago

I just noticed that default values are also not reported in the help text either.

For example:

package main

import (
    "fmt"

    "github.com/alexflint/go-arg"
)

type args struct {
    Number  int  `arg:"positional,env:NUMBER" default:"1" placeholder:"NUMBER" help:"a number to process"`
    Verbose bool `arg:"--verbose,env:VERBOSE" default:"false" help:"show more about the processing"`
}

func main() {
    var args args
    arg.MustParse(&args)

    fmt.Printf("%+v\n", args)
}
$ go run main.go
{Number:1 Verbose:false}

$ NUMBER=5 go run main.go
{Number:5 Verbose:false}

$ go run main.go -h
Usage: main [--verbose] [NUMBER]

Positional arguments:
  NUMBER                 a number to process

Options:
  --verbose              show more about the processing [default: false, env: VERBOSE]
  --help, -h             display this help and exit