Mutually dependent parameters

Apr 16, 2014 at 9:19 AM
First off, thanks for your great library!

I have an enum that looks like this:
    /// <summary>
    /// The action type.
    /// </summary>
    public enum ActionType {
        /// <summary>
        /// The list switches action.
        /// </summary>

        /// <summary>
        /// The init DB action.
        /// </summary>

        /// <summary>
        /// The collect data action.
        /// </summary>

        /// <summary>
        /// The pre flight action.
        /// </summary>
and a parameter that looks like this
        /// <summary>
        /// Gets or sets the action type.
        /// </summary>
        [Option('a', "action", 
            Required = true,            
            HelpText = 
                "The action to execute. Possible values are: " + 
                "ListSwitches: List the switches known to the collector. " +
                "InitDb: Resets the database and reloads the switches from the switches.list file. " + 
                "CollectData: Collects interface data from the switches. + " +
                "PreFlight: Queries a single switch without writing to the DB")]
        public ActionType ActionType { get; set; }
Now if (and only if) --action PreFlight is set, I want to enforce a mandatory second parameter, like --action PreFlight --switch <somehostname>

How could this be done? I tried to find a way to override the default parsing behavior with a custom one but couldn't find hints on how to to this.

Oct 1, 2014 at 8:18 AM
I am no expert but I don't think this is support out of the box. That being said I did this a couple of times using a small workaround:

Define another option class inheriting your main option class (let's call it PreFlightOption).
Add the --action PreFlight dependents option to PreFlightOption
Parse args as your main option class
If ActionType is equal to ActionType.PreFlight, Parse args as PreFlightOption

Hope it helps!