title: Using argparse with function that takes **kwargs argument
body: <p>I'm using <code>argparse</code> to take input and pass it to a function that takes as arguments two variables and <code>**kwargs</code>.</p>
  
  <p>Here's my function: </p>
  
  <pre><code>import requests
  import sys
  import argparse
  
  
  def location_by_coordinate(LAT, LNG, **kwargs):
      if not kwargs:
          coordinate_url = "https://api.instagram.com/v1/locations/search?lat=%s&amp;lng=%s&amp;access_token=%s" % (LAT, LNG, current_token)
          r = requests.get(coordinate_url).text
      else:
          coordinate_url = "https://api.instagram.com/v1/locations/search?lat=%s&amp;lng=%s&amp;access_token=%s" % (LAT, LNG, current_token)
          for key, value in kwargs.iteritems():
              if 'DISTANCE' in kwargs:
                  distance = kwargs.get('DISTANCE')
                  if distance &gt; 5000:
                      print distance
                      print "max distance is 5000m, value is reassigned to default of 1000m"
                      distance = 1000
                      coordinate_url = "https://api.instagram.com/v1/locations/search?lat=%s&amp;lng=%s&amp;access_token=%s" % (LAT, LNG, current_token)
                      r = requests.get(coordinate_url).text
                  else:
                      pass
                      coordinate_url = "https://api.instagram.com/v1/locations/search?lat=%s&amp;lng=%s&amp;access_token=%s" % (LAT, LNG, current_token)
                      r = requests.get(coordinate_url).text
              if 'FACEBOOK_PLACES_ID' in kwargs:
                  fb_places_id = kwargs.get('FACEBOOK_PLACES_ID')
                  payload = {'FACEBOOK_PLACES_ID': '%s' % (fb_places_id), 'DISTANCE': '%s' % (DISTANCE)}
                  r = requests.get(coordinate_url, params=payload).text
              if 'FOURSQUARE_ID' in kwargs:
                  foursquare_id = kwargs.get('FOURSQUARE_ID')
                  payload = {'FOURSQUARE_ID': '%s' % (foursquare_id), 'DISTANCE': '%s' % (DISTANCE)}
                  r = requests.get(coordinate_url, params=payload).text
              if 'FOURSQUARE_V2_ID' in kwargs:
                  foursquare_v2_id = kwargs.get('FOURSQUARE_V2_ID')
                  payload = {'FOURSQUARE_V2_ID': '%s' % (foursquare_v2_id), 'DISTANCE': '%s' % (DISTANCE)}
                  r = requests.get(coordinate_url, params=payload).text
      #print r
      return r
  </code></pre>
  
  <p>Given this function and its use of **kwargs, how should I setup the subparsers? </p>
  
  <p>Here's how I've setup the command line parser thus far:</p>
  
  <pre><code> def main():
          parser = argparse.ArgumentParser(description="API Endpoints tester")
          subparsers = parser.add_subparsers(dest="command", help="Available commands")
  
          location_by_parser = subparsers.add_parser("location_by_coordinate", help="location function")
          location_by_parser.add_argument("LAT", help="latitude")
          location_by_parser.add_argument("LNG", help="longitude")
  
          arguments = parser.parse_args(sys.argv[1:])
          arguments = vars(arguments)
          command = arguments.pop("command")
          if command == "location_by_coordinate":
              LAT, LNG = location_by_coordinate(**arguments)
          else:
              print "No command provided..."
  
      if __name__ == "__main__":
          main()
  </code></pre>
  
  <p>Obviously, the above main() function works fine with the location_by_coordinate() function when I call it at the command line like this: </p>
  
  <pre><code>$ python argstest.py location_by_coordinate 40.5949799 -73.9495148
  </code></pre>
  
  <p>But with the code the way it is currently, if I try: </p>
  
  <pre><code>$ python argstest.py location_by_coordinate 40.5949799 -73.9495148 DISTANCE=3000
  </code></pre>
  
  <p>Obviously, I get: </p>
  
  <pre><code>argstest.py: error: unrecognized arguments: DISTANCE=3000
  </code></pre>
  
  <p>But I'm not sure how to setup a subparser for **kwargs. If I try to setup a subparser like this: </p>
  
  <pre><code>location_by_parser.add_argument("**kwargs", help="**kwargs")
  </code></pre>
  
  <p>and then try that command again: </p>
  
  <pre><code>$ python argstest.py location_by_coordinate 40.5949799 -73.9495148 DISTANCE=3000
  </code></pre>
  
  <p>That doesn't work because the <code>arguments</code> object (which is a dictionary), becomes this:</p>
  
  <p><code>{'LAT': '40.5949799', 'LNG': '-73.9495148', 'command': 'location_by_coordinate', '**kwargs': 'DISTANCE=3000'</code>}</p>
  
  <p>And this Traceback is returned: </p>
  
  <pre><code>Traceback (most recent call last):
    File "argstest.py", line 118, in &lt;module&gt;
      main()
    File "argstest.py", line 108, in main
      foo = location_by_coordinate(**arguments)
    File "argstest.py", line 40, in location_by_coordinate
      return r
  UnboundLocalError: local variable 'r' referenced before assignment
  </code></pre>
  
  <p>How can I enable argparse to handle/to parse what is entered at the command line that is intended to be passed to the function via **kwargs? </p>