C# 命令行分析器

来源:互联网 发布:linux 搭建cdn 编辑:程序博客网 时间:2024/06/11 01:49

http://commandline.codeplex.com/

下载

CommandLine.dll;

应用

using CommandLine;
using CommandLine.Text;

作用:

用于取出命令行中的参数。

使用:

定义 一个CommandLineArguments类:

class CommandLineArguments    {        // 短参数名称,长参数名称,是否是可选参数,默认值,帮助文本等        // 第一个参数-f        [Option('f', "file", Required = true, HelpText = "Segy Filename.")]        public string SegyFile { get; set; }        // 第二个参数-s        [Option('s', "samples", DefaultValue = 15, HelpText = "keep these samples.")]        public int NewSamples { get; set; }        // 第三个参数-r        [Option('r', "traces", DefaultValue = 1000, HelpText = "keep these traces.")]        public int NewTraces { get; set; }        [ParserState]        public IParserState LastParserState { get; set; }        [HelpOption]        public string GetUsage()        {            return HelpText.AutoBuild(this,              (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));        }    }
在主函数中:

       static void Main(string[] args)        {            var options = new CommandLineArguments();            if (CommandLine.Parser.Default.ParseArguments(args, options))            {                string segyfile = options.SegyFile;                int newSamples = options.NewSamples;                int newTraces = options.NewTraces;                Console.WriteLine(segyfile);            }        }


0 0
原创粉丝点击