JAVA command line flags

Is there is a good solution out there for creating custom flags to be set on the command line?

(from my search online, JAVA doesn't appear to have this capability natively, like C)

so I could do something like this:

java MyClass -t50 -d100

Open in new window


and in my program have some conditional logic that checks for the existence of the flags, and sets those variables accordingly, or else sets default values

Thanks.
LVL 25
Kyle HamiltonData ScientistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sharon SethCommented:
Why not in java ? The args passed from the command line are  received by the String [] arg of main() method .
0
CEHJCommented:
For simple stuff, i usually use the form

-flag1 arg1 -flag2 arg2

Open in new window


and turn the String[] into a Map in a homebrewed manner. For more sophisticated and complex requirements, you probably need an api such as
http://www.gnu.org/software/gnuprologjava/api/gnu/getopt/Getopt.html
0
Kyle HamiltonData ScientistAuthor Commented:
@sharonseth, because I would like something more along the lines of C's argv where I can pass optional flags in arbitrary order.

@CEHJ, for my current purposes a homebrewed Map sounds pretty good. I will definitely look into the Getopt API though - that's more along the lines of what I am ultimately looking for.

Could you show me an example  of your Map?

Something like this maybe?

if I want to run: java MyClass -b myValue
Map<Character, String> opts = new HashMap<Character, String>(); 
            for(int i = 0; i < args.length-1; i+=2) {
              opts.put(args[i].charAt(1), args[i+1]);
            }
        System.out.println(opts.get('b'));

Open in new window

..and use a switch to parse the flags? (Sorry, this is very incomplete on my part)

thanks,
Kyle
0
CEHJCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kyle HamiltonData ScientistAuthor Commented:
thank you, as always :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.