-
-
Notifications
You must be signed in to change notification settings - Fork 261
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
Display possible value for enum in Help text #92
Comments
This is a great question. I've had similar thoughts about trying to use validators or type inference to generate help text. I've avoided doing this so far as it can be complicated to do it automatically. What you've done for now is what I've been recommending to users as the simplest fix. I would be open to investigating this more. My current thinking is that we could update the DefaultHelpTextGenerator to inspect the |
I have subsequently changed it to actually display the list of possible values in the description, e.g.
I actually like that more, because otherwise the left-hand column of that option table may get too wide. So on second thought, perhaps this is better if you leave it to the developer to manually display however they choose? I won't mind if you close this issue. |
I've had similar requests, so not going to close. I think its a useful feature; I just haven't thought thru an API for it. |
I currently do this manually:
I'd be interested in this feature as well. My format (of listing integer values as well) is useful but I'd estimate that it is generally not what people would want. |
Another thing is that it may also be necessary to indicate the default option, So as per @atruskie sample above, maybe something like this:
|
Whatever API we come up with needs to be flexible enough to allow users to control the output. I don't want to add behavior that is hard to override or change. While some may like the int<->Enum mapping, I know others who would not. |
I filed a question about Here's my implementation for parsing an bool TryParseEnumOption<T>(
CommandOption option,
T defaultvalue,
out T result
) where T : struct {
if(option.Value() == null) {
result = defaultvalue;
return true;
}
if(int.TryParse(option.Value(), out int intValue)) {
if(!Enum.GetValues(typeof(T)).Cast<int>().Any(v => v == intValue)) {
goto failed;
}
result = (T)Convert.ChangeType(Enum.ToObject(typeof(T), intValue), typeof(T));
return true;
}
if(Enum.TryParse(typeof(T), option.Value(), ignoreCase: true, result: out object enumValue)) {
result = (T)Convert.ChangeType(enumValue, typeof(T));
return true;
}
failed:
var pairs = Enum.GetValues(typeof(T))
.Cast<int>()
.Zip(
Enum.GetNames(typeof(T)).Cast<string>(),
(value, name) => $"{value}={name.ToLowerInvariant()}"
);
Console.WriteLine($"value for {option.Template} must be one of {string.Join(", ", pairs)}");
result = defaultvalue;
return false;
} Intended usage is: if(verboseLevelOption.HasValue()) {
if(!TryParseEnumOption(
verboseLevelOption,
VerboseLevel.Normal,
out _verboseLevel
)) {
// handle option error
return;
}
} It doesn't handle the default case, although it seems that would be a simple addition since it already has everything otherwise. |
I've put this in the 2.3.0 milestone. I'm planning to implement API to control help text in a more granular way, without having to re-implement all of IHelpTextGenerator. Let me know if you are interested in helping implement it. |
Is this still in the 2.3.0 milestone ? |
No. I moved it out because I'm not planning to implement this myself. If you'd like to take this one, let me know. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 7 days. |
Would you be interested in an implementation that simply prints out the legal values. existing behaviour
then becomes
|
@kbilsted yes! |
When options or arguments are parsed into an enum, the help text will now include the values contained in the enum. fixes natemcmaster#92
When options or arguments are parsed into an enum, the help text will now include the values contained in the enum. fixes natemcmaster#92
When options or arguments are parsed into an enum, the help text will now include the values contained in the enum. Fixes #92 Also made the default constructor public. This allows the DefaultHelpTextGenerator to be instantiated on a per-use basis in order to keep unit tests a isolated from each other as possible. Without that change, all instances would be the same singleton instance - i.e. a test can affect another test by fiddling with the overridden width.
I have several options for my command which are limited to enumerated types. Currently, the help text is displayed as follows:
But I want to display the possible values for the enumerated types. What I have done (for now) is to update the
rel
option, for example, as follows:This will then display the possible values as follows:
Is this the correct way to go about it? Or is there a way to automatically display the list of values in the case of enumerated types which I have missed?
Also, is this not perhaps something we can add as a standard "feature" to the generated help text? I think it is pretty common for certain options to be limited to specific predefined values.
The text was updated successfully, but these errors were encountered: