Getopt for java?

T

Tom Anderson

Hello people,

If you want to process command-line arguments, doing things like
recognising flags, some of which may take parameters, how do you do it? In
C, you'd normally use getopt; many other languages provide comparable
utilities in their standard libraries (python has a getopt module, for
instance). Am i right in thinking the java standard library doesn't? (in
1.5, at least)? If so, are there almost-standard libraries (Apache commons
etc) that do? If not, what do you do? Is there something in com.sun that
could be used? How do javac and the other tools process their command
lines?

I once wrote a class to do it, using fairly basic rules, but i seem to
have misplaced my code. I could write it again from scratch fairly easily,
but i thought i'd see if there was a better alternative first.

tom
 
M

Mike Schilling

Tom said:
Hello people,

If you want to process command-line arguments, doing things like
recognising flags, some of which may take parameters, how do you do
it? In C, you'd normally use getopt; many other languages provide
comparable utilities in their standard libraries (python has a getopt
module, for instance). Am i right in thinking the java standard
library doesn't? (in 1.5, at least)? If so, are there almost-standard
libraries (Apache
commons etc) that do? If not, what do you do? Is there something in
com.sun that could be used? How do javac and the other tools process
their command lines?


There seem to be many open-source Java ports of getopt. Here's one

http://www.freebsdsoftware.org/java/java-getopt.html
 
M

Martin Gregorie

Hello people,

If you want to process command-line arguments, doing things like
recognising flags, some of which may take parameters, how do you do it?
In C, you'd normally use getopt; many other languages provide comparable
utilities in their standard libraries (python has a getopt module, for
instance). Am i right in thinking the java standard library doesn't? (in
1.5, at least)? If so, are there almost-standard libraries (Apache
commons etc) that do? If not, what do you do? Is there something in
com.sun that could be used? How do javac and the other tools process
their command lines?
I already used my own C getopt() because it guarantees identical argument
parsing (caseless options written as -o=value if they supply values)
under M$, Linux and OS-9. So I ported that to Java, adding switchable
caseless/caseful parsing and the ability to mix long and short options
(-o or --option).
 
A

Arne Vajhøj

Tom said:
If you want to process command-line arguments, doing things like
recognising flags, some of which may take parameters, how do you do it?
In C, you'd normally use getopt; many other languages provide comparable
utilities in their standard libraries (python has a getopt module, for
instance). Am i right in thinking the java standard library doesn't? (in
1.5, at least)? If so, are there almost-standard libraries (Apache
commons etc) that do?
http://commons.apache.org/cli/

If not, what do you do?

Something very primitive.
Is there something in
com.sun that could be used? How do javac and the other tools process
their command lines?

I don't know, very likely something primitive.

Arne
 
R

RC

Tom said:
Hello people,

If you want to process command-line arguments, doing things like
recognising flags, some of which may take parameters, how do you do it?
In C, you'd normally use getopt;

Here is how I do

java -Dvar1=var1 \
-Dvar2=val2 \
..... \
-Dvarn=valn \
YourClass

In your Java code

String val1 = System.getProperty("var1");
String val2 = System.getProperty("var2");
.....
String valn = System.getProperty("varn");

Of course, you can also do

Properties properties = System.getProperties();

Enumeration<String> enumeration = properties.propertyNames();

while (enumeration.hasMoreElements())
{
String var = (String)enumeration.nextElement();
String val = properties.getProperty(var);
}
 
T

Tom Anderson

Here is how I do

java -Dvar1=var1 \
-Dvar2=val2 \
.... \
-Dvarn=valn \
YourClass

In your Java code

String val1 = System.getProperty("var1");
String val2 = System.getProperty("var2");
....
String valn = System.getProperty("varn");

Yes! That occurred to me, and it is a very easy and javaish way of doing
it. I was a bit put off by the fact that it was rather different to
conventional command-line arguments, though.

tom
 
M

Martin Gregorie

Yes! That occurred to me, and it is a very easy and javaish way of doing
it. I was a bit put off by the fact that it was rather different to
conventional command-line arguments, though.
It seems to me you need to consider how the program is expected to be run
and design accordingly. If its always to be run from a command line or
shell script, then traditional CL arguments and options are good. If its
expected to be run from a GUI launcher etc. then properties/config files
are best. Properties, obviously, also have advantages if more than one
program needs a particular set of values.

Of course, there's also a third way: it may be convenient to use
properties as the preferred way of doing things but also use options to
override the properties. This is VERY useful during testing. For this
reason I'd always suggest implementing help (-?, --help), version and
debugging options.

If a set of programs needs particularly complex configuration data and
uses a database, then it can be a good idea to put the configuration into
a set of database tables.

In the past I've even found it beneficial to use a parser generator, i.e.
lex+yacc for C or Coco/R for Java, to define a configuration language.
This setup let us input a human-readable set of configuration files to
the configuration parser which populated a set of tables after
restructuring the data into a form that suited the application. It was
surprisingly easy to implement. This particular system had a 4000+ line
configuration that defined the system's file formats: a mandatory client
requirement was that arbitrary changes to the input files could be made
without any corresponding code changes to the system.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top