Skip to content

Instantly share code, notes, and snippets.

@kkAyataka
Last active August 29, 2015 14:11
Show Gist options
  • Save kkAyataka/8f737405397cad0083aa to your computer and use it in GitHub Desktop.
Save kkAyataka/8f737405397cad0083aa to your computer and use it in GitHub Desktop.
Python OptionParser for command line arguments
#!/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