Created
February 26, 2013 02:28
-
-
Save riywo/5035308 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from optparse import OptionParser | |
class Hoge(object): | |
def __init__(self): | |
parser = OptionParser() | |
parser.add_option("-f", "--foo", action="store_true", dest="foo", default=False, | |
help="Foo") | |
(self.options, self.args) = parser.parse_args() | |
def run(self): | |
print self.options | |
print self.args | |
if __name__ == "__main__": | |
hoge = Hoge() | |
hoge.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
簡単なスクリプトなら、いちいちクラスを作るのはちょっとやり過ぎな感じ。
dest は指定しなくても、勝手に long なオプション名があればそれを、無ければ short なオプション名を使ってくれます。
default を指定しない場合、勝手に
None
が入るので、if opts.foo:
と分岐を書けばデフォルトで偽になります。アンパック代入の左辺は括弧不要です。