2017-02-26 5 views
1

この質問はクラスのファミリーについてのもので、豊富なコマンドライン解析の経験を提供するために.netコアで一緒に働いています。主なクラスはCommandLineApplicationです。 Thisは、主な施設を通って歩く物です。ヘルプオプションがコマンドラインで指定されている場合、アプリケーションを終了するにはどうすればよいですか?

これは、自動的に生成されたヘルプを表示するヘルプオプションを設定する方法です。

cla.HelpOption("-? | -h | --help"); 

私は、ヘルプオプションは、コマンドライン上のどこでも見つかった場合への私のアプリケーションではなく、実行中の維持のため、を終了します。しかし、私はこれを達成する良い方法を見つけることができません。私はもちろん、オプションが指定されているかどうかを調べるために引数を解析することができますが、これを行うためのこの機能の全ポイントではありませんか?ここで

は、私が使用しているサンプルコードです:

public class Config 
{ 
    public string Option1 {get; set;} 
} 

public class CommandLine 
{ 
    public static void ApplyCommandLineArguments(string[] args, Config config) 
    { 
     CommandLineApplication cla = new CommandLineApplication(false); 
     CommandOption option1 = cla.Option(
      "-o | --option1", 
      "Set this option to specify option1", 
      CommandOptionType.SingleValue 
     ); 
     cla.HelpOption("-? | -h | --help"); 
     cla.OnExecute(() => 
     { 
      if (option1.HasValue()) 
      { 
       config.Option1 = option1.Value(); 
      } 
      return 0; 
     }); 

     try 
     { 
      cla.Execute(args); 
     } 
     catch (CommandParsingException ex) 
     { 
      Console.WriteLine(ex.Message); 
      cla.ShowHelp(); 
     } 
    } 
} 

次にMain方法で:ヘルプオプションが見つかった場合

Config config = new Config(); 
CommandLine.ApplyCommandLineArguments(args, config); 
// I want to exit here if user specified the help option anywhere on the command line. 
Console.WriteLine("Hello World!"); 
+0

'if(option1.HasValue())'の中でコードを実行することになっていると思われます(あるいは 'config.Option1 = true'を設定して、あなたのメインコード)。 –

+0

@VisualVincent、これは私には明らかですが、ここでは 'option1'の価値はどのように役立つでしょうか?私は何が欠けていますか? –

+0

どのようにそのライブラリが動作するのかわかりませんが、 'option1.HasValue()'は、コマンドの1つが存在するときに 'true'を返すように見えます。次に、 'config.Option1'をその値に設定します。あなたはデバッガを使って変数を検査しようとしましたか? –

答えて

2

cla.Executeは実際にちょうどあなたのOnExecuteコールバックを実行して、行いませんOnExecuteコールバックのゼロ以外の値を返すことで使用できます。

public class CommandLine 
{ 
    // returns true if parse was successful and you can proceed. Returns false if you can terminate 
    public static bool ApplyCommandLineArguments(string[] args, Config config) 
    { 
     CommandLineApplication cla = new CommandLineApplication(false); 
     CommandOption option1 = cla.Option(
      "-o | --option1", 
      "Set this option to specify option1", 
      CommandOptionType.SingleValue 
     ); 
     cla.HelpOption("-? | -h | --help"); 
     cla.OnExecute(() => 
     { 
      if (option1.HasValue()) { 
       config.Option1 = option1.Value(); 
      } 
      // non-zero value 
      return 1; 
     }); 

     try { 
      int result = cla.Execute(args); 
      // check result 
      return result > 0; 
     } 
     catch (CommandParsingException ex) { 
      Console.WriteLine(ex.Message); 
      cla.ShowHelp(); 
      return false; 
     } 
    } 
} 

public static void Main(string[] args) 
    { 
     Config config = new Config(); 
     if (!CommandLine.ApplyCommandLineArguments(args, config)) { 
      return; 
     } 

     // I want to exit here if user specified the help option anywhere on the command line. 
     Console.WriteLine("Hello World!"); 
     Console.ReadKey(); 
    } 
+0

ヘルプを表示するには、 'ex.Command'で' ShowHelp'を呼び出すといいでしょう。対応するコマンド。 – Dejan

関連する問題