Statistics
| Revision:

root / trunk / extensions / ext3Dgui / buildman / bin / bmcore / ArgumentParser.py @ 26254

History | View | Annotate | Download (2.99 KB)

1
from ApplicationUsage import ApplicationUsage
2
from bmbase.PlugInManager import PlugInManager
3

    
4

    
5
"""
6
        ArgumentParser is a helper in the task of 
7
        reading parameters from command line.
8
"""
9
class ArgumentParser:
10
        def __init__(self,argv):
11
                self._argv = argv
12
                self._argc = len(self._argv)
13
                self._error = False
14
                self._errorMessages = []
15
                self._applicationUsage = ApplicationUsage()
16
                self._applicationUsage.setApplicationName(self.getApplicationName())
17

    
18
        def errors(self):
19
                return self._error
20

    
21
        def writeErrorMessages(self):
22
                for error in self._errorMessages:
23
                        print error + "\n"
24

    
25
        def writeGoalHelpMessages(self,goal):
26
                if goal in self._applicationUsage.getCommandLineGoals().keys():
27
                        print "Goal supported by " + str(PlugInManager().getPlugInInstanceByGoal(goal).__class__) + " plugin:"
28
                        print goal + "\t" + self._applicationUsage.getCommandLineGoalDescription(goal) + "\n"
29
                        for option,value in self._applicationUsage.getCommandLineGoalOptions(goal):
30
                                print "\t" + option + "\t" + value + "\n"
31
                else:
32
                        print "Goal `" + goal +"` not supported, try --help-all option" 
33

    
34
        def writeHelpMessages(self):
35
                print "BuildMan options:"
36
                print "-----------------" + "\n"
37
                for option,value in self._applicationUsage.getCommandLineOptions():
38
                        print option + "\t" + value + "\n"
39
                if len(self._applicationUsage.getCommandLineGoals().keys())>0:
40
                        print "Supported Goals:"
41
                        print "----------------" + "\n"
42
                        for goal in self._applicationUsage.getCommandLineGoals().keys():
43
                                print goal + "\t" + self._applicationUsage.getCommandLineGoalDescription(goal) + "\n"
44
                                for option,value in self._applicationUsage.getCommandLineGoalOptions(goal):
45
                                        print "\t" + option + "\t" + value + "\n"
46

    
47
        def getApplicationUsage(self):
48
                return self._applicationUsage
49

    
50
        def getApplicationName(self):
51
                return self._argv[0]
52

    
53
        def isOption(self, str):
54
                return (len(str) > 1 and str[0]=='-')
55

    
56
        def argc(self):
57
                return self._argc
58

    
59
        def argv(self):
60
                return self._argv
61

    
62
        def find(self,str):
63
                for pos in range(1,self._argc):
64
                        if str == self._argv[pos]:
65
                                return pos
66
                return 0
67

    
68
        def match(self,pos,str):
69
                return (pos<self._argc and str==self._argv[pos])
70

    
71
        def containsOptions(self):
72
                for op in self._argv:
73
                        if self.isOption(op):
74
                                return True
75
                return False
76

    
77
        def remove(self,pos,num):
78
                if num==0: return
79
                removeList = []
80
                for i in range(pos,pos+num):
81
                        removeList.append(self._argv[i])
82
                for r in removeList:
83
                        self._argv.remove(r)
84
                self._argc-=num
85
                
86
                
87
        def read(self,str,values=[]):
88
                pos = self.find(str)
89
                if pos<=0:
90
                        return False
91
                if len(values)==0:
92
                        self.remove(pos,1)
93
                        return True
94
                return self.__read(pos,str,values)
95

    
96
        def __read(self,pos,str,values):
97
                if self.match(pos,str):
98
                        if pos+len(values)<self._argc:
99
                                inc = 1
100
                                for i in range(len(values)):
101
                                        values[i] = self._argv[pos+inc]
102
                                        inc+=1
103
                                self.remove(pos,len(values)+1)
104
                                return True
105
                        self.__reportError("argument to `"+str+"` is missing")
106
                        return False
107
                return False
108

    
109
        def __reportError(self, str):
110
                self._error = True
111
                self._errorMessages.append(str)