Link to home
Start Free TrialLog in
Avatar of Kyle Hamilton
Kyle HamiltonFlag for United States of America

asked on

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.
Avatar of Sharon Seth
Sharon Seth
Flag of India image

Why not in java ? The args passed from the command line are  received by the String [] arg of main() method .
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kyle Hamilton

ASKER

@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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you, as always :)