Python-ArgParse (Method)
The python-argparse method is a function method that launches a Python script in response to an API request and returns any information that the script writes to the console.
While the standard 'python' version of this method passes all of the parameters in a single JSON file, this version instead provides each parameter on the command-line, suitable for reading in with the 'argparse' library.
Captured Output
To allow for diagnostic and debugging messages, the output returned to the user is split by separators. By default, this is a set of four minus-signs ('—-').
If these separators are present in the output of your program, only the text after the last separator will be sent.
So in the following output…
Hello World Sending Query.... Running Analytics... ---- Time, Value 10:00:00,10 10:00:10,11 10:00:20,12
…only the lower half (from 'Time,Value') would actually be sent to the user.
If there are no separators found, the system returns the full output of your script.
How It Works
For this method, each user-provided parameter is passed as a command-line parameter. The format used is two dashes (–) followed by the parameter tag name.
For example, the following info.json file…
{
"name": "Testing",
"method": "python",
"path": "[ROOT]/myscript.py",
"parameters": [{
"name": "Message",
"type": "string",
"tag": "msg",
"default": "Hello World"
}].
"folder": "Testing"
}
…would result in the following steps if it was called with the default parameter values.
Calling the Function
The python code would then be run with the following command…
python c:\ARDI\web\sites\default\functions\testing\myscript.py --msg="Hello World"
Capturing Output
The output of the function would then be captured and sent to the user.
In more advanced scenarios, some style scripts might also be run to help translate the output between formats such as JSON to XML.