alecthomas / kong

Kong is a command-line parser for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Env Var Default value for flag sourced from a Plugin

ffluk3 opened this issue · comments

We are trying to construct a plugin that could apply a --plain flag to several CLI tools. The ideal state is that either of the following are vaild:

my-cli --plain

# ...or...

PLAIN=true my-cli

The rough structure of our plugin is the following:

package kongplugin

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

type PlainFlag struct {
	Plain plainFlag `env:"PLAIN" help:"disable styling of the output"`
}

type plainFlag bool

func (v plainFlag) IsBool() bool { return true }
func (v plainFlag) BeforeApply(ktx *kong.Context) error {
	// Disable styling for any of our downstream tools

	return nil
}

Our main problem with this approach is that the environment variable PLAIN does not get respected by this approach, primarily because it appears Kong will not run any of plainFlag's hooks unless the flag is explicitly declared. We have tried the following:

  • Leverage all lifecycle hooks supported by Kong
  • Customer Resolver configurations to respect the default

Are there any recommendations of how we could get this functionality?