Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:58:27+00:00 2026-06-17T16:58:27+00:00

So I have an Options instance which among other options has (notice the isRequired()

  • 0

So I have an Options instance which among other options has (notice the isRequired()):

        options.addOption(OptionBuilder
                .withLongOpt("seq1")
                .withDescription("REQUIRED : blah blah")
                .hasArg().isRequired().create());
        options.addOption(OptionBuilder
                .withLongOpt("seq2")
                .withDescription("REQUIRED : blih blih")
                .hasArg().isRequired().create());
        options.addOption(new Option("?", "help", false,
                "print this message and exit"));

When I call parser.parse(args) throws an exception if seq1 and seq2 are not present – but I want to have it print my message and no exception thrown – how to go about it ? This throws NPE in line.hasOption("help"), naturally :

CommandLine line = null;
try {
    CommandLineParser parser = new GnuParser();
    // parse the command line arguments
    line = parser.parse(options, args);
} catch (ParseException e) {
    if (line.hasOption("help")) { //NPE
        usage(0);
    }
    System.err.println("Parsing failed.  Reason: " + e.getMessage());
    usage(1);
}

private static void usage(int exitCode) {
    // automatically generate the help statement
    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp("Smith Waterman", OPTIONS, true);
    System.exit(exitCode);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:58:27+00:00Added an answer on June 17, 2026 at 4:58 pm

    Solution adapted from here

    private static final Options OPTIONS = new Options();
    private static final Options HELP_OPTIONS = new Options();
    OPTIONS.addOption(OptionBuilder
            .withLongOpt("seq1")
            .withArgName("file1")
            .withDescription(
                    "REQUIRED : the file containing sequence 1")
            .hasArg().isRequired().create());
    // etc
    final Option help = new Option("?", "help", false,
            "print this message and exit");
    HELP_OPTIONS.addOption(help);
    OPTIONS.addOption(help);
    // later
    CommandLineParser parser = new GnuParser();
    CommandLine line = parser.parse(HELP_OPTIONS, args, true); // true so it
    // does not throw on unrecognized options
    if (line.hasOption("help")) {
        usage(0); // calls exit
    }
    line = parser.parse(OPTIONS, args);
    

    If anything more elegant comes up I would gladly accept it

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an options column which is serialized. I plan to store user preferences
I have Python classes, of which I need only one instance at runtime, so
I have an UITextView which is for instance 380 characters in length: NSLog(@aTextView.text lenght
I have a singleton instance that is referenced throughout the project which works like
I have a situation which has to be simple to solve, I would guess.
Currently I have a database object, vacancies which has a column (foreign key) CareerLevels.
I have one requirement in which User will have options to select languages for
I have developed a web dashboard which has a structure of controls embedded inside
I have created an application having tab activity which in turn has 5 different
Consider we have a database that has a table, which is a record of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.