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

How to suppress UnexpectedArgException #50

Closed
piranout opened this issue Sep 11, 2014 · 1 comment
Closed

How to suppress UnexpectedArgException #50

piranout opened this issue Sep 11, 2014 · 1 comment

Comments

@piranout
Copy link

I have a BaseArgs class and a bunch of derived specialized argument classes. The BaseArgs class has an Enum for the action to be invoked. Actions are complex enough to warrant their own specific classes to hold their args.

I was hoping to use Args.Parse<BaseArgs>(args) to get the Action enum, then based on the result, use Args.Parse<DerivedArgs>(args), since DerivedArgs's type corresponds to the Action I got from the previous call. Is there a way to do this, or would I have to just try to parse as each derived class and catch the exceptions myself?

@adamabdelhamed
Copy link
Owner

I would not base the actions off of an enum. Here's the pattern I'd recommend. It lets you model your action specific options as classes, while letting PowerArgs invoke the right action for you so you don't have to base it off of a switch.

    [ArgExceptionBehavior(ArgExceptionPolicy.StandardExceptionHandling,ShowTypeColumn=false,ShowPossibleValues=true)]
    public class Commands
    {
        // global options
        [ArgShortcut("-q"), ArgDescription("Don't show verbose output")]
        public bool Quiet { get; set; }

        [ArgShortcut("-?"), ArgDescription("Displays this help"), HelpHook(ShowTypeColumn=false, ShowPossibleValues=true)]
        public bool Help { get; set; }

        [NonInteractiveIndicator, ArgDescription("Indicates that the REPL should not be used")]
        public bool NoREPL { get; set; }

        [ArgActionMethod,ArgDescription("Command1 help here")]
        public void Command1(Command1Args command)
        {
            command.Invoke();            
        }

        [ArgActionMethod,ArgDescription("Command2 help here")]
        public void Command2(Command2Args command)
        {
            command.Invoke();            
        }
    }  

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