public abstract class Arguments
extends java.lang.Object
public static class Args extends Arguments { /**Argument is manually tested * / public String sTitle; /**Argument needs a Arguments.SetArgument(...) operation, longer form * / public File fOut; /**Argument contains the timestamp.val to use as value, shorter form * / public Argument timestamp = new Argument("-time", ":yyyy-MM-dd+hh:mm sets a timestamp in UTC (GMT)"); Arguments.SetArgument setOutput = new Arguments.SetArgument(){ @Override public boolean setArgument(String val){ Args.this.fOut = new File(val); return true; }}; Args(){ super.aboutInfo = "...your about info"; super.helpInfo="obligate args: -o:..."; addArg(new Argument("-o", ":path/to/output.file", this.setOutput)); addArg(timestamp); } /**This operation is necessary only for manual test of argument Strings. * / (at)Override protected boolean testArgument(String arg, int nArg) { boolean bOk = true; //set to false if the argc is not passed String value; if( (value = checkArgVal("-title", arg)) !=null) { this.sTitle = value; //the graphic GUI-appearance } else { bOk = super.testArgument(arg, nArg); } return bOk; } /**This operation checks the consistence of all operations. * / (at)Override public boolean testArgs(Appendable msg) throws IOException { boolean bOk = true; if(this.fOut == null) { msg.append("-o:outfile obligate\n"); bOk = false; } if(!bOk) { super.showHelp(msg); } return bOk; } }You should instantiate in your application:
final Args argData;In the main you should call:
public static void main(String[] cmdArgs) { Args args = new Args(); // an inherit class of Arguments if( args.parseArgs(cmdArgs, System.err) // returns true if no argument error && args.testConsistence(System.err) // returns true if all arguments are proper ) { // then execute: smain(args); // internal smain with prepared Args } }Now the
smain(args)
is also usable inside another Java program, with given Args:public static void smain(Args args) { try { MyAppl main = new MyAppl(args); //should store the args in a final reference thiz.exec(); }
-x:...
---comment
simple comment args with three minus
--@path/to/file
read arguments from a file, whereas one line is one argument.
--@path/to/file:label
read arguments from a part of a file after the label, see next.
java -cp classpath pkg.path.Class --@localfile.bat:myLabel ::myLabel ## ::-o:path/to/myoutput ##That is the output file for xyz ::inputfile pauseThis is an example/pattern for a batch file, which contains the arguments one per line in the argument file. This class supports also a comment for the arguments. The comment start string is given after the label, whereby spaces will be omitted (trimmed). One space after the label means that also trailing spaces in the non commented lines will be trimmed.
Modifier and Type | Class and Description |
---|---|
static class |
Arguments.Argument
Class to describe one argument.
|
static interface |
Arguments.SetArgument
Interface for implementation of setting arguments.
|
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
aboutInfo |
protected java.util.List<Arguments.Argument> |
argList |
static int |
exitCodeArgError |
protected java.lang.String |
helpInfo |
(package private) java.lang.Appendable |
outMsg |
(package private) java.lang.String |
sLogLevel |
(package private) java.lang.String |
sLogPath |
static java.lang.String |
sVersion
Version, history and license.
|
Constructor and Description |
---|
Arguments() |
Modifier and Type | Method and Description |
---|---|
java.lang.String |
aboutInfo() |
protected void |
addArg(Arguments.Argument arg) |
protected boolean |
checkArg(java.lang.String check,
java.lang.String arg)
Checks whether an option arg is given without argument value.
|
protected java.lang.String |
checkArgVal(java.lang.String check,
java.lang.String arg)
Check whether an arg with value is given.
|
boolean |
parseArgs(java.lang.String[] args)
This is the user operation to process all arguments from a container as String[].
|
boolean |
parseArgs(java.lang.String[] args,
java.lang.Appendable errMsg)
This is the user operation to process all arguments from a container as String[].
|
void |
readArguments(java.lang.String[] cmdArgs)
Read arguments from given command line arguments.
|
static java.lang.String |
replaceEnv(java.lang.String argval)
Replaces expressions "...
|
void |
showHelp(java.lang.Appendable out)
Writes all arguments with
Argument#arg Arguments.Argument.help in its order in one in a line. |
protected boolean |
testArgument(java.lang.String argc,
int nArg)
This operation tests one argument maybe from a container as String[].
|
abstract boolean |
testConsistence(java.lang.Appendable msg)
This operation should be implemented and called by the user.
|
private boolean |
tryTestArgument(java.lang.String argc,
int nArg,
java.lang.Appendable errMsg,
java.io.Closeable farg)
Wraps
testArgument(String, int) in try-catch to catch a user Exception while evaluating argument strings. |
public static final java.lang.String sVersion
parseArgs(String[], Appendable)
is now exception free, more simple in handling.
If the helpInfo
starts with "!" then it will be written on emtpy arguments.
With that changes the usage is more simple, see example in javadoc to the class.
replaceEnv(String)
: If it starts with "/tmp/" it substitutes the TMP on windows.
readArguments(String[])
as ready to usable in main().
checkArgVal(String, String)
improved, see comments in code there. Accept spaces
testConsistence(Appendable)
instead testArgs(...), more expressive. Adaption necessary and done in all vishia Files
Arguments.Argument
can now also contain the value itself, more simple for pure String arguments.
replaceEnv(String)
with also $identifier
MainCmd
.
protected java.lang.String aboutInfo
protected java.lang.String helpInfo
protected java.util.List<Arguments.Argument> argList
public static int exitCodeArgError
java.lang.String sLogPath
java.lang.String sLogLevel
java.lang.Appendable outMsg
protected final void addArg(Arguments.Argument arg)
public static java.lang.String replaceEnv(java.lang.String argval)
argval
- String with environment variables to replace.java.lang.IllegalArgumentException
- on faulty name of environment variableprotected boolean testArgument(java.lang.String argc, int nArg) throws java.io.FileNotFoundException
super.textArgument(argc, nArg);This operation is only called in
tryTestArgument(String, int, Appendable, Closeable)
,
which is called in parseArgs(String[], Appendable)
and nothing else.
It is protected to prevent faulty calling.
argc
- The given argumentnArg
- position of the argument in the container, counted from 0argList
given on ctorjava.io.FileNotFoundException
- If the argument is formally accepted but evaluation cause an exceptionFileNotFoundException
accepted by user implementations.protected final boolean checkArg(java.lang.String check, java.lang.String arg)
checkArgVal(String, String)
.
The arg should be identically with checkcheck
- arg
- protected final java.lang.String checkArgVal(java.lang.String check, java.lang.String arg)
check
is the given check String.
check
- The start sequence of the argarg
- given one argument, either one line in arg file, or sequence till space in cmd linevalue
it is matching.public final boolean parseArgs(java.lang.String[] args) throws java.io.IOException
main(String[] args)
args
- The argument string array.java.io.IOException
java.lang.IllegalArgumentException
- on argument errorpublic final boolean parseArgs(java.lang.String[] args, java.lang.Appendable errMsg)
main(String[] args)
args
- The argument string array.
If the helpInfo
starts with "!" the help info is shown on empty arguments.errMsg
- if given then all arguments will be parsed. Errors will be output here.testConsistence(Appendable)
afterwards.
false argument error, the application may be used though if testConsistence(Appendable)
returns true.java.io.IOException
- only on unexpected errors writing errMsgjava.lang.IllegalArgumentException
- on argument error only if errMsg == nullprivate final boolean tryTestArgument(java.lang.String argc, int nArg, java.lang.Appendable errMsg, java.io.Closeable farg) throws java.io.IOException
testArgument(String, int)
in try-catch to catch a user Exception while evaluating argument strings.argc
- nArg
- errMsg
- if given, outputs the errorfarg
- if given, close it on error if error is not given, before an IllegalArgumentException
is thrownjava.io.IOException
- only on formally IO error on errMsgjava.lang.IllegalArgumentException
- if errMsg==null and the argument is faulty.public void showHelp(java.lang.Appendable out)
Argument#arg
Arguments.Argument.help
in its order in one in a line.out
- Any output channeljava.io.IOException
- only on unexpected problems with outpublic java.lang.String aboutInfo()
public abstract boolean testConsistence(java.lang.Appendable msg) throws java.io.IOException
#showArgs(Appendable)
to output the help info on error.msg
- to write out an info as line with \n for faulty arguments. System.err
can be used.java.io.IOException
- only on unexpected problems writing msgpublic void readArguments(java.lang.String[] cmdArgs)
parseArgs(String[], Appendable)
and testConsistence(Appendable)
cmdArgs
-