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 8362961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:03:54+00:00 2026-06-09T12:03:54+00:00

I am trying to use Java cli commanlineparser to parse the follwing arguments, java

  • 0

I am trying to use Java cli commanlineparser to parse the follwing arguments,

java -OC:\mydirectory -NMyfile

Option -O is for directory and -N is for the name of file.

I have been looking online but couldnt find a good example and this is what I am trying to do,

Option option = new Option()
option.addOpton("O",true, "output directory)
option.addOpton("N",true, "file name)
...
CommandLineParser parser = new BasicParser();
...
if (cmd.hasOption("O")
...

Basically, I am trying to add multiple options and be able to parse them. Is this correct way to run the program with above options?

Thanks.

  • 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-09T12:03:56+00:00Added an answer on June 9, 2026 at 12:03 pm

    Try the following:

    ...
    Option opt1 = OptionBuilder.hasArgs(1).withArgName("output directory")
        .withDescription("This is the output directory").isRequired(true)
        .withLongOpt("output").create("O");
    
    Option opt2 = OptionBuilder.hasArgs(1).withArgName("file name")
        .withDescription("This is the file name").isRequired(true)
        .withLongOpt("name").create("N")
    
    Options o = new Options();
    o.addOption(opt1);
    o.addOption(opt2);
    CommandLineParser parser = new BasicParser();
    
    try {
      CommandLine line = parser.parse(o, args); // args are the arguments passed to the  the application via the main method
      if (line.hasOption("output") {
         //do something
      } else if(line.hasOption("name") {
         // do something else
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
    ...
    

    Also, you should leave a blank space between the argument and the value in the command line.

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

Sidebar

Related Questions

I'm trying to use java.util.List in a C# file. I have IKVM 0.40. I
I have been trying to use java.util.logging.logger to log stuff on to a file
I am trying to use Java based webservice and have soap request: <?xml version=1.0?>
I have a android webservice client application. I am trying to use the java
I am trying to use Robot in Java to make an image file of
I am trying to use the java Mail API in my program, i have
I'm trying to use Java Applet for my website. but I have a problem
I have a line of DNA code and I'm trying to use a Java
I'm trying to use java to read a string from a file that was
I am trying to use a Java application (which I do not have the

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.