2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

optparseモジュールの使い方

2
Posted at

pythonスクリプトを書いていてオプションの渡し方をきれいにしてくれるモジュールを見つけた。それがoptparse

使い方は以下の通り。

# まずモジュールのインポート
import optparse

# parserのメインオブジェクトの作成
parser = optparse.OptionParser()

# 各オプションの設定追加
# この場合だと--debug引数を与えると後で取得する
# optionsオブジェクトのdebugプロパティがTrueになる
# helpは--helpを与えたときに各オプションで表示される
# usageメッセージのこと。
parser.add_option("--debug",
	action="store_true",
	dest="debug",
	help="Debug option")

# optionsが各オプションの値を連想配列としてもっている。
# あとはこれを煮るなり焼くなりする
(options, args) = parser.parse_args()

基本的に順番とか気にしなくてすむのでとても便利。

2
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?