Coder Social home page Coder Social logo

Comments (4)

adamabdelhamed avatar adamabdelhamed commented on July 29, 2024

[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

from powerargs.

workabyte avatar workabyte commented on July 29, 2024

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

from powerargs.

adamabdelhamed avatar adamabdelhamed commented on July 29, 2024

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.

from powerargs.

adamabdelhamed avatar adamabdelhamed commented on July 29, 2024

Available in 2.0.6

from powerargs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.