Package org.python.core
Class ArgParser
java.lang.Object
org.python.core.ArgParser
A utility class for handling mixed positional and keyword arguments.
Typical usage:
public MatchObject search(PyObject[] args, String[] kws) { ArgParser ap = new ArgParser("search", args, kws, "pattern", "pos", "endpos"); String string = ap.getString(0); int start = ap.getInt(1, 0); int end = ap.getInt(2, string.length()); ...
-
Constructor Summary
ConstructorDescriptionCreate an ArgParser for a one-argument function.Create an ArgParser for a multi-argument function.ArgParser
(String funcname, PyObject[] args, String[] kws, String[] paramnames, int minargs, boolean takesZeroArgs) Create an ArgParser for a two-argument function.Create an ArgParser for a three-argument function. -
Method Summary
Modifier and TypeMethodDescriptionint
getIndex
(int pos) Return an required argument as an index.int
getIndex
(int pos, int def) Return an optional argument as an index.int
getInt
(int pos) Return a required argument as an int.int
getInt
(int pos, int def) Return an optional argument as an int.getList
(int pos) Return the remaining arguments as a tuple.getPyObject
(int pos) Return a required argument as a PyObject.getPyObject
(int pos, PyObject def) Return an optional argument as a PyObject.getPyObjectByType
(int pos, PyType type) Return a required argument as a PyObject, ensuring the object is of the specified type.getPyObjectByType
(int pos, PyType type, PyObject def) Return an optional argument as a PyObject, or return the default value provided, which may benull
.getString
(int pos) Return a required argument as a String.Return an optional argument as a String.void
Ensure no keyword arguments were passed, raising a TypeError if so.
-
Constructor Details
-
ArgParser
Create an ArgParser for a one-argument function.- Parameters:
funcname
- Name of the function. Used in error messages.args
- The actual call arguments supplied in the call.kws
- The actual keyword names supplied in the call.p0
- The expected argument in the function definition.
-
ArgParser
Create an ArgParser for a two-argument function.- Parameters:
funcname
- Name of the function. Used in error messages.args
- The actual call arguments supplied in the call.kws
- The actual keyword names supplied in the call.p0
- The first expected argument in the function definition.p1
- The second expected argument in the function definition.
-
ArgParser
Create an ArgParser for a three-argument function.- Parameters:
funcname
- Name of the function. Used in error messages.args
- The actual call arguments supplied in the call.kws
- The actual keyword names supplied in the call.p0
- The first expected argument in the function definition.p1
- The second expected argument in the function definition.p2
- The third expected argument in the function definition.
-
ArgParser
Create an ArgParser for a multi-argument function.- Parameters:
funcname
- Name of the function. Used in error messages.args
- The actual call arguments supplied in the call.kws
- The actual keyword names supplied in the call.paramnames
- The list of expected argument in the function definition.
-
ArgParser
-
ArgParser
-
-
Method Details
-
getString
Return a required argument as a String.- Parameters:
pos
- The position of the .. First argument is numbered 0.
-
getString
Return an optional argument as a String.- Parameters:
pos
- The position of the argument. First argument is numbered 0.
-
getInt
public int getInt(int pos) Return a required argument as an int.- Parameters:
pos
- The position of the argument. First argument is numbered 0.
-
getInt
public int getInt(int pos, int def) Return an optional argument as an int.- Parameters:
pos
- The position of the argument. First argument is numbered 0.
-
getIndex
public int getIndex(int pos) Return an required argument as an index.- Parameters:
pos
- The position of the argument. First argument is numbered 0.
-
getIndex
public int getIndex(int pos, int def) Return an optional argument as an index.- Parameters:
pos
- The position of the argument. First argument is numbered 0.
-
getPyObject
Return a required argument as a PyObject.- Parameters:
pos
- The position of the argument. First argument is numbered 0.
-
getPyObject
Return an optional argument as a PyObject.- Parameters:
pos
- The position of the argument. First argument is numbered 0.
-
getPyObjectByType
Return a required argument as a PyObject, ensuring the object is of the specified type.- Parameters:
pos
- the position of the argument. First argument is numbered 0type
- the desired PyType of the argument- Returns:
- the PyObject of PyType type
-
getPyObjectByType
Return an optional argument as a PyObject, or return the default value provided, which may benull
. If the returned value is notnull
, it must be of the specified type.- Parameters:
pos
- the position of the argument. First argument is numbered 0type
- the desired PyType of the argumentdef
- to return if the argument at pos was not given (null allowed)- Returns:
- the PyObject of PyType type
-
getList
Return the remaining arguments as a tuple.- Parameters:
pos
- The position of the argument. First argument is numbered 0.
-
noKeywords
public void noKeywords()Ensure no keyword arguments were passed, raising a TypeError if so.
-