|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.dynamide.util.Opts
public class Opts
This class handles one command line options style: All options must begin with a dash. In this documentation, "switch" is the same as "option", but refers specifically to the token with the dash. Dashes may be single or double:
-foo or --fooValues may not have dashes. E.g.
myprogram -foo -bar -file ./myfile -user "laramie"
The simplest way to think of this class is:
More details follow:
Boolean arguments can appear anywhere in the command line. Any switch that is followed by another switch is considered a boolean argument. (Except that anything after -- is not considered a switch).
To end switched arguments, uses "--" by itself, then any remaining arguments. The remaining arguments can then be retrieved by a call to getRemainingArgs(), which returns a String[]. e.g.,
-foo -bar mojo -- nixon finbarsets up a command line where calling getRemainingArgs() would retrieve {"nixon", "finbar"} as a Java String array. Parameters after -- can begin with dashes.
If you follow the rules, you don't need to use -- to begin the list of remaining args. If command line is "java myclass -foo a b a", remaining args will be "b" and "a", unless you call addOption("-foo", Boolean.class, false) or addOption("-foo", Boolean.class, true) then remaining args is "a", "b", and "c".
Alternatively, you can call getOptionList() to get the list of arguments at the end, or getOptionList(key) to see the non-switch arguments after "key", EVEN if you have defined "key" to be boolean. See examples at those methods. The getOptionList(key) method enables you to use embedded lists:
-list1 a b c -list2 d e f
You cannot group option letters: "-abc" cannot be construed as options "a", "b", and "c" all being true. In this version, you must specify such options as "-a -b -c"
You can have spaces in the values if the Java VM on your platform and you shell handle spaces in quotes, by putting quotes around option values. In csh, etc., you can use single or double quotes, in NT CMD you must use double quotes.
This style is not supported: -b12 where this implies option "b" == 12.
If addOption is called, the options will be rescanned with the new rules before the next access of any option. You can also call validate() after addOption and the new rules will apply.
If the form you need to process is "java MyClass -foo a b c", then you have three options: the default behavior is that -foo will be a String type and getOptionList will return {"b", "c"} as an array; this will also be the behavior if you call addOption("-foo", String.class) or addOption("-foo", String.class, true); else you can call addOption("-foo", Boolean.class) etc., in which case getOptionList will return {"a", "b", "c"}
Example usage: Opts opts = new Opts(args); String iniFile = opts.getOption("-ini", ""); if ( iniFile.length()==0 ) { usage(); } String foo = opts.getOption("-foo"); int ifoo = opts.getOptionInt("-foo", 4); System.out.println("getOptionBool(\"-bar\")=" +opts.getOptionBool("-bar"); String[] lastArgs = opts.getOptionList(""); String [] list = opts.getOptionList("-list"); Example 2: Opts opts = new Opts(args); opts.addOption("-foo", Integer.class, true); opts.addOption("-bar", true); String err = opts.validate(); if ( err.length()>0 ) { System.out.println(err); System.exit(1); }
For more examples, see com.dynamide/util/Opts.java and run the default test cases:
java com.dynamide.util.Optsor run your own tests:
java com.dynamide.util.Opts -my -test -case foo bar
Nested Class Summary | |
---|---|
class |
Opts.Option
|
Constructor Summary | |
---|---|
Opts(java.lang.String[] args)
|
Method Summary | |
---|---|
void |
addOption(java.lang.String key,
java.lang.Class type,
boolean required)
Declare options to have types and be required/optional. |
java.lang.String |
dump()
|
java.lang.String |
getArgAt(int index)
|
java.lang.String |
getOption(java.lang.String opt)
Get the string value of option named by key, else return. "" Also, allow getOption("foo") and getOption("-foo"). |
java.lang.String |
getOption(java.lang.String key,
java.lang.String defaultValue)
Get the string value of option named by key, else return default e.g. |
boolean |
getOptionBool(java.lang.String opt)
Return true if option present, false if option is absent. |
int |
getOptionInt(java.lang.String opt,
int defaultValue)
Return integer value if option present and option parameter is a legal integer string. |
java.lang.String[] |
getOptionList()
Same as calling getOptionList(""); |
java.lang.String[] |
getOptionList(java.lang.String key)
Gets the option list specified by key, that is, all the args after -key until next "-" (start of a switch or --) or the end. |
java.lang.String |
getRemainingArgAt(int index)
|
java.lang.String[] |
getRemainingArgs()
This is always the last arguments on the command line that are not switches, except that the leading value may be "consumed" by a String switch. |
int |
length()
|
static void |
main(java.lang.String[] args)
Calls lots of test cases if called with no command line args, else processes command line args and shows some tests and a dump of processed state. |
java.lang.String |
toString()
|
java.lang.String |
validate()
Call to getting the first error string when validatating based on parameter types and requirements passed in addOption. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Opts(java.lang.String[] args)
Method Detail |
---|
public java.lang.String toString()
toString
in class java.lang.Object
public int length()
public java.lang.String getOption(java.lang.String key, java.lang.String defaultValue)
public java.lang.String getOption(java.lang.String opt)
public boolean getOptionBool(java.lang.String opt)
public int getOptionInt(java.lang.String opt, int defaultValue)
public java.lang.String[] getOptionList()
public java.lang.String[] getOptionList(java.lang.String key)
java MyClass -list a b c -foo mojo
getOptionList("-list") ==> a b c
getOptionList() ==> []
getOptionList("-foo") ==> mojo
java MyClass -list a b c -foo mojo nixon
getOptionList("-list") ==> a b c
getOptionList() ==> nixon
getOptionList("-foo") ==> mojo nixon
java MyClass a b c , then
getOptionList("") ==> a b c
public java.lang.String[] getRemainingArgs()
"-foo a b"
, and -foo is not declared to
be Boolean by a call to addOption(), then b is "remaining", (and -foo will have the value "a").addOption("-foo", Boolean.class)
-foo -- a b
public java.lang.String getRemainingArgAt(int index)
public java.lang.String getArgAt(int index)
public void addOption(java.lang.String key, java.lang.Class type, boolean required)
type
- A Class type -- Legal values are: Boolean.class, Integer.class, and String.classpublic java.lang.String validate()
public java.lang.String dump()
public static void main(java.lang.String[] args)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |