There's actually an example in the SWIG documentation for exactly this sort of function with Python. I've quoted it here with a minor change:
%typemap(in) (int argc, char *argv[]) {
int i;
if (!PyList_Check($input)) {
PyErr_SetString(PyExc_ValueError, "Expecting a list");
return NULL;
}
$1 = PyList_Size($input);
$2 = (char **) malloc(($1+1)*sizeof(char *));
for (i = 0; i < $1; i++) {
PyObject *s = PyList_GetItem($input,i);
if (!PyString_Check(s)) {
free($2);
PyErr_SetString(PyExc_ValueError, "List items must be strings");
return NULL;
}
$2[i] = PyString_AsString(s);
}
$2[i] = 0;
}
%typemap(freearg) (int argc, char *argv[]) {
free($2); // If here is uneeded, free(NULL) is legal
}
This allows you in Python to do simply:
import test
test.foo(["a", "b", "c"])
Where test is the name of the module you gave SWIG and foo is a function that matches the signature int argc, char *argv[]. Simple and intuitive to use for a Python programmer and it encapsulates and reuses the complex bit.
What the documentation doesn't seem to mention though is that there's an interface file that does all this for you already:
%module test
%include <argcargv.i>
%apply (int ARGC, char **ARGV) { (int argc, char *argv[]) }
void foo(int argc, char *argv[]);
is sufficient.