当前位置: 动力学知识库 > 问答 > 编程问答 >

--debug command line argument in Python

问题描述:

When I introduce to the command line "--debug" argument I need to set variable "debug", from my python script, to the value 1.

I've tried something, but I have to write "--debug=1" to the command line to set variable.

parser = argparse.ArgumentParser()

parser.add_argument("--debug", default=2)

When I run the command:

python script.py --rev1=1.2 --rev2=1.5 --debug

my variable "debug" should have value 1.

网友答案:

If you're interested to know whether a certain command line flag has been passed to your script, you'd set the action argument of Argument.add_argument to store_true.

parser.add_argument('--debug', action='store_true')

Then parser.parse_args().debug will have the value of True if --debug was present and False otherwise.

$ python script.py
parser.parse_args() returned Namespace(debug=False)
$ python script.py --debug
parser.parse_args() returned Namespace(debug=True)
分享给朋友:
您可能感兴趣的文章:
随机阅读: