title: Conditional command line option with Apache commons CLI
body: <p>I am currently using Apache's common CLI library to parse command line options and I was wondering if there's an easy way for conditional requirements. </p>
  
  <p>For example, in the example below I have two required options <code>-a</code> and <code>-b</code> as well as an optional <code>-h</code>, which should show some description. The way the parser works right now is that it expected to throw a ParserException whenever one of the required options is missing. So far so good. My question is if there is a way to not look for required options when <code>-h</code> is specified. Since this it's common that <code>-h</code> should only print help and not actually starting my app, I'd like to skip checking other command line options.</p>
  
  <pre class="lang-java prettyprint-override"><code>import org.apache.commons.cli.*;
  
  public class MyClass {
  
      ...
  
      public static void main(String[] parameters)
      {
          Options options = new Options();
  
          Option opt_a = Option.builder("a")
                  .argName("A Option")
                  .required(true)
                  .longOpt("a-option")
                  .desc("First option")
                  .build();
  
          Option opt_b = Option.builder("b")
                  .argName("B Option")
                  .required(true)
                  .longOpt("b-option")
                  .desc("Second option")
                  .build();
  
          Option opt_h = Option.builder("h")
                  .argName("Help")
                  .longOpt("help")
                  .desc("Shows this help")
                  .build();
  
          options.addOption(opt_a);
          options.addOption(opt_b);
          options.addOption(opt_h);
  
          CommandLineParser parser = new DefaultParser();
  
          try {
              CommandLine cmd = parser.parse(options, args);
              ...
          } catch (ParseException e) {
              System.err.println("Error parsing command line options");
              System.err.println(e.getMessage());
              usage(options);
              System.exit(1);
          }
      }
  
      public static void usage(Options options) {
          String header = "My application\n\n";
          String footer = "\nPlease report issues at http://github.com/user/repo/issues";
  
          HelpFormatter formatter = new HelpFormatter();
          formatter.printHelp("MyApp", header, options, footer, true);
      }
  }
  </code></pre>