Last active
August 29, 2015 14:11
-
-
Save kkAyataka/8f737405397cad0083aa to your computer and use it in GitHub Desktop.
Python OptionParser for command line arguments
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 | |
# -*- coding: utf-8 -*- | |
'''optparseを用いているが、これはPython 2.7でdeprecated | |
optparseではなく、argparseを使う | |
''' | |
from optparse import OptionParser | |
# optionの定義 | |
p = OptionParser() | |
p.add_option('-f', '--flg', dest='flg', action='store_true', help='boolean flag') | |
p.add_option('-v', '--val', dest='val', type='int', help='int value') | |
p.add_option('-s', '--str', dest='str', type='string', help='string value') | |
# 引数の処理 | |
(opts, args) = p.parse_args() | |
print 'opts: {}, args: {}'.format(opts, args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment