+
Skip to content

Panic with ${env} in help for positional arguments #556

@dbohdan

Description

@dbohdan

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/kong@v1.12.1/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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载