title: Neatly pass positional arguments as args and optional arguments as kwargs from argparse to a function
body: <p>I would like to write a Python script that takes some necessary positional and some optional command-line arguments via <code>argparse</code>:</p>
  
  <ul>
  <li>Let's call the positional args <code>a</code>,<code>b</code>,<code>c</code>, and the optional arguments <code>x</code>,<code>y</code>,<code>z</code>.</li>
  <li>In my Python script, I would like to pass these args on to a function; specifically, I want <code>a</code>,<code>b</code>,<code>c</code> to be passed as <code>*args</code>, and <code>x</code>,<code>y</code>,<code>z</code> to be passed as <code>**kwargs</code>, the latter retaining their names.</li>
  <li>I would like to do this many times with different functions and different numbers of positional and optional arguments. Is there a neat, flexible, and/or pythonic way to do this?</li>
  </ul>
  
  <p>Here is some example code:</p>
  
  <pre><code>import argparse
  
  def parse():
      parser = argparse.ArgumentParser()
      parser.add_argument('a', help='1st arg')
      parser.add_argument('b', help='2nd arg')
      parser.add_argument('c', help='3rd arg')
      parser.add_argument('-x', '--x', help='1st kwarg')
      parser.add_argument('-y', '--y', help='2nd kwarg')
      parser.add_argument('-z', '--z', help='3rd kwarg')
      return parser.parse_args()
  
  def func(*args, **kwargs):
      a, b, c = args
      print 'a=', a
      print 'b=', b
      print 'c=', c
      for k, v in kwargs.iteritems():
          print '%s=' % k, v
  
  if __name__ == '__main__':
  
      all_args = parse()
      ### need to split all_args into args and kwargs here ###
      func(*args, **kwargs)
  </code></pre>