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

add ability to check if an arg was provided by a user #47

Closed
workabyte opened this issue May 23, 2014 · 4 comments
Closed

add ability to check if an arg was provided by a user #47

workabyte opened this issue May 23, 2014 · 4 comments

Comments

@workabyte
Copy link

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!

@adamabdelhamed
Copy link
Owner

[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.

    public class ConditionalArgs
    {
        [ArgRequired(If = "SecondArgument")]
        public string FirstArgument { get; set; }

        public string SecondArgument { get; set; }
    }

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.

    public class ComplexUnlessArgs
    {
        [ArgRequired(IfNot = "LocalFile & LocalFileUserName & LocalFilePassword")]
        [ArgCantBeCombinedWith("LocalFile | LocalFileUserName | LocalFilePassword")]
        public string Uri { get; set; }

        public string LocalFile { get; set; }
        public string LocalFileUserName { get; set; }
        public string LocalFilePassword { get; set; }
    }

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:

// Define a nullable property
public int? OptionalInt{get;set;}

// Check a nullable property
if(OptionalInt.HasValue)
{
    int intValue = OptionalInt.Value;
    // Do something with your int
}

Hopefully that does what you need. Let me know if it does not solve your problem or if you have any other feedback.

Thanks,
Adam

@workabyte
Copy link
Author

sounds like you covered it all, that is great!
thank you again for such a complete solution, really makes working with command line args fun lol

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 ;)

@adamabdelhamed
Copy link
Owner

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.

@adamabdelhamed
Copy link
Owner

Available in 2.0.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants