0

The default functionality below is not being executed when I execute myExe.exe, it is however being executed when I run myExe.exe -launch. Any thoughts on why this application isn't executing by default? According to the Documentation this should work

Excerpt from Documentation:

As a fallback, a default handler may be registered which will handle all arguments which are not handled by any of the above matching algorithms. The default handler is designated by the name <> (which may be an alias for another named NDesk.Options.Option).

My Code:

public static void Main (string[] args)
{
    bool showHelp = false;
    var options = new OptionSet() {
        {
            "h", "Show help",
            v => showHelp = true
        }, {
            "config", "Reconfigure the launcher with different settings",
            v => PromptConfig()
        }, {
            "v", "Show current version",
            v => ShowVersion()
        }, {
            "launch",
            v => LaunchApplication()
        }, {
            "<>", //default
            v => LaunchApplication()
        }
    };

    //handle arguments
    List<string> extra;
    try {
        extra = options.Parse (args);
    }
    catch (OptionException e) {
        Console.Write("MyApp:");
        Console.WriteLine(e.Message);
        Console.WriteLine("Try `MyApp--help' for more information");
        return;
    }

    if (showHelp) {
        ShowHelp(options);
        return;
    }
}
Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
Ben
  • 60,438
  • 111
  • 314
  • 488
  • What *isn't* executing? It looks like it works fine without knowing the details of `options.Parse` – Quintin Robinson Nov 13 '12 at 23:19
  • `LaunchApplication()` is what's not running when no parameters are passed. I'm not sure what .Parse looks like, it's the NDesk.Options system found here: http://www.ndesk.org/doc/ndesk-options/NDesk.Options/OptionSet.html – Ben Nov 13 '12 at 23:21

1 Answers1

1

The default handler is designed to handle any argument for which you didn't provide a specific handler.

In your case, running MyExe.exe should not invoke the default handler, because there is not argument to handle. If you run a command line such as MyExe.exe -someUnknownArgument then the default handler should kick in.

Anyway, I believe the intention of the Parse method is to help you parse the command line and initialize your own model that represents the arguments, and then act on them.

So, for example, your code could look like:

public enum Action
{
    ShowHelp,
    ShowVersion,
    PromptConfig,
    LaunchApplication
}

public static void Main (string[] args)
{
    var action = Action.LaunchApplication;

    var options = new OptionSet() {
        {
            "h", "Show help",
            v => action = Action.ShowHelp
        },
        {
            "config", "Reconfigure the launcher with different settings",
            v => action = Action.PromptConfig
        },
        {
            "v", "Show current version",
            v => action = Action.ShowVersion
        }, 
        {
            "launch",
            v => action = Action.LaunchApplication
        }
    }

    try 
    {
        // parse arguments
        var extra = options.Parse(args);

        // act
        switch (action)
        {
            // ... cases here to do the actual work ...
        }
    }
    catch (OptionException e) 
    {
        Console.WriteLine("MyApp: {0}", e.Message);
        Console.WriteLine("Try `MyApp --help' for more information");
    }
}
Ran
  • 5,989
  • 1
  • 24
  • 26