Commons-cli-help-example

提供:Dev Guides
移動先:案内検索

Apache Commons CLI-ヘルプの例

Apache Commons CLIは、コマンドライン引数に関連するヘルプを印刷するHelpFormatterクラスを提供します。 例を見てください。

*CLITester.java*
import java.io.PrintWriter;

import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class CLITester {
   public static void main(String[] args) throws ParseException {

      Options options = new Options();
      options.addOption("p", "print", false, "Send print request to printer.")
         .addOption("g", "gui", false, "Show GUI Application")
         .addOption("n", true, "No. of copies to print");

      HelpFormatter formatter = new HelpFormatter();

      final PrintWriter writer = new PrintWriter(System.out);
      formatter.printUsage(writer,80,"CLITester", options);
      writer.flush();
   }
}

出力

ファイルを実行し、結果を確認します。

java CLITester
usage: CLITester [-g] [-n <arg>] [-p]