-
-
Notifications
You must be signed in to change notification settings - Fork 252
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
Feature request: provide default value for option (and show it in the help) #82
Comments
I found https://github.com/natemcmaster/CommandLineUtils/blob/master/samples/Attributes/Program.cs#L19 which is pretty much what I want. I guess the help text can't be easily generated from there. |
That being said, I would like it if it could infer the default value from the target property, as sometimes I want default values that can be statically set, but can't be set as a constant (e.g. a path that is in the user's home directory). For example: [Option("--data-directory")]
public string DataDirectory { get; set; } = Path.Combine(Environment.GetSpecialDirectoryPath(Environment.SpecialDirectories.UserProfile), ".cache/app"); (typed from memory, may not compile) My hope is that I could add a |
Could we reopen this? |
Marking this as up-for grabs. I would be okay adding logic to print the default CLR value in the help. I'm not sure yet about adding new properties to |
My current workaround is to set the default value on the property, don't mark it with Required (as this would stop the execution because the option was not set in the command line) and call |
And how would you set the default value using the builder API? |
I made a PR #369 about showing allowed values in help text which is somewhat similar with this issue. |
Here's my proposal public class CommandOption
{
// other members
public Func<object> DefaultValue { get; set; }
} Usage var app = new CommandLineApplication();
app.Option("-o|--opt", "Some description.", CommandOptionType.SingleValue, o => o.DefaultValue = () => "foo"); attribute: class Program
{
[Option("-o|--opt", "Some description.", CommandOptionType.SingleValue)]
public string? Option { get; } = "foo";
} help text
|
We may need re-consider about the below implementation, or leave it as is: public class CommandOption
{
// other members
public bool HasValue()
{
return Values.Any();
}
public string? Value()
{
return HasValue() ? Values[0] : null;
}
} |
Do you have any comments? @natemcmaster |
I prefer to avoid using public class CommandOption
{
// other members
public string? DefaultValue { get; set; }
}
public class CommandOption<T>
{
private T _defaultValue;
public new T DefaultValue
{
get => _defaultValue;
set
{
_defaultValue = value;
base.DefaultValue = value?.ToString();
}
}
} |
I proposed object instead of string because I wanted to avoid converting, especially for attribute API. The most institute way to populate a default value is [Option("-o|--opt", "Some description.", CommandOptionType.SingleValue)]
public SomeType? Option { get; } = SomeExpressionToBuildAValueOfSomeType; I’ll rethink about it. |
The problem here is the logic about Value() and HasValue() /// <summary>
/// True when <see cref="Values"/> is not empty.
/// </summary>
/// <returns></returns>
public bool HasValue()
{
return Values.Any(); // Should we continue checking if the default value is set?
}
/// <summary>
/// Returns the first element of <see cref="Values"/>, if any.
/// </summary>
/// <returns></returns>
public string? Value()
{
return HasValue() ? Values[0] : this.Default; // Sure we can return the first element of values, if any, and fall back to default value, if it has.
}
public string? DefaultValue { get; set; } |
|
Default value for |
I think the least-surprising behavior here would be to update Value and HasValue. For CommandOption:
For CommandArgument:
|
I checked the code and I think we can share the same logic: |
This issue is resolved and available now in this beta: https://www.nuget.org/packages/McMaster.Extensions.CommandLineUtils/4.0.0-beta.56. Please take a look and let me know if you have feedback on its behavior. |
@natemcmaster First thank you for this library, its really helpful. Regarding Default values, since this is in beta, I think the sample code at the front page shouldn't include |
Apologies if I've missed this one. I see in the examples you're using conditionals to set default values, but what about providing something like
[Option(Description = "Verbosity",DefaultValue = 5)]
so when an option is not required a default value is supplied - this could also be reflected in the help text so we're following the Principle of Least Astonishment.The text was updated successfully, but these errors were encountered: