Class PyArgParser
java.lang.Object
com.inductiveautomation.ignition.common.script.PyArgParser
Roughly equivalent to the older
PyArgumentMap, but with stricter validation of types and some more "modern"
conveniences.-
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsKey(String keyword) Retrieve the argument specified bykeywordas a plain list.Retrieve the argument specified bykeywordas a typed list ofclazz.Retrieve the argument specified bykeywordas an untyped map.Retrieve the argument specified bykeywordas a typed map.getBoolean(String keyword) Getskeyword, if and only if it was supplied as a boolean or number.Getskeyword, if and only if it was supplied as a Date.Getskeyword, if and only if it was supplied as a double.Getskeyword, if and only if it was supplied as a float.getInteger(String keyword) Getskeyword, if and only if it was supplied as an integer.Getskeyword, if and only if it was supplied as a long.getNullableBoolean(String keyword) Retrieve argumentkeyword, if and only if it was supplied as a Boolean or number.getNullableDate(String keyword) Retrieve argumentkeyword, if and only if it was supplied as a Date.getNullableDouble(String keyword) Retrieve argumentkeyword, if and only if it was supplied as a Double.getNullableFloat(String keyword) Retrieve argumentkeyword, if and only if it was supplied as a Float.getNullableInteger(String keyword) Retrieve argumentkeyword, if and only if it was supplied as a Integer.getNullableLong(String keyword) Retrieve argumentkeyword, if and only if it was supplied as a Long.getNullableString(String keyword) Retrieve argumentkeyword, if and only if it was supplied as a String.<T> TgetNullSafe(String keyword, Class<T> clazz) Optional<org.python.core.PyObject>getPyObject(String keyword) Getskeyword, directly as a PyObject; i.e., without doing any casting.Getskeyword, if and only if it was supplied as a String.<T> Optional<T>Ifkeywordwas supplied, return it (type-safe) or throw a TypeError.static PyArgParserparseArgs(org.python.core.PyObject[] args, String[] keywords, Class<?> callingClass, String methodName) Similar toPyArgumentMap.interpretPyArgs(PyObject[], String[], Class, String), but with more explicit type-checking behavior.static PyArgParserparseArgs(org.python.core.PyObject[] args, String[] keywords, String[] expectedKeywords, Class<?>[] expectedTypes, String functionName) Similar toPyArgumentMap.interpretPyArgs(PyObject[], String[], String[], Class[]), but with more explicit type-checking behavior.booleanrequireBoolean(String keyword) Retrieve required argumentkeyword, if and only if it was supplied as a boolean or number.requireDate(String keyword) Retrieve required argumentkeyword, if and only if it was supplied as a Date.doublerequireDouble(String keyword) Retrieve required argumentkeyword, if and only if it was supplied as a double.floatrequireFloat(String keyword) Retrieve required argumentkeyword, if and only if it was supplied as a float.intrequireInteger(String keyword) Retrieve required argumentkeyword, if and only if it was supplied as an integer.requireList(String keyword) Retrieve required argumentkeywordif and only if it was supplied as a list.<T> List<T>requireList(String keyword, Class<T> clazz) Retrieve required argumentkeywordif and only if it was supplied as a list ofclazz.longrequireLong(String keyword) Retrieve required argumentkeyword, if and only if it was supplied as a long.org.python.core.PyObjectrequirePyObject(String keyword) Retrievekeyword, directly as a PyObject; i.e., without doing any casting.requireString(String keyword) Retrieve required argumentkeyword, if and only if it was supplied as a String.<T> TrequireTyped(String keyword, Class<T> clazz) Retrieve required argumentkeyword, if and only if the supplied value can be coerced toclazz.
-
Method Details
-
parseArgs
public static PyArgParser parseArgs(org.python.core.PyObject[] args, String[] keywords, String[] expectedKeywords, Class<?>[] expectedTypes, String functionName) Similar toPyArgumentMap.interpretPyArgs(PyObject[], String[], String[], Class[]), but with more explicit type-checking behavior.- Parameters:
args- args from a function callkeywords- keyword names from a function callexpectedKeywords- expected keyword argumentsexpectedTypes- expected argument typesfunctionName- the name of the function; used for better contextual error messages- Returns:
- a PyArgParser instance
- Throws:
IllegalArgumentException- if the provided method cannot be found, or is missing the KeywordArgs annotation.AssertionError- ifkeywordsis shorter thanargs, or the list of expected keywords doesn't match the list of expected types.
-
parseArgs
public static PyArgParser parseArgs(org.python.core.PyObject[] args, String[] keywords, Class<?> callingClass, String methodName) Similar toPyArgumentMap.interpretPyArgs(PyObject[], String[], Class, String), but with more explicit type-checking behavior.- Parameters:
args- args from a function callkeywords- keyword names from a function callcallingClass- the class the method is contained in. Will be checked reflectively for theKeywordArgsannotation.methodName- the name of the method. Used to look up annotations reflectively, and for thrown errors.- Returns:
- a PyArgParser instance
- Throws:
IllegalArgumentException- if the provided method cannot be found, or is missing the KeywordArgs annotation.AssertionError- ifkeywordsis shorter thanargs, or the list of expected keywords doesn't match the list of expected types.org.python.core.PyException- TypeError if more arguments are provided than expected argument names
-
getTyped
Ifkeywordwas supplied, return it (type-safe) or throw a TypeError. If not specified or None was supplied, returns an empty optional.- Throws:
org.python.core.PyException- TypeError if the supplied value cannot be coerced toclazz.IllegalArgumentException- if the provided keyword is not an expected argument for the function
-
requireTyped
Retrieve required argumentkeyword, if and only if the supplied value can be coerced toclazz.- Returns:
- the retrieved value of
keywordcoerced toclazz. - Throws:
org.python.core.PyException- TypeError if the supplied value cannot be coerced toclazz.IllegalArgumentException- if the provided keyword is not an expected argument for the function.
-
getNullSafe
-
getBoolean
Getskeyword, if and only if it was supplied as a boolean or number.- Returns:
- the boolean value of
keyword, if supplied, or an empty optional. - Throws:
org.python.core.PyException- TypeError if the supplied value was not a boolean.IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
requireBoolean
Retrieve required argumentkeyword, if and only if it was supplied as a boolean or number.- Returns:
- the boolean value of
keyword - Throws:
org.python.core.PyException- TypeError if the supplied value was not a boolean, or was not specified.IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
getNullableBoolean
Retrieve argumentkeyword, if and only if it was supplied as a Boolean or number. UnlikerequireBoolean(String), will accept a supplied value ofPyNoneand return null.- Returns:
- the Boolean value of
keyword, or null. - Throws:
org.python.core.PyException- TypeError if the supplied value was not a BooleanIllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
getInteger
Getskeyword, if and only if it was supplied as an integer.- Returns:
- the integer value of
keyword, if supplied, or an empty optional. - Throws:
org.python.core.PyException- TypeError if the supplied value was not a integer.IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
requireInteger
Retrieve required argumentkeyword, if and only if it was supplied as an integer.- Returns:
- the integer value of
keyword - Throws:
org.python.core.PyException- TypeError if the supplied value was not a integer, or was not specified.IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
getNullableInteger
Retrieve argumentkeyword, if and only if it was supplied as a Integer. UnlikerequireInteger(String), will accept a supplied value ofPyNoneand return null.- Returns:
- the Integer value of
keyword, or null. - Throws:
org.python.core.PyException- TypeError if the supplied value was not a IntegerIllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
getLong
Getskeyword, if and only if it was supplied as a long.- Returns:
- the long value of
keyword, if supplied, or an empty optional. - Throws:
org.python.core.PyException- TypeError if the supplied value was not a long.IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
requireLong
Retrieve required argumentkeyword, if and only if it was supplied as a long.- Returns:
- the long value of
keyword - Throws:
org.python.core.PyException- TypeError if the supplied value was not a long, or was not specified.IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
getNullableLong
Retrieve argumentkeyword, if and only if it was supplied as a Long. UnlikerequireLong(String), will accept a supplied value ofPyNoneand return null.- Returns:
- the Long value of
keyword, or null. - Throws:
org.python.core.PyException- TypeError if the supplied value was not a LongIllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
getFloat
Getskeyword, if and only if it was supplied as a float.- Returns:
- the float value of
keyword, if supplied, or an empty optional. - Throws:
org.python.core.PyException- TypeError if the supplied value was not a float.IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
requireFloat
Retrieve required argumentkeyword, if and only if it was supplied as a float.- Returns:
- the float value of
keyword - Throws:
org.python.core.PyException- TypeError if the supplied value was not a float, or was not specified.IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
getNullableFloat
Retrieve argumentkeyword, if and only if it was supplied as a Float. UnlikerequireFloat(String), will accept a supplied value ofPyNoneand return null.- Returns:
- the Float value of
keyword, or null. - Throws:
org.python.core.PyException- TypeError if the supplied value was not a FloatIllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
getDouble
Getskeyword, if and only if it was supplied as a double.- Returns:
- the double value of
keyword, if supplied, or an empty optional. - Throws:
org.python.core.PyException- TypeError if the supplied value was not a double.IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
requireDouble
Retrieve required argumentkeyword, if and only if it was supplied as a double.- Returns:
- the double value of
keyword - Throws:
org.python.core.PyException- TypeError if the supplied value was not a double, or was not specified.IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
getNullableDouble
Retrieve argumentkeyword, if and only if it was supplied as a Double. UnlikerequireDouble(String), will accept a supplied value ofPyNoneand return null.- Returns:
- the Double value of
keyword, or null. - Throws:
org.python.core.PyException- TypeError if the supplied value was not a DoubleIllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
getDate
Getskeyword, if and only if it was supplied as a Date.- Returns:
- the Date value of
keyword, if supplied, or an empty optional. - Throws:
org.python.core.PyException- TypeError if the supplied value was not a Date.IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
requireDate
Retrieve required argumentkeyword, if and only if it was supplied as a Date.- Returns:
- the Date value of
keyword - Throws:
org.python.core.PyException- TypeError if the supplied value was not a Date, or was not specified.IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
getNullableDate
Retrieve argumentkeyword, if and only if it was supplied as a Date. UnlikerequireDate(String), will accept a supplied value ofPyNoneand return null.- Returns:
- the Date value of
keyword, or null. - Throws:
org.python.core.PyException- TypeError if the supplied value was not a DateIllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
getString
Getskeyword, if and only if it was supplied as a String.- Returns:
- the String value of
keyword, if supplied, or an empty optional. - Throws:
org.python.core.PyException- TypeError if the supplied value was not a String.IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
requireString
Retrieve required argumentkeyword, if and only if it was supplied as a String.- Returns:
- the String value of
keyword - Throws:
org.python.core.PyException- TypeError if the supplied value was not a String, or was not specified.IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
getNullableString
Retrieve argumentkeyword, if and only if it was supplied as a String. UnlikerequireString(String), will accept a supplied value ofPyNoneand return null.- Returns:
- the String value of
keyword, or null. - Throws:
org.python.core.PyException- TypeError if the supplied value was not a StringIllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
getPyObject
Getskeyword, directly as a PyObject; i.e., without doing any casting.- Returns:
- the PyObject value of
keyword, if supplied, or an empty optional. Can bePy.Noneif None was supplied. - Throws:
IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
requirePyObject
Retrievekeyword, directly as a PyObject; i.e., without doing any casting.- Returns:
- the PyObject value of
keyword - Throws:
org.python.core.PyException- TypeError if the supplied value was not provided.IllegalArgumentException- if the provided keyword is not an expected argument for the function- See Also:
-
containsKey
-
getAsList
Retrieve the argument specified bykeywordas a typed list ofclazz. Will be a shallow copy.- Throws:
org.python.core.PyException-- TypeError if any elements of the PyList supplied as an argument can't be coerced to
clazz- TypeError if the supplied PyObject is not iterable
- TypeError if any elements of the PyList supplied as an argument can't be coerced to
-
requireList
Retrieve required argumentkeywordif and only if it was supplied as a list ofclazz. Will be a shallow copy.- Throws:
org.python.core.PyException-- TypeError if any elements of the PyList supplied as an argument can't be coerced to
clazz - TypeError if the supplied PyObject is not iterable
- TypeError if any elements of the PyList supplied as an argument can't be coerced to
IllegalArgumentException- if the provided keyword is not an expected argument for the function
-
getAsList
Retrieve the argument specified bykeywordas a plain list. Will be a shallow copy.- Throws:
org.python.core.PyException-- TypeError if the supplied PyObject is not iterable
-
requireList
Retrieve required argumentkeywordif and only if it was supplied as a list. Will be a shallow copy.- Throws:
org.python.core.PyException-- TypeError if the supplied PyObject is not iterable
IllegalArgumentException- if the provided keyword is not an expected argument for the function
-
getAsMap
Retrieve the argument specified bykeywordas a typed map. Will be a shallow copy.- Throws:
org.python.core.PyException-- TypeError if any elements of the PyDictionary supplied as an argument can't be coerced to the right class
- TypeError if the supplied PyObject is not iterable
-
getAsMap
Retrieve the argument specified bykeywordas an untyped map. Will be a shallow copy.- Throws:
org.python.core.PyException-- TypeError if the supplied PyObject is not iterable
-