title: Creating a parsing dictionary to use with sys.argv
body: <p>I'm trying to make a very basic script that utilises TkInter to draw lines based on user input. The user will execute the script through a command line, using parameters to determine the lines drawn.</p>
  
  <p>The possible commands are:</p>
  
  <pre><code>(# signifies any whole number the user chooses)
  
  P #   (selects a pen to draw with)
  D     (pen down / starts drawing)
  N #   (moves the pen north # increments)
  E #   (moves the pen east # increments)
  S #   (moves the pen south # increments)
  W #   (moves the pen west # increments)
  U     (pen up / stops drawing)
  </code></pre>
  
  <p>Example:</p>
  
  <pre><code>Parser.py P 3 D N 4 E 2 S 4 W 2 U
  </code></pre>
  
  <p>The above example will draw a tall rectangle.</p>
  
  <p>I'm trying to create a basic parser for sys.argv that will run through all the arguments and execute methods from a dictionary based on said arguments and the order thereof.</p>
  
  <p>Here is my code that runs through sys.argv and creates an array based on the arguments (I will be adding error-checking, so just assume a happy-day scenario for now):</p>
  
  <pre><code>class JoshSourceReader(object):
      """ responsibe for providing source text for parsing and drawing
          Initiates the Draw use-case.
          Links to a parser and passes the source text onwards """
      def __init__(self):
          self.parser = JoshParser()
          self.source = []
      def go(self):
          i = 1
          x = len(sys.argv)
          while i &lt; x:
              if sys.argv[i]=="P":
                  self.source.append("P " + str(sys.argv[i + 1]))
                  i += 2
              elif sys.argv[i]=="D":
                  self.source.append("D")
                  i += 1
              elif sys.argv[i]=="N":
                  self.source.append("N " + str(sys.argv[i + 1]))
                  i += 2
              elif sys.argv[i]=="E":
                  self.source.append("E " + str(sys.argv[i + 1]))
                  i += 2
              elif sys.argv[i]=="S":
                  self.source.append("S " + str(sys.argv[i + 1]))
                  i += 2
              elif sys.argv[i]=="W":
                  self.source.append("W " + str(sys.argv[i + 1]))
                  i += 2
              elif sys.argv[i]=="U":
                  self.source.append("U")
                  i += 1
          self.parser.parse(self.source)
  </code></pre>
  
  <p>So the array that would be generated from my example above would look like this:</p>
  
  <pre><code>source=['P 3', 'D', 'N 4', 'E 2', 'S 4', 'W 2', 'U']
  </code></pre>
  
  <p><strong>So my question is this:</strong> How can I create an efficient dictionary of methods that works with the above array and executes methods based on the array elements, one by one? I'm even starting to doubt if it's possible due to the fact that some methods will need properties passed (the number), and some won't. Anyone able to shed some light on this matter?</p>
  
  <p>And please keep in mind that I'm <em>very</em> new to Python.</p>
  
  <p><strong>EDIT:</strong> I forgot one crucial piece of information. N, E, S, W all refer to the same method, but it takes a different argument each time. Here's the method (doesn't draw yet):</p>
  
  <pre><code>def drawLine(self, direction, distance):
      print("drawing line of length " + str(distance) + " at "
            + str(direction))
  </code></pre>
  
  <p>So if I were calling this method using "S 3", I would call it like so:</p>
  
  <pre><code>drawer.drawLine(180, 3)
  </code></pre>
  
  <p>And if I were to call it using "W 1", I would call it like so:</p>
  
  <pre><code>drawer.drawLine(270, 1)
  </code></pre>