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 wanted to do something like this: <asp:Label ID=lblMyLabel onclick=lblMyLabel_Click runat=server>My Label</asp:Label> I know
I wanted to write a timer in java.which will do the following: when program
In java, if I wanted to create some application which could receive both doubles
For example, if I wanted if I wanted to parse the input arguments from
I have seen few questions similar to this one, but I wanted to make
I'm using lxml in Python to parse some HTML and I want to extract
I wanted to see different views / opinions on this. I've got Jquery invoking
I have a hash that i wanted to parse to XML using SimpleXML but
I just wanted to pause in an F# console application, so I wrote: Console.ReadKey()

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.