refactor: passage argparse
This commit is contained in:
parent
1064e517be
commit
73b87797c1
75
mailing.py
75
mailing.py
@ -22,27 +22,44 @@
|
||||
# This program is used to mail a message to several recipients, thus
|
||||
# providing customization for each recipient.
|
||||
|
||||
import sys, getopt, re
|
||||
import argparse, sys, re
|
||||
import smtplib, time, mimetypes
|
||||
from email.header import Header
|
||||
from email.message import EmailMessage
|
||||
|
||||
#
|
||||
# displays usage of the program, then quit
|
||||
#
|
||||
def usage(returncode):
|
||||
print(
|
||||
"""SYNTAX:
|
||||
""",
|
||||
sys.argv[0],
|
||||
""" -t tofile -b bodyfile [-p attachedfile]
|
||||
|
||||
tofile : sort of CSV file containing addresses of recipients
|
||||
bodyfile : template of the mail to be sent
|
||||
attachedfile : optionnal attachment
|
||||
""",
|
||||
)
|
||||
sys.exit(returncode)
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description="""
|
||||
Simple mailing script.
|
||||
"""
|
||||
)
|
||||
|
||||
ma = parser.add_argument_group(title="mandatory arguments", description=None)
|
||||
ma.add_argument(
|
||||
"-t",
|
||||
"--tofile",
|
||||
metavar="TO.FILE",
|
||||
type=str,
|
||||
required=True,
|
||||
help="Sort of CSV file containing addresses of recipients.",
|
||||
)
|
||||
ma.add_argument(
|
||||
"-b",
|
||||
"--bodyfile",
|
||||
metavar="BODY.FILE",
|
||||
type=str,
|
||||
required=True,
|
||||
help="Template of the mail to be sent",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"-a",
|
||||
"--attachement",
|
||||
metavar="ATTACHED.FILE",
|
||||
type=str,
|
||||
nargs="?",
|
||||
help="Optionnal attachment.",
|
||||
)
|
||||
|
||||
|
||||
TOFILE = None
|
||||
@ -180,26 +197,10 @@ def send_message(message, to, attachment):
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
# handle options of the program
|
||||
try:
|
||||
opts, args = getopt.getopt(sys.argv[1:], "t:b:p:")
|
||||
except getopt.error as msg:
|
||||
print("getopt error: %s" % msg)
|
||||
usage(1)
|
||||
|
||||
for name, value in opts:
|
||||
if name == "-t":
|
||||
TOFILE = value
|
||||
elif name == "-b":
|
||||
BODYFILE = value
|
||||
elif name == "-p":
|
||||
JOINFILE = value
|
||||
else:
|
||||
print("argument: ", name, " invalid")
|
||||
usage(1)
|
||||
|
||||
if not TOFILE or not BODYFILE:
|
||||
usage(1)
|
||||
args = parser.parse_args()
|
||||
TOFILE = args.tofile
|
||||
BODYFILE = args.bodyfile
|
||||
JOINFILE = args.attachement
|
||||
|
||||
# read the recipients file
|
||||
sets = read_recipients()
|
||||
|
Loading…
Reference in New Issue
Block a user