Commit cb6a95b7 authored by Samuel Tan's avatar Samuel Tan
Browse files

replaced getopt with argparse

parent d5eb5240
......@@ -4,7 +4,7 @@
from chemF import *
from inp_formats import *
import re, os
import sys, getopt
import sys, getopt, argparse
import collections as col
taskDict = {}
......@@ -49,7 +49,7 @@ def prettyPrint(contentsList, out_filename = None):
def usage():
usage_str = """
Usage:
qcpp <task_file> -x <xyz_file> -o <out_filename> -t <task> -s <template>
qcpp <task_file> -x <xyz_file> -o <out_filename> -t <template>
See the "templates" directory for examples of templates
"""
warning(usage_str)
......@@ -61,52 +61,34 @@ def main():
# list of (option,value) pairs
# arguments after optional arguments removed
# optional parameters that take an arg must be followed by a ":"
# Wed 17 Feb 2016: getopt made redundant, and argparse used
try:
opts, args = getopt.getopt(sys.argv[1:], "hx:o:t:s:")
except getopt.GetoptError:
usage()
warning("GetoptError: trouble reading in arguments")
sys.exit(3)
optDict = dict(opts)
if "-h" in optDict:
usage()
sys.exit()
# parser
p = argparse.ArgumentParser(description = "Python Preprocessor for Quantum Chemistry")
p.add_argument("taskfile", help = "path to the task file")
p.add_argument("-o", "--out_filename", help = "path/name of the file to write to")
p.add_argument("-x", "--xyz_file", help = "path to xyz file")
p.add_argument("-t", "--template", help = "path to template file")
# check command line for task, xyz, and template
essentials = ["-t", "-x"]
args = p.parse_args()
if all(a in optDict for a in essentials):
pass
# read task file if, not all 3 essentials are specified in cmd line
elif len(args) == 1:
tsk_f = args[0]
try:
readTaskFile(tsk_f)
except FileNotFoundError:
print("Task file cannot be found")
usage()
sys.exit(2)
elif len(args) > 1:
print("""Too many arguments given. Only task file required, other arguments are optional
and must be prefixed with a marker""")
usage()
else:
# if no taskfile specified
try:
readTaskFile(args.taskfile)
except FileNotFoundError:
readTaskFile(scriptPath("taskfiles/intEgamess.gen"))
warning("Task file cannot be found: did you forget to specify it?")
print("task file cannot be found ", args[0])
usage()
#sys.exit(2)
# override task file defaults with optional arguments
if "-x" in optDict:
taskDict["using"] = optDict["-x"]
elif "-o" in optDict:
taskDict["out_filename"] = optDict["-o"]
elif "-t" in optDict:
taskDict["task"] = optDict["-t"]
elif "-s" in optDict:
taskDict["template"] = optDict["-s"]
if args.xyz_file:
taskDict["using"] = args.xyz_file
if args.out_filename:
taskDict["out_filename"] = args.out_filename
if args.template:
taskDict["template"] = args.template
essentials = ["task", "using"]
# check that taskDict contains all the information we need
......@@ -138,6 +120,10 @@ def main():
if taskDict["task"] == "psi4sapt":
a = genPsi4(curr_sys = curr_sys, tsk_d = taskDict)
a = prettyPrint(a, taskDict.get("out_filename"))
try:
prettyPrint(a, taskDict["out_filename"])
except KeyError:
prettyPrint(a)
return 0
......@@ -149,7 +135,10 @@ def main():
print("Please remove line from task file and use a template file instead")
return 0
a = optGamess(curr_sys = curr_sys, tsk_d = taskDict)
a = prettyPrint(a, out_filename = taskDict["out_filename"])
try:
prettyPrint(a, out_filename = taskDict["out_filename"])
except KeyError:
prettyPrint(a)
return 0
......@@ -183,18 +172,19 @@ def main():
#print(a)
# these print nicely now
# ironing out the last bits
# of the cluster input file
# ironing out the last bits of the cluster input file
# can use enumerate to get sequential cation/anion input files
for cation in a["cations"]:
print("Cation ", cation)
prettyPrint(a[cation])
print('end cation')
prettyPrint(a[cation], "cation_" + str(cation) + ".ok")
for anion in a["anions"]:
print("Anion ", anion)
prettyPrint(a[anion])
prettyPrint(a[anion], "anion_" + str(anion) + ".ok")
# print cluster
prettyPrint(a["cluster"])
try:
prettyPrint(a["cluster"], taskDict["out_filename"])
except KeyError:
prettyPrint(a["cluster"])
return 0
......@@ -212,7 +202,10 @@ def main():
# note using same function as optimise, since
# basically just replacing xyz_data
a = optGamess(curr_sys = curr_sys, tsk_d = taskDict)
a = prettyPrint(a, out_filename = taskDict["out_filename"])
try:
prettyPrint(a, out_filename = taskDict["out_filename"])
except KeyError:
prettyPrint(a)
return 0
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment