-
Notifications
You must be signed in to change notification settings - Fork 56
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
add ability to check if an arg was provided by a user #47
Comments
[Edit] - I've modified the solution below and published to NuGet. I've been waiting a long time for someone to ask this question :). I've added 2 properties to the [ArgRequired] attribute. They are 'If' and 'IfNot'. Here's how they work.
In this example, 'FirstArgument' is required only if 'SecondArgument' is specified by the user. You'll still need to check for null at runtime to see which one the user actually provided, but you'll get the validation for free. You can even put boolean expressions in the If and IfNot properties like this if you need to do some more complex logic.
You can do ands '&', ors '|', group expressions in parentheses, and use not '!'. That last example showed another new attribute called [ArgCantBeCombinedWith] that lets you declare that this argument is not valid if some other arguments are specified. Boolean expressions are allowed there as well. And if you're worried about hardcoding the property names as strings within the If and IfNot values then I understand, but I do some basic validation for you. For example if you make a typo and type the name of an argument that does not exist then you'll get an InvalidArgDefinitionException which is almost as good as a compilation error. After parsing, your code can tell if an argument was specified by checking for null. If you want to make a value type optional then use a Nullable. If you're not familiar with nullables they let you use value types like reference types. Here's an example:
Hopefully that does what you need. Let me know if it does not solve your problem or if you have any other feedback. Thanks, |
sounds like you covered it all, that is great! looking forward to the next nuget release so i can throw this in. I would just grab latest but think im going to wait so i can just let nuget manage the versions for me. I owe you a beer | coffee | tea ..... what ever it is you prefer to drink ;) |
Thanks for the kind words. I'll probably add a few more tests before updating NuGet. I wrote a full boolean expression parser to make this solution flexible and I want to make sure it's solid before I publish it. ETA - 1-2 weeks. |
Available in 2.0.6 |
First let me say how awesome this lib is and thank you for it!
one feature idea i have is that I would really like to be able to say
parsed.AnArge.WasProvidedByUser()
naturally the method call can be anything you want it to be but the feature is something i would like to have.
this is partly because i currently have 3 date arguments, start and end date or an as of date. either start and end date must be provided or as of date so i am not able to mark them all as required. looking into solving this with a custom validator but it seems like i will still not be able to leverage the prompt if not provided attribute so if you can come up with another solution that would allow that, maybe an argument dependency option.
[ArgRequiredWith(ArgClass.OtherArg)]
[ArgRequireIfOtherArgumentNotProvided(ArgClass.OtherArg)]
thanks again for everything!
The text was updated successfully, but these errors were encountered: