title: how to pass argparse arguments to a class
body: <p>I'm very new to coding in general and Python in particular. I'm trying to learn how to pass argparse arguments I have created into a class for use the right/recommend way. In addition to learning python, I'm trying to learn how to do things in an OOP manner so that learning other, OOP-type languages comes a bit easier.</p>
  
  <p>So here's a sample of what I am trying to do:</p>
  
  <pre><code>import argparse
  
  class passyourcliargstome():
      def __init__(self, whatdoiputheretogetmycliargs):
          #how do I get my cli args here?
          pass
      def otherfunctionsthatdothings():
          pass
  
  if __name__ == '__main__':
      #grab the arguments when the script is ran
      parser = argparse.ArgumentParser(
           description='Make things happen.')
      parser.add_argument('-f', '--foo', action='store_true', default=False, help='here be dragons')
      parser.add_argument('-b', '--bar', action='store_true', default=False, help='here be more dragons')
  
      passyourcliargstome.otherfunctionsthatdothings()
  </code></pre>
  
  <p>So, I'm defining argparse arguments outside of the main class, and want to know how to get them <strong>inside</strong> the class. Is this even the right way to do it? should I just make argparse a function under my class? </p>
  
  <p>Thank you in advance for any assistance, references, etc.</p>
  
  <p>Edit: 11/16 2:18 EST</p>
  
  <p>Note: Since I don't have enough rep to answer my own question, this is my only recourse for posting a proper answer.</p>
  
  <p>Okay, it took me some doing, but I managed to piece this together. RyPeck's answers helped me in getting my arguments (something my code was missing), but then afterwards I was getting unbound method errors When I was trying to test the code. I had no idea what that meant. Did I mention that I live up to my screen name?</p>
  
  <p>It didn't really click until I found and read <a href="https://stackoverflow.com/questions/6012799/python-unbound-method">this</a>. Here is my working code. If anyone has anything to add to this, up to and including "You're still doing it wrong, do it this way, the right way." I'm all ears. In the meantime, thanks for your help.</p>
  
  <pre><code>import argparse
  
  class Passyourcliargstome(object):
  
      def __init__(self):
          #here's how I got my args here
          self.foo = args.foo
          self.bar = args.bar
      def otherfunctionsthatdothings(self):
          print "args inside of the main class:"
          print self.foo
          print self.bar
  
  if __name__ == '__main__':
      #grab the arguments when the script is ran
      parser = argparse.ArgumentParser(description='Make things happen.')
      parser.add_argument('-f', '--foo', action='store_true', default=False, help='here be dragons')
      parser.add_argument('-b', '--bar', action='store_true', default=False, help='here be more dragons')
  
      args = parser.parse_args()
      print "args outside of main:"
      print args.foo
      print args.bar
  
      #this was the part that I wasn't doing, creating an instance of my class.
      shell = Passyourcliargstome()
      shell.otherfunctionsthatdothings()
  </code></pre>
  
  <p>Running this code with no arguments prints False four times. two times outside of the class instance, two times within the class instance.</p>