Python Argparse 'following arguments are required' error

元气小坏坏 提交于 2019-12-08 13:56:04

问题


I'm trying to use a github repo and having some issues with the argparse part of the project. The repo starts like this:

import markdown, sys, csv, getpass, smtplib, argparse
from email.mime.text import MIMEText
from jinja2 import Template


parser = argparse.ArgumentParser()
parser.add_argument('-m', '--markdown', help='Path to Markdown Template', required=True)
parser.add_argument('-c', '--csv', help='Path to CSV file', required=True)
parser.add_argument('-v', '--verbose', help='Write out emails')
args = parser.parse_args() 

I've entered both the relative and absolute path to both the markdown and CSV files... and I get in return this error:

usage: mass-email.py [-h] -m MARKDOWN -c CSV
mass-email.py: error: the following arguments are required: -m/--markdown, -c/--csv

Process finished with exit code 2

I can't find any specific help with this. Any advice would be much appreciated.


回答1:


Call your script with mass-email.py -m /path/to/my/file -c /path/to/csv

Your arguments are defined by their flags. If you want your arguments to be positional, don't include the dashes before them.

parser.add_argument('markdown', help='Path to Markdown Template', required=True)
parser.add_argument('csv', help='Path to CSV file', required=True)



回答2:


Seems like you may be running it from the IDE without giving any command line arguments. I see you are using pycharm, so check this post about passing in command line arguments to pycharm runs:

Pycharm and sys.argv arguments

Hope this helps!



来源:https://stackoverflow.com/questions/51678520/python-argparse-following-arguments-are-required-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!