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

问题描述 投票:0回答:1
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:退出:使用'exit','quit'或按Ctrl-d。警告(“退出:使用'退出','退出'或Ctrl-D。”,stacklevel = 1)

windows jupyter-notebook argparse
1个回答
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")
© www.soinside.com 2019 - 2024. All rights reserved.