简体   繁体   中英

Trying using Help and Verbose while parsing command line arguments

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")

I'm getting this error

usage: ipykernel_launcher.py [-h] [--verbose] ipykernel_launcher.py: error: unrecognized arguments: -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: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

In a shell window (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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM