Commits

yanchuan sim committed 102aa57

added option for whether to exit after displaying usage information

Comments (0)

Files changed (1)

src/edu/cmu/cs/ark/yc/config/AppConfig.java

    */
   private final boolean                           enable_config;
 
+  private boolean                                 exit_after_help;
+
   /**
    * Creates a new application configuration whose name is <code>name</code> and a short description about the application.
    * <p>
-   * The <code>-h/--help</code> option is enabled and <code>--config-file</code> is disabled by default.
+   * The <code>-h/--help</code>, <code>-c/--config-file</code> options are enabled and the program exits after displaying usage information (see {@link #AppConfig(String, String, boolean, boolean, boolean)}).
    * 
    * @param name
    *          name of application
     super();
     this.name = name;
     this.description = description;
-    this.enable_config = false;
+    this.enable_config = true;
+    this.exit_after_help = true;
 
-    try
-    {
-      this.registerParameter(new Switch("help", 'h', "help", "Displays this usage information."));
-    }
-    catch (JSAPException e)
-    {
-      e.printStackTrace();
-      System.exit(-1);
-    }
+    init(true);
   }
 
   /**
    * Creates a new application configuration whose name is <code>name</code> and a short description about the application.
    * <p>
-   * The <code>-h/--help</code> option is enabled by default.
+   * The <code>-h/--help</code> is enabled and the program exits after displaying usage information (see {@link #AppConfig(String, String, boolean, boolean, boolean)}).
    * 
    * @param name
    *          name of application
    */
   public AppConfig(String name, String description, boolean enableConfigFile)
   {
+    super();
     this.name = name;
     this.description = description;
     this.enable_config = enableConfigFile;
+    this.exit_after_help = true;
 
-    try
-    {
-      this.registerParameter(new Switch("help", 'h', "help", "Displays this usage information."));
-
-      if (enableConfigFile)
-        this.registerParameter(new FlaggedOption("config-file", FileStringParser.getParser().setMustExist(true).setMustBeFile(true), null, false, JSAP.NO_SHORTFLAG, "config-file", "Load configutation options from here."));
-    }
-    catch (JSAPException e)
-    {
-      e.printStackTrace();
-      System.exit(-1);
-    }
+    init(true);
   }
 
   /**
    * Creates a new application configuration whose name is <code>name</code> and a short description about the application.
+   * <p>
+   * The program exits after displaying usage information (see {@link #AppConfig(String, String, boolean, boolean, boolean)}).
    * 
    * @param name
    *          name of application
     this.name = name;
     this.description = description;
     this.enable_config = enableConfigFile;
+    this.exit_after_help = true;
+
+    init(addHelpArgument);
+  }
+
+  /**
+   * Creates a new application configuration whose name is <code>name</code> and a short description about the application.
+   * 
+   * @param name
+   *          name of application
+   * @param description
+   *          short description of app.
+   * @param enableConfigFile
+   *          enable the use of a -c/--config-file option where default settings are loaded from
+   * @param addHelpArgument
+   *          enable the use of a -h/--help flag
+   * @param exitAfterHelp
+   *          whether to exit after displaying usage information when user chooses <code>-h/--help</code>
+   */
+  public AppConfig(String name, String description, boolean enableConfigFile, boolean addHelpArgument, boolean exitAfterHelp)
+  {
+    super();
+    this.name = name;
+    this.description = description;
+    this.enable_config = enableConfigFile;
+    this.exit_after_help = exitAfterHelp;
+
+    init(addHelpArgument);
+  }
 
+  private void init(boolean addHelpArgument)
+  {
     try
     {
       if (addHelpArgument)
         this.registerParameter(new Switch("help", 'h', "help", "Displays this usage information."));
-      if (enableConfigFile)
+
+      if (enable_config)
         this.registerParameter(new FlaggedOption("config-file", FileStringParser.getParser().setMustExist(true).setMustBeFile(true), null, false, JSAP.NO_SHORTFLAG, "config-file", "Load configutation options from here."));
     }
     catch (JSAPException e)
     if (result.contains("help"))
     {
       displayUsage();
-      System.exit(0);
+      if (exit_after_help)
+        System.exit(0);
+
       return result;
     }
 
     if (result.contains("help") && result.getBoolean("help"))
     {
       displayUsage();
-      System.exit(-1);
+      if (exit_after_help)
+        System.exit(0);
     }
 
-    // if (!result.success())
-    // displayHelp(result);
-
     return result;
   }