-
-
Notifications
You must be signed in to change notification settings - Fork 102
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
Custom help text? #146
Comments
Hey @Lilja- thank you for posting this. What would you like the help text to be in this case? Which strings would you like to join together? |
I think it all boils down to the question:
I'll try to share my problem with some of the context of what I'm trying to build. I'm building a SSH config parser, which read the In my case I have a couple of columns: type Column int
const (
Host = iota
Username
Port
) And for my custom validation // A helper to show translate strings into this "iota type" (new to go, sorry if I call things incorrectly)
var columnKeys = map[Column]string {
"Host": Host,
"Username": Username,
"Port": Port,
}
type Columns = []Column
type Config struct {
// Help message not ideal, doesn't show what values are accepted.
Columns Columns `Help:"The columns to show"`
}
// and my proper validation goes something like this:
func (columns *Columns) UnmarshalText(b []byte) error {
// Behind the scenes some split by comma and then a for loop for each element. Like --column Host,Username
v, safe := columnKeys[string(b)]
if !safe {
return errors.New("Invalid value")
}
*columns = append(*columns, v)
return nil
} Now what if type Config struct {
Columns Columns `Help:"The columns to show. Possible value: Host,Username,Port"`
} But that isn't very maintainable as it splits the source of truth into two places. Now if I want to add an entry into the Column type I have to add it twice. It would be sweet if I could supply some kind of function here: type Config struct {
// Bear in mind my Javascript syntax here. I'm more familiar with Javascript.
Columns Columns `Help:"The columns to show, possible values: Object.keys(columnKeys)"`
} Or maybe some kind of custom help-message function? |
I see. Thank you for explaining this, @Lilja. I understand your need. It seems reasonable to me. One way we could do it is with a convention where you can put a function on your struct to customize the help text for any given struct field. For example: type Config struct {
Columns Columns
}
func (c *Config) HelpTextForColumns() string {
// do some computation here
} Under this approach we would just make it a convention that for a field named X, you name your function HelpTextForX. Another way would be to specify the function to call as a struct tag: type Config struct {
Columns Columns `helpfunc:MyCustomHelpText`
}
func (c *Config) MyCustomHelpText() string {
// do some computation here
} The main advantage of this approach that I see is that it would make it more obvious to people reading the code that this field does in fact have help text and that it is generated programmatically by a function. A third approach is that you could use go:generate to slot the help text into the struct tag at "go generate" time. A fourth approach is that you could just keep the tag updated manually, as you mention. I will think about this some more. Thanks for taking the time to write this issue. |
For what it's worth I like this approach: type Config struct {
Columns Columns `helpfunc:MyCustomHelpText`
}
func (c *Config) MyCustomHelpText() string {
// do some computation here
} |
Hello, Unfortunately, couldn't find any other alternatives that support this. type Config struct {
Columns Columns
}
func (c *Config) HelpTextForColumns() string {
localizer := i18n.NewLocalizer(bundle, "en")
result, _ := localizer.Localize(
&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "HelpText",
Other: "Nick has 2 cats.",
},
},
)
return result
} |
Is it possible to add custom help text? I'm using a custom type with a pointer receiver
UnmarshalText
function which takes "columns" as inputs, which is a series of columns(in the sense of a table) and validates these according to theiota
.This works fine and my validation does correct work. However, the help text isn't really helpful. Imagine if
--columns
took a series of columns, like--columns A,B
. Which then in my "enum"The text was updated successfully, but these errors were encountered: