title: python argparser: how do i associate specific arguments with specific functions
body: <p>I'm just new to python and new the argparser library.  I'm trying to figure out how to write a script file that has many different functions, each one requiring different arguments.</p>
  
  <p>So for example, I might have the following method signatures:</p>
  
  <pre><code> def function1(a,b,c):
   def function2(x,y,z):
   def function3():
  </code></pre>
  
  <p><strong>What I have so far</strong></p>
  
  <p>So far, I've added all possible arguments as optional items like so: </p>
  
  <pre><code>import argparse
  
   def function1(lc,uc):
       assert(lc is not None)
       assert(uc is not None)
       #do something
  
   def function2():
       #do something else
  
   def function3(lr,ur):
       assert(lr is not None)
       assert(ur is not None)
       #do something else
  
  parser = argparse.ArgumentParser(description='This is the description of my program')
  parser.add_argument('-lc','--lower_create', type=int, help='lower range value for creating mailboxes')
  parser.add_argument('-uc','--upper_create', type=int, help='upper range value for creating mailboxes')
  parser.add_argument('-lr','--lower_reserve', type=int, help='lower range value for reserving mailboxes')
  parser.add_argument('-ur','--upper_reserve', type=int, help='upper range value for reserving mailboxes')
  
  args = parser.parse_args()
  </code></pre>
  
  <p>So far so good.  When I run the script like so:</p>
  
  <pre><code> python3 myutils.py
  </code></pre>
  
  <p>I dont' get any errors. </p>
  
  <p>When I do this: </p>
  
  <pre><code> python3 myutils.py -h
  </code></pre>
  
  <p>it gives me a nice help system. </p>
  
  <p>But what isn't clear to me is how to associate specific args with specific functions.  This is the error I get when I invoke function1 for example: </p>
  
  <pre><code>lab-1:/var/www/localhost/htdocs/pvmaildbapp# python3 myutils.py function1
  usage: myutils.py [-h] [-lc LOWER_CREATE] [-uc UPPER_CREATE]
                         [-lr LOWER_RESERVE] [-ur UPPER_RESERVE]
  myutils.py: error: unrecognized arguments: function1
  </code></pre>
  
  <p>I'm still making my way for the first time through the argparse docs so I'm sure I missed something.  But any tips / pointers in the right direction would be appreciated</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>Based on answers below from d_kennetz and UtahJarhead (thank you btw) , I have changed the code to include a "dest" parm for each argument and I also need an arg for the function the user is trying to call, like so: </p>
  
  <pre><code>parser = argparse.ArgumentParser(description='This is the description of my program')
  
  parser.add_argument('func_name',help='name of function you want to run')
  
  parser.add_argument('-lc','--lower_create', type=int, dest='lc', help='lower range value for creating mailboxes')
  parser.add_argument('-uc','--upper_create', type=int, dest='uc', help='upper range value for creating mailboxes')
  parser.add_argument('-lr','--lower_reserve', type=int, dest='lr', help='lower range value for reserving mailboxes')
  parser.add_argument('-ur','--upper_reserve', type=int, dest='ur', help='upper range value for reserving mailboxes')
  
  args = parser.parse_args()
  </code></pre>
  
  <p>and then, I've also changed the definition of function1 to look like this: </p>
  
  <pre><code> def function1(lc,uc):
       assert(lc is not None)
       assert(uc is not None)
       print('made it here')
       return True
  </code></pre>
  
  <p>lastly, i now try to call via commandline like this: </p>
  
  <pre><code> python3 myutils.py function1 -lc 1 -uc 3
  </code></pre>
  
  <p>but it doesn't call function1 at all.
  I'm clearly missing one more piece of the puzzle.</p>
  
  <p>i think what I need at the end of my code - after I call parse_args() is something like this:  (pseudocode)</p>
  
  <pre><code>select case args.func_name
     case 'function1'
         make sure i have lc and uc
     case function3
         make sure i have lr and ur
  end select
  </code></pre>
  
  <p>Is this correct?</p>
  
  <p><strong>Edit 2</strong></p>
  
  <p>ok so i think i've connected all the dots. </p>
  
  <ol>
  <li>I was definitely missing a positional argument to save the function
  name that the user wants to call. </li>
  <li>I'm still using optional argument
  notation to save the values for lc, uc etc. </li>
  <li>The piece i was missing was logic to manually match up the functions with args.  For some reason, I thought I'd get some of this plumbing for free with the argparse library.  </li>
  </ol>
  
  <p>So now I have code that looks like this:</p>
  
  <pre><code>args = parser.parse_args()
  #print(args)
  if args.func_name == 'function1':
      #make sure we have lc and uc
      assert(args.lc is not None)
      assert(args.uc is not None)
      function1(args.lc, args.uc)
  elif args.func_name == 'function3':
      assert(args.lr is not None)
      assert(args.ur is not None)
      function3(args.lr, args.ur)
  elif args.func_name == 'function2':
      function2()
  </code></pre>
  
  <p>The code works.  But i feel like there's probably a more graceful way to handle the last series of if/elifs that I have ... As this utility file grows and I add more methods to it, the if elif list will grow pretty big.</p>