c#藉助CommandLineParser寫命令行工具

CommandLine.dll可以從http://commandline.codeplex.com/下載

//選項類
class Options
{
	[Option('n', HelpText = "This is a test")]
	public string No { get; set; }

	[Option('m', HelpText = "This is a test")]
	public string Name { get; set; }

	[Option('i', HelpText = "This is a test",Required=true)]
	public int Id { get; set; }

	[Option('f', HelpText = "This is a test")]
	public bool Flag { get; set; }

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

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

static void Main(string[] args)
{
    try
    {
        //獲取main參數
        Options option = new Options();
        if (CommandLine.Parser.Default.ParseArguments(args, option))
        {
            if (option.No.Equals(1))
            {
                //處理
            }
            
            if(option.Flag )
            {
            	//處理
            }
         }
     }
  }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章