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.
-
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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds an entry to this map.Appends all the entries in the specified map to this map.Add if flag istrue .Enables filtering based on a predicate test.getArg(int i) Returns main argument at the specified index, ornull if the index is out of range.<T> TReturns 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.booleanhasArg(int i) Returnstrue if argument exists at specified index.booleanReturnstrue if the named argument exists.Set an inner map in this map to allow for chained get calls.The opposite ofJsonMap.removeAll(String...).Returns a modifiable copy of this map if it's unmodifiable.session(BeanSession session) Override the default bean session used for converting POJOs.setBeanSession(BeanSession value) Sets theBeanSessioncurrently associated with this map.Returns an unmodifiable copy of this map if it's modifiable.Methods inherited from class org.apache.juneau.collections.JsonMap
appendFirst, appendIf, appendIfAbsent, appendIfAbsentIf, asJson, asReadableString, asString, asString, cast, cast, containsKey, containsKeyNotEmpty, containsOuterKey, create, deleteAt, entrySet, exclude, 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, isUnmodifiable, keySet, of, of, ofJson, ofJson, ofText, ofText, postAt, put, putAt, putJson, removeAll, removeAll, removeBoolean, removeBoolean, removeInt, removeInt, removeString, removeString, removeWithDefault, toString, writeToMethods inherited from class java.util.LinkedHashMap
clear, containsValue, forEach, getOrDefault, removeEldestEntry, replaceAll, valuesMethods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, sizeMethods inherited from class java.util.AbstractMap
equals, hashCodeMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods 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 as a raw command line.
-
Args
Constructor.- Parameters:
args- Arguments passed in through amain(String[] args) method.
-
-
Method Details
-
append
Description copied from class:JsonMapAppends all the entries in the specified map to this map. -
append
Description copied from class:JsonMapAdds an entry to this map. -
appendIf
Description copied from class:JsonMapAdd if flag istrue . -
filtered
Description copied from class:JsonMapEnables filtering based on a predicate test.If the predicate evaluates to
false on values added to this map, the entry will be skipped. -
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.
-
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.
-
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.
-
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.
-
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.
-
inner
Description copied from class:JsonMapSet an inner map in this map to allow for chained get calls.If
JsonMap.get(Object)returnsnull , thenJsonMap.get(Object)will be called on the inner map.In addition to providing the ability to chain maps, this method also provides the ability to wrap an existing map inside another map so that you can add entries to the outer map without affecting the values on the inner map.
JsonMap
map1 = JsonMap.ofJson ("{foo:1}" ); JsonMapmap2 = JsonMap.of ().setInner(map1 );map2 .put("foo" , 2);// Overwrite the entry int foo1 =map1 .getInt("foo" );// foo1 == 1 int foo2 =map2 .getInt("foo" );// foo2 == 2 -
keepAll
Description copied from class:JsonMapThe opposite ofJsonMap.removeAll(String...).Discards all keys from this map that aren't in the specified list.
-
modifiable
Description copied from class:JsonMapReturns a modifiable copy of this map if it's unmodifiable.- Overrides:
modifiablein classJsonMap- Returns:
- A modifiable copy of this map if it's unmodifiable, or this map if it is already modifiable.
-
session
Description copied from class:JsonMapOverride the default bean session used for converting POJOs.Default is
BeanContext.DEFAULT, which is sufficient in most cases.Useful if you're serializing/parsing beans with transforms defined.
-
setBeanSession
Description copied from class:JsonMapSets theBeanSessioncurrently associated with this map.- Overrides:
setBeanSessionin classJsonMap- Parameters:
value- TheBeanSessioncurrently associated with this map.- Returns:
- This object.
-
unmodifiable
Description copied from class:JsonMapReturns an unmodifiable copy of this map if it's modifiable.- Overrides:
unmodifiablein classJsonMap- Returns:
- An unmodifiable copy of this map if it's modifiable, or this map if it is already unmodifiable.
-