title: Wrap a function that takes a struct of optional arguments using kwargs
body: <p>In C it's not uncommon to see a function that takes a lot of inputs, many/most of which are optional group these up in a struct to make the interface cleaner for developers. (Even though you should be able to rely on a compiler accepting <a href="https://stackoverflow.com/questions/9034787/function-parameters-max-number">at least 127 arguments to a function</a> nobody actually wants to write that many, especially as C has no overloading or default function argument support). As a hypothetical example we could consider the following struct/function pair (test.h) to illustrate the problem:</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdbool.h&gt;
  
  typedef struct {
    const char *name;
    void *stuff;
    int max_size;
    char flags;
    _Bool swizzle;
    double frobination;
    //...
  } ComplexArgs;
  
  void ComplexFun(const ComplexArgs *arg) {}
  </code></pre>
  
  <p>When it comes to wrapping this using SWIG we can get something working quickly using:</p>
  
  <pre class="lang-c prettyprint-override"><code>%module test
  
  %{
  #include "test.h"
  %}
  
  typedef bool _Bool;
  
  %include "test.h"
  </code></pre>
  
  <p>That works and we can use it as follows:</p>
  
  <pre class="lang-py prettyprint-override"><code>import test
  
  args=test.ComplexArgs()
  args.flags=100;
  args.swizzle=True
  test.ComplexFun(args)
  </code></pre>
  
  <p>But that isn't exactly Pythonic. A Python developer would be more accustomed to seeing kwargs used to support this kind of calling:</p>
  
  <pre><code>import test
  
  # Not legal in the interface currently:
  test.ComplexFun(flags=100, swizzle=True)
  </code></pre>
  
  <p>How can we make that work? The SWIG -keyword command line option doesn't help either because there's only one actual argument to the function. </p>