forked from vhsvhs/asr-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
argParser.py
111 lines (98 loc) · 2.78 KB
/
argParser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# ArgParser.py
#
# by Victor Hanson-Smith
# victorhansonsmith@gmail.com
#
# This is a super useful class to include in your Python project that
# needs many command-line options passed to the program.
#
# Usage in your code:
#
# 1. Instantiate an instance of this class:
# ap = ArgParser(sys.argv)
#
# 2. Get the values of required arguments. For example:
# x = ap.getArg("--inputfile")
# --> where "--inputfile" can be really anything.
#
# 3. Get the values of optional arguments:
#
#
import sys
import os
import re
from debugTools import *
class ArgParser:
def __init__(self, cliArgs):
self.args = cliArgs
self.params = {}
# use this method to grab REQUIRED command-line parameters:
def getArg(self, flag):
if self.args.__contains__(flag):
i = self.args.index(flag)
return self.args[i+1]
else:
printError( argDoesNotExist )
printTip("argument: " + flag)
exit(1)
def getOptionalArg(self, flag):
if self.args.__contains__(flag):
i = self.args.index(flag)
return self.args[i+1]
else:
return None
# This method will return a list of tokens following 'flag', but not including
# tokens which start with "--"
def getList(self, flag):
if self.args.__contains__(flag):
i = self.args.index(flag)
returnList = []
flagPattern = re.compile("^\-\-.*")
for j in range( i+1, self.args.__len__() ):
if re.match(flagPattern, self.args[j] ):
return returnList
else:
returnList.append( self.args[j] )
return returnList
else:
printError( argDoesNotExist )
printTip("argument: " + flag)
exit(1)
# use this method to grab OPTIONAL command-line toggles (boolean on/off switches)
def getOptionalToggle(self, flag):
if self.args.__contains__(flag):
return True
else:
return False
def doesContainArg(self, flag):
return self.args.__contains__(flag)
def getOptionalList(self, flag, type=str):
#print self.args, flag
if self.args.__contains__(flag):
i = self.args.index(flag)
returnList = []
flagPattern = re.compile("^\-\-.*")
for j in range( i+1, self.args.__len__() ):
if re.match(flagPattern, self.args[j] ):
return returnList
else:
returnList.append( type(self.args[j]) )
return returnList
else:
return None
def which(program):
import os
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None