列出所有可能的开关案例?

时间:2017-10-16 01:19:00

标签: c# console-application

我正在制作一个控制台应用程序并且它工作得很好但是对于它的某些部分是切换案例,我想做一个

case "help":
    //List all cases here without Console.WriteLine("Option, Option, Option")

我试过谷歌搜索到目前为止没有结果,如果我打印开关的值它返回帮助:/

1 个答案:

答案 0 :(得分:1)

我已使用http://jsfiddle.net/wb5fwLoc/1/中的CommandLineParser实现此目标。

例如:

    class Options
    {
        [Option("account-name", Required = true, HelpText = "Name of the account to use")]
        public string AccountName { get; set; }

        [Option("single-file", HelpText = "Use one file as output")]
        public bool SingleFile { get; set; }

        [Option("excel-timestamps", DefaultValue = false, HelpText = "If set, timestamps will be printed with no timezone information in a format recognisable by Excel")]
        public bool ExcelTimestamps { get; set; }

        [ParserState]
        public IParserState LastParserState { get; set; }

        [HelpOption]
        public string GetUsage()
        {
            return HelpText.AutoBuild(this,
              (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
        }
    }

然后在Main

static int Main(string[] args)
    {
        try
        {
            var options = new Options();
            if (!Parser.Default.ParseArguments(args, options))
            {
                options.GetUsage();//Prints to console

                (...)