Class Args
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<String,
Object>
Used to parse command-line arguments of the form
The format of a main argument is a token that does not start with
The format of an optional argument is
Command-line examples
java com.sample.MyClass mainArg1 java com.sample.MyClass mainArg1 mainArg2 java com.sample.MyClass mainArg1 -optArg1 java com.sample.MyClass -optArg1 java com.sample.MyClass mainArg1 -optArg1 optArg1Val java com.sample.MyClass mainArg1 -optArg1 optArg1Val1 optArg1Val2 java com.sample.MyClass mainArg1 -optArg1 optArg1Val1 -optArg1 optArg1Val2
Example:
Main arguments are available through numeric string keys (e.g. JsonMap
API to convert main arguments directly to POJOs, such as an
Equivalent operations are available on optional arguments through the getArg(Class, String)
method.
Notes:
- This class is not thread safe.
See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,
V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiongetArg
(int i) Returns main argument at the specified index, ornull if the index is out of range.<T> T
Returns the optional argument value converted to the specified object type.Returns the optional argument value, or blank if the optional argument was not specified.Returns the optional argument values as a list of strings.boolean
hasArg
(int i) Returnstrue if argument exists at specified index.boolean
Returnstrue if the named argument exists.Methods inherited from class org.apache.juneau.collections.JsonMap
append, append, appendFirst, appendIf, appendIf, appendIfAbsent, appendIfAbsentIf, asJson, asReadableString, asString, asString, cast, cast, containsKey, containsKeyNotEmpty, containsOuterKey, create, deleteAt, entrySet, exclude, filtered, filtered, filteredMap, filteredMap, find, find, findBoolean, findInt, findKeyIgnoreCase, findList, findLong, findMap, findString, get, get, get, getAt, getAt, getBeanSession, getBoolean, getBoolean, getClassMeta, getFirstKey, getInt, getInt, getList, getList, getList, getList, getLong, getLong, getMap, getMap, getMap, getMap, getString, getString, getStringArray, getStringArray, getSwapped, getWithDefault, getWithDefault, getWithDefault, include, inner, isUnmodifiable, keepAll, keySet, modifiable, of, of, ofJson, ofJson, ofText, ofText, postAt, put, putAt, putJson, removeAll, removeAll, removeBoolean, removeBoolean, removeInt, removeInt, removeString, removeString, removeWithDefault, session, setBeanSession, toString, unmodifiable, writeTo
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, forEach, getOrDefault, removeEldestEntry, replaceAll, values
Methods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, size
Methods inherited from class java.util.AbstractMap
equals, hashCode
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, hashCode, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, size
-
Constructor Details
-
Args
Constructor.- Parameters:
args
- Arguments passed in through amain(String[] args) method.
-
Args
Constructor.- Parameters:
args
- Arguments passed in as a raw command line.
-
-
Method Details
-
getArg
Returns main argument at the specified index, ornull if the index is out of range.Can be used in conjunction with
hasArg(int)
to check for existence of arg.// Check for no arguments if (!args .hasArg(0))printUsageAndExit ();// Get the first argument StringfirstArg =args .getArg(0);Since main arguments are stored as numeric keys, this method is essentially equivalent to...
// Check for no arguments if (!args .containsKey("0" ))printUsageAndExit ();// Get the first argument StringfirstArg =args .getString("0" );- Parameters:
i
- The index position of the main argument (zero-indexed).- Returns:
- The main argument value, or
"" if argument doesn't exist at that position.
-
hasArg
Returnstrue if argument exists at specified index.- Parameters:
i
- The zero-indexed position of the argument.- Returns:
true if argument exists at specified index.
-
hasArg
Returnstrue if the named argument exists.- Parameters:
name
- The argument name.- Returns:
true if the named argument exists.
-
getArg
Returns the optional argument value, or blank if the optional argument was not specified.If the optional arg has multiple values, returns values as a comma-delimited list.
- Parameters:
name
- The optional argument name.- Returns:
- The optional argument value, or blank if the optional argument was not specified.
-
getArg
Returns the optional argument value converted to the specified object type.If the optional arg has multiple values, returns only the first converted value.
Example:
// Command: java com.sample.MyClass -verbose true -debug 5 boolean bool =args .getArg(boolean .class ,"verbose" );int _int =args .getArg(int .class ,"debug" );- Type Parameters:
T
- The class type to convert the value to.- Parameters:
c
- The class type to convert the value to.name
- The optional argument name.- Returns:
- The optional argument value, or blank if the optional argument was not specified.
-
getArgs
Returns the optional argument values as a list of strings.Example:
// Command: java com.sample.MyClass -extraArgs foo bar baz List<String>list1 =args .getArgs("extraArgs" );// ['foo','bar','baz'] List<String>list2 =args .getArgs("nonExistentArgs" );// An empty list - Parameters:
name
- The optional argument name.- Returns:
- The optional argument values, or an empty list if the optional argument was not specified.
-