toagit
7/9/2015 - 3:52 AM

javaの実行可能クラスの雛形。CLIからの呼び出し用。

javaの実行可能クラスの雛形。CLIからの呼び出し用。

/**
 * 実行可能クラス雛形
 */
public class SampleMain() {
  
  private boolean readyToRun = false;
  private String sampleValue = "";
  
  /**
  * コンストラクタ
  */
  public void SampleMain(){
  }
  
  /**
  * エントリーポイント
  */
  public static void main(String[] args) {
    SampleMain sm = new SampleMain();
    sm.processArgs(args);
    if (sm.readyToRun) {
      tyr {
        sm.proc();
      } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
      } 
    }
    System.exit(0);
  }
  
  
  /**
  * メイン処理
  */
  private void proc() throws Exception {
    checkArgs();
    // メイン処理
  }
  
  /**
  * パラメーターチェック
  */
  private void checkArgs() throws SampleException {
    
  }
  
  /**
  * パラメーター解析
  */
  private void processArgs(String[] args) {
    if (args.length == 0) {
      printUsage();
      readyToRun = false;
      return;
    }

    readyToRun = true;
    String arg = "";
    for (int i = 0; i < args.length; i++) {
      arg = args[i];
      if (arg.equals("-help") || arg.equals("-h")) {
        printUsage();
        readyToRun = false;
        break;
      } else {
        sampleValue = args[++i];
      }
    }
  }
  
  /**
  * ヘルプ出力
  */
  private void printUsage {
    System.out.println("myClass [options]");
    System.out.println("Options:");
    System.out.println("  -help, -h             本クラスの使い方");
  }
}