解析命令行参数时尝试使用Help和Verbose

时间:2018-09-08 12:35:17

标签: windows jupyter-notebook argparse

import argparse

parser = argparse.ArgumentParser(description='Demo')
parser.add_argument('--verbose',
    action='store_true',
    help='verbose flag' )

args = parser.parse_args()

if args.verbose:
   print("~ Verbose!")
else:
   print("~ Not so verbose")

我收到此错误

  

用法:ipykernel_launcher.py [-h] [--verbose]   ipykernel_launcher.py:错误:无法识别的参数:-f C:\ Users \ Sourav \ AppData \ Roaming \ jupyter \ runtime \ kernel-c07f728a-968e-40af-b09e-e7d9b67b47fe.json

     

SystemExit:2

     

C:\ Users \ Sourav \ AppData \ Local \ conda \ conda \ envs \ tensorflow \ lib \ site-packages \ IPython \ core \ interactiveshell.py:2855:UserWarning:要退出:请使用“退出”,“退出” '或Ctrl-D。     警告(“退出:使用'退出','退出'或Ctrl-D。”,堆栈级别= 1)

1 个答案:

答案 0 :(得分:0)

在shell窗口(Linux bash)中:

1317:~/mypy$ python stack52235399.py 
~ Not so verbose
0940:~/mypy$ python stack52235399.py --verbose
~ Verbose!
0940:~/mypy$ cat stack52235399.py 
import argparse

parser = argparse.ArgumentParser(description='Demo')
parser.add_argument('--verbose',
    action='store_true',
    help='verbose flag' )

args = parser.parse_args()

if args.verbose:
   print("~ Verbose!")
else:
   print("~ Not so verbose")