Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text flags #2051

Open
3 tasks done
somebadcode opened this issue Feb 5, 2025 · 1 comment · May be fixed by #2055
Open
3 tasks done

Text flags #2051

somebadcode opened this issue Feb 5, 2025 · 1 comment · May be fixed by #2055
Labels
area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this

Comments

@somebadcode
Copy link

somebadcode commented Feb 5, 2025

Checklist

  • Are you running the latest v3 release? The list of releases is here.
  • Did you check the manual for your release? The v3 manual is here.
  • Did you perform a search about this feature? Here's the GitHub guide about searching.

What problem does this solve?

The standard library's flag package has had flag.TextVar since 1.19 which allows any type that satisfies encoding.TextMarshaller and encoding.TextUnmarshaller to be set using flags. A good use for this is to set log level using strings such as slog.LevelVar.

Other logging packages such as go.uber.org/zap satisfies these interfaces for its atomic level type. You can have a logger before the log level has been set. It'll be ready before arguments have been parsed and output structured logs in case of a parse error.

Solution description

Implement TextValue and TextFlag.

Describe alternatives you've considered

Here's what I had to implement to get the functionality. It would be nice to see this being supported by this package.

package clix

import (
	"encoding"
	"strings"

	"github.com/urfave/cli/v3"
)

type TextMarshalUnMarshaller interface {
	encoding.TextMarshaler
	encoding.TextUnmarshaler
}

type TextFlag = cli.FlagBase[TextMarshalUnMarshaller, cli.StringConfig, TextValue]

type TextValue struct {
	Value  TextMarshalUnMarshaller
	Config cli.StringConfig
}

func (v TextValue) String() string {
	text, err := v.Value.MarshalText()
	if err != nil {
		return ""
	}

	return string(text)
}

func (v TextValue) Set(s string) error {
	if v.Config.TrimSpace {
		return v.Value.UnmarshalText([]byte(strings.TrimSpace(s)))
	}

	return v.Value.UnmarshalText([]byte(s))
}

func (v TextValue) Get() any {
	return v.Value
}

func (v TextValue) Create(t TextMarshalUnMarshaller, _ *TextMarshalUnMarshaller, c cli.StringConfig) cli.Value {
	return &TextValue{
		Value:  t,
		Config: c,
	}
}

func (v TextValue) ToString(t TextMarshalUnMarshaller) string {
	text, err := t.MarshalText()
	if err != nil {
		return ""
	}

	return string(text)
}

I can contribute a solution based on this if so desired.

@somebadcode somebadcode added area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this labels Feb 5, 2025
@dearchap
Copy link
Contributor

dearchap commented Feb 5, 2025

@somebadcode Yes please !!!!

somebadcode added a commit to somebadcode/urfave-cli that referenced this issue Feb 10, 2025
* Added `TextFlag` which supports setting values for types that satisfies
  the `encoding.TextMarshaller` and `encoding.TextUnmarshaller` which is
  very handy when you want to set log levels or string-like types that
  satifies the interfaces.

Fixes: urfave#2051
Signed-off-by: Tobias Dahlberg <[email protected]>
@somebadcode somebadcode linked a pull request Feb 10, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants