kong icon indicating copy to clipboard operation
kong copied to clipboard

Panic with `${env}` in help for positional arguments

Open dbohdan opened this issue 3 months ago • 2 comments

I would like to use ${env} in the help message of a positional argument. It would make it consistent with my options. When I try to, help in Kong 1.12.1 panics saying "undefined variable".

Here is an demo.

Option with ${env}

module test

go 1.25.0

require github.com/alecthomas/kong v1.12.1
package main

import (
	"github.com/alecthomas/kong"
)

var cli struct {
	Foo string `help:"Foo (${env})" env:"FOO"`
}

func main() {
	ctx := kong.Parse(&cli)
	err := ctx.Run()
	ctx.FatalIfErrorf(err)
}

Let's build and run:

> go build && ./test -h
Usage: test [flags]

Flags:
  -h, --help          Show context-sensitive help.
      --foo=STRING    Foo (FOO)

Argument with ${env}

Let's use arg:"" to turn Foo into a positional argument, leaving everything else unchanged:

package main

import (
	"github.com/alecthomas/kong"
)

var cli struct {
	Foo string `arg:"" help:"Foo (${env})" env:"FOO"`
}

func main() {
	ctx := kong.Parse(&cli)
	err := ctx.Run()
	ctx.FatalIfErrorf(err)
}

The program panics:

> go build && ./test -h
panic: help for <foo>: undefined variable ${env}

goroutine 1 [running]:
github.com/alecthomas/kong.Parse({0x598660?, 0x757830?}, {0x0?, 0xc000110068?, 0x0?})
        /home/dbohdan/go/pkg/mod/github.com/alecthomas/[email protected]/global.go:11 +0xa5
main.main()
        /tmp/demo/main.go:12 +0x28

Argument without ${env}

Now let's remove ${env} from the description:

package main

import (
	"github.com/alecthomas/kong"
)

var cli struct {
	Foo string `arg:"" help:"Foo" env:"FOO"`
}

func main() {
	ctx := kong.Parse(&cli)
	err := ctx.Run()
	ctx.FatalIfErrorf(err)
}

It works again:

> go build && ./test -h
Usage: test <foo>

Arguments:
  <foo>    Foo ($FOO)

Flags:
  -h, --help    Show context-sensitive help.

dbohdan avatar Sep 28 '25 06:09 dbohdan

env isn't supported with positional arguments IIRC

alecthomas avatar Sep 28 '25 14:09 alecthomas

env isn't supported with positional arguments IIRC

It seems to be. env with positional arguments works. It has tests. Here is an earlier issue about positional arguments, env, and required: #153.

dbohdan avatar Sep 28 '25 16:09 dbohdan