Kyle Hamilton
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:
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.
(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
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.
Why not in java ? The args passed from the command line are received by the String [] arg of main() method .
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
thanks,
Kyle
@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'));
..and use a switch to parse the flags? (Sorry, this is very incomplete on my part)thanks,
Kyle
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you, as always :)