pythonのパラメータ(プログラム引数)取得 - OptionParser
- Tue
- 00:31
- python
pythonのパラメータ(プログラム引数)取得にOptionParserを利用。
Verは2.4.3で確認。
パラメータの形式はショート/ロング同時に指定できて便利。
ソースコード:test01_child.py
実行例:
Verは2.4.3で確認。
パラメータの形式はショート/ロング同時に指定できて便利。
ソースコード:test01_child.py
#!/usr/local/bin/python import sys, os, array import time import ConfigParser from optparse import OptionParser prm_proc = '' prm_date = 0 def main (argv, environ): print " start test01_child.py main: %s %s" % (prm_proc, prm_date) time.sleep (5) if __name__ == "__main__": print " *** test01_child: start." parser = OptionParser() parser.add_option("-p", "--proc", type="string", dest="prm_proc") parser.add_option("-d", "--date", type="int", dest="prm_date") (pgm_opts, args) = parser.parse_args() prm_proc = pgm_opts.prm_proc prm_date = pgm_opts.prm_date print " test01_child: prm_proc=[%s] prm_date=[%s]" % (prm_proc, prm_date) #common_conf = test_mod01.getGenConfig('') #print " test01_child: common_conf=[%s]" % common_conf #print " ini_dir=[%s]" % common_conf['ini_dir'] main (sys.argv, os.environ) print " *** test01_child: finished."
実行例:
$ python -V Python 2.4.3 $ ./test01_child.py -p proc -d 20101018 *** test01_child: start. test01_child: prm_proc=[proc] prm_date=[20101018] start test01_child.py main: proc 20101018 *** test01_child: finished. $ ./test01_child.py --proc test01_child --date 20101018 *** test01_child: start. test01_child: prm_proc=[test01_child] prm_date=[20101018] start test01_child.py main: test01_child 20101018 *** test01_child: finished.
- 関連記事
-
- Emacs 27.1のelpy設定
- pythonでsjisファイルutf8に変換しようとして
- emacsでpython、事始
- pythonのConfigParserによる設定ファイルの入力
- pythonのパラメータ(プログラム引数)取得 - OptionParser
Comment
Trackback
- URL
- https://nosource.blog.fc2.com/tb.php/17-e5533243
- この記事にトラックバック(FC2Blog User)