Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Parsing options using <code>optparse</code> has always been rather opaque to me. Using <a href="http://docs.python.org/library/argparse.html#module-argparse" rel="nofollow noreferrer"><code>argparse</code></a> helps a little.</p> <p>The insight that I think will help is that the <code>optparse</code> module doesn't actually help you perform the actions specified on the command line. Rather, it helps you collect information from the command line arguments, which you can act on later.</p> <p>In this case, the information you're collecting is stored in the tuple <code>(options, args)</code> in your line:</p> <pre><code>(options, args) = parser.parse_args() </code></pre> <p>To actually act on this information, you're going to have to do your own checking in your code. I like to put things like this in a block at the end of my program, <a href="https://stackoverflow.com/questions/419163/what-does-if-name-main-do">which will only run if it is called from the command line</a>.</p> <pre><code>if __name__ == '__main__': if options.list: list_my_repos() </code></pre> <p>To make how this works a little clearer, it helps to realize that you could do the same thing without optparse at all, by using <a href="http://docs.python.org/library/sys.html#sys.argv" rel="nofollow noreferrer"><code>sys.argv</code></a>. </p> <pre><code>import sys if __name__ == '__main__': if sys.argv[1] == '-l': list_my_repos() </code></pre> <p>as you can probably see, however, this would be a very fragile implementation. Being able to handle more complex cases without doing too much of your own programming is what <code>optparse</code>/<code>argparse</code> buys you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload