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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:43:10+00:00 2026-05-25T09:43:10+00:00

What if I wanted to parse this: java MyProgram -r opt1 -S opt2 arg1

  • 0

What if I wanted to parse this:

java MyProgram -r opt1 -S opt2 arg1 arg2 arg3 arg4 --test -A opt3

And the result I want in my program is:

regular Java args[]  of size=4
org.apache.commons.cli.Options[]  of size=3
org.apache.commons.cli.Options[] #2 of size=1

I would prefer to use Apache Commons CLI, but the documentation is a little unclear about the case I present above. Specifically, the documentation doesn’t tell you how to handle options of the 3rd type I specify below:

1. options with a "-" char
2. options with a "--" char
3. options without any marker, or "bare args"

I wish that Apache Commons CLI would work but STILL be able to pass regular args to the program if those args didn’t have a option prefix. Maybe it does but the documentation doesnt say so as I read through it…

  • 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-05-25T09:43:11+00:00Added an answer on May 25, 2026 at 9:43 am

    You could just do it manually.

    NB: might be better to use a HashMap instead of an inner class for the opts.

    /** convenient "-flag opt" combination */
    private class Option {
         String flag, opt;
         public Option(String flag, String opt) { this.flag = flag; this.opt = opt; }
    }
    
    static public void main(String[] args) {
        List<String> argsList = new ArrayList<String>();  
        List<Option> optsList = new ArrayList<Option>();
        List<String> doubleOptsList = new ArrayList<String>();
    
        for (int i = 0; i < args.length; i++) {
            switch (args[i].charAt(0)) {
            case '-':
                if (args[i].length < 2)
                    throw new IllegalArgumentException("Not a valid argument: "+args[i]);
                if (args[i].charAt(1) == '-') {
                    if (args[i].length < 3)
                        throw new IllegalArgumentException("Not a valid argument: "+args[i]);
                    // --opt
                    doubleOptsList.add(args[i].substring(2, args[i].length));
                } else {
                    if (args.length-1 == i)
                        throw new IllegalArgumentException("Expected arg after: "+args[i]);
                    // -opt
                    optsList.add(new Option(args[i], args[i+1]));
                    i++;
                }
                break;
            default:
                // arg
                argsList.add(args[i]);
                break;
            }
        }
        // etc
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is my XML. I wanted to parse this using XSL. What I want
i have a string that looks like this: /dir/location/test-load-ABCD.p and i need to parse
I wanted to parse a text file that contains unstructured text. I need to
I wanted to parse the xml feed and display as links in my Blackberry
I wanted to write a timer in java.which will do the following: when program
I have a Java servlet, where I want to use the Jackson JSON parser
How can I parse this string on a javascript, var string = http://www.facebook.com/photo.php?fbid=322916384419110&set=a.265956512115091.68575.100001022542275&type=1; I
I've just started using DOM and wanted to parse an Android XML layout file
This is a piece of my code. Lambda accepts 3 parameters, and I wanted
I'm following a tutorial and it's working fine but if I wanted to parse

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.