LoginSignup
1
2

More than 5 years have passed since last update.

Python > 実行時引数 > -dがついているかどうかを確認する

Last updated at Posted at 2017-03-24
動作環境
Xeon E5-2620 v4 (8コア) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 とその-devel
mpich.x86_64 3.1-5.el6とその-devel
gcc version 4.4.7 (とgfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.1を使用。
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv

Python スクリプトを実行時、-dがついているかどうかのチェックをする。

参考: https://docs.python.jp/3/howto/argparse.html
store_true記載の部分。

test_python_170324a.py
import argparse

parser = argparse.ArgumentParser(description="do something")

parser.add_argument(
    '-d',
    '--debugMode',
    dest='debugMode',
    action='store_true',
    help='debug mode')

cmd_args = parser.parse_args()

if(cmd_args.debugMode == False):
    print("normal mode")
else:
    print("debug mode")
結果
$ python test_python_170324a.py 
normal mode
$ python test_python_170324a.py -d
debug mode
$ python test_python_170324a.py --debugMode
debug mode
1
2
0

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
1
2