Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 控制台应用程序没有参数时不执行默认方法_C#_Ndesk.options - Fatal编程技术网

C# 控制台应用程序没有参数时不执行默认方法

C# 控制台应用程序没有参数时不执行默认方法,c#,ndesk.options,C#,Ndesk.options,当我执行myExe.exe时,下面的默认功能没有被执行,但它是在我运行myExe.exe-launch时执行的。有没有想过为什么这个应用程序在默认情况下不执行?根据法律,这应该有效 文件摘录: 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 def

当我执行
myExe.exe
时,下面的默认功能没有被执行,但它是在我运行
myExe.exe-launch
时执行的。有没有想过为什么这个应用程序在默认情况下不执行?根据法律,这应该有效

文件摘录:

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).
作为回退,可能会注册一个默认处理程序,该处理程序将处理上述任何匹配算法都无法处理的所有参数。默认处理程序由名称指定(可能是另一个名为NDesk.Options.Option的别名)。
我的代码:

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;
    }
}
publicstaticvoidmain(字符串[]args)
{
bool showHelp=false;
var options=new optionstart(){
{
“h”,“显示帮助”,
v=>showHelp=true
}, {
“配置”,“使用不同的设置重新配置启动器”,
v=>PromptConfig()
}, {
“v”,“显示当前版本”,
v=>ShowVersion()
}, {
“发射”,
v=>LaunchApplication()
}, {
“”,//默认值
v=>LaunchApplication()
}
};
//处理参数
额外列出;
试一试{
extra=options.Parse(args);
}
捕获(OptionException e){
Console.Write(“MyApp:”);
控制台写入线(e.Message);
WriteLine(“有关更多信息,请尝试使用'MyApp--help'”);
返回;
}
如果(显示帮助){
ShowHelp(选项);
返回;
}
}

默认处理程序设计用于处理未提供特定处理程序的任何参数

在您的情况下,运行
MyExe.exe
不应调用默认处理程序,因为没有要处理的参数。如果运行命令行,如
MyExe.exe-someUnknownArgument
,则默认处理程序应启动

无论如何,我相信
Parse
方法的目的是帮助您解析命令行,初始化表示参数的您自己的模型,然后对它们执行操作

例如,您的代码可能如下所示:

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");
    }
}
什么是不执行的?在不知道
选项的详细信息的情况下,它看起来工作正常。当没有传递任何参数时,Parse
LaunchApplication()
是不运行的。我不确定.Parse看起来像什么,它是在这里找到的NDesk.Options系统: