I have created a Python wrapper around a C++ program using SWIG on Linux. The structure of the main function of the C++ program is like this:
MyProg.cpp
int main (int argc, char *argv[])
{
...
...
...
}
In other words, it takes a number of arguments from the command line (e.g. arg1
, arg2
) and uses those values during the execution:
$ g++ MyProg.cpp -o MyProg.x
$ MyProg.x arg1 arg2 ...
I created the python wrapper using the following SWIG commands:
$ swig -c++ -python MyProg.i
$ g++ -fpic -c MyProg.cpp -I/usr/include/python3.6 -I/usr/include -I/usr/include/x86_64-linux-gnu
$ gcc -shared MyProg_wrap.o MyProg.o -o _MyProg.so -lstdc++
where the file MyProg.i has the following:
MyProg.i
%module MyProg
%{
int main (int argc, char *argv[]);
%}
int main (int argc, char *argv[]);
My question is:
How do I capture the command line argument bit in the Python wrapper? I have tried passing the arguments inside brackets as shown below, but it does not work, as you can see:
$ python3
Python 3.6.9
[GCC 8.3.0] on linux
>>> import MyProg
>>> MyProg.main("arg1 arg2")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: main() missing 1 required positional argument: 'argv'
>>>
Is there an easy way I can reproduce the original C++ program functionality (i.e. passing command line arguments) in the Python wrapper too?