summaryrefslogtreecommitdiff
path: root/src/DllPlugInTester/CommandLineParser.cpp
blob: 1ad024588d09a284e0e99c6f84ed8d170c1ddb97 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "CommandLineParser.h"


CommandLineParser::CommandLineParser( int argc, 
                                      const char *argv[] )
    : m_useCompiler( false )
    , m_useXml( false )
    , m_briefProgress( false )
    , m_noProgress( false )
    , m_useText( false )
    , m_useCout( false )
    , m_waitBeforeExit( false )
    , m_currentArgument( 0 )
{
  for ( int index =1; index < argc; ++index )
  {
    std::string argument( argv[index ] );
    m_arguments.push_back( argument );
  }
}


CommandLineParser::~CommandLineParser()
{
}


void 
CommandLineParser::parse()
{
  while ( hasNextArgument() )
  {
    getNextOption();
    if ( isOption( "c", "compiler" ) )
      m_useCompiler = true;
    else if ( isOption( "x", "xml" ) )
    {
      m_useXml = true;
      m_xmlFileName = getNextOptionalParameter();
    }
    else if ( isOption( "s", "xsl" ) )
      m_xsl = getNextParameter();
    else if ( isOption( "e", "encoding" ) )
      m_encoding = getNextParameter();
    else if ( isOption( "b", "brief-progress" ) )
      m_briefProgress = true;
    else if ( isOption( "n", "no-progress" ) )
      m_noProgress = true;
    else if ( isOption( "t", "text" ) )
      m_useText = true;
    else if ( isOption( "o", "cout" ) )
      m_useCout = true;
    else if ( isOption( "w", "wait" ) )
      m_waitBeforeExit = true;
    else if ( !m_option.empty() )
      fail( "Unknown option" );
    else if ( hasNextArgument() )
      readNonOptionCommands();
  }
}


void 
CommandLineParser::readNonOptionCommands()
{
  if ( argumentStartsWith( ":" ) )
    m_testPath = getNextArgument().substr( 1 );
  else
  {
    CommandLinePlugInInfo plugIn;
    int indexParameter = getCurrentArgument().find( '=' );
    if ( indexParameter < 0 )
      plugIn.m_fileName = getCurrentArgument();
    else
    {
      plugIn.m_fileName = getCurrentArgument().substr( 0, indexParameter );
      std::string parameters = getCurrentArgument().substr( indexParameter +1 );
      plugIn.m_parameters = CPPUNIT_NS::PlugInParameters( parameters );
    }
    
    m_plugIns.push_back( plugIn );

    getNextArgument();
  }
}


bool 
CommandLineParser::hasNextArgument() const
{
  return m_currentArgument < m_arguments.size();
}


std::string 
CommandLineParser::getNextArgument()
{
  if ( hasNextArgument() )
    return m_arguments[ m_currentArgument++ ];
  return "";
}


std::string 
CommandLineParser::getCurrentArgument() const
{
  if ( m_currentArgument < m_arguments.size() )
    return m_arguments[ m_currentArgument ];
  return "";
}


bool 
CommandLineParser::argumentStartsWith( const std::string &expected ) const
{
  return getCurrentArgument().substr( 0, expected.length() ) == expected;
}


void 
CommandLineParser::getNextOption()
{
  if ( argumentStartsWith( "-" )  ||  argumentStartsWith( "--" ) )
    m_option = getNextArgument();
  else
    m_option = "";
}


bool 
CommandLineParser::isOption( const std::string &shortName,
                             const std::string &longName )
{
  return (m_option == "-" + shortName)  ||
         (m_option == "--" + longName);
}


std::string 
CommandLineParser::getNextParameter()
{
  if ( !hasNextArgument() )
    fail( "missing parameter" );
  return getNextArgument();
}


std::string
CommandLineParser::getNextOptionalParameter()
{
  if ( argumentStartsWith( "-" )  ||  argumentStartsWith( ":" ) )
    return "";
  return getNextArgument();
}


void 
CommandLineParser::fail( std::string message )
{
  throw CommandLineParserException( "while parsing option " + m_option+
            ",\n" + message );
}


bool 
CommandLineParser::useCompilerOutputter() const
{
  return m_useCompiler;
}


bool 
CommandLineParser::useXmlOutputter() const
{
  return m_useXml;
}


std::string 
CommandLineParser::getXmlFileName() const
{
  return m_xmlFileName;
}


std::string 
CommandLineParser::getXmlStyleSheet() const
{
  return m_xsl;
}


std::string 
CommandLineParser::getEncoding() const
{
  return m_encoding;
}


bool 
CommandLineParser::useBriefTestProgress() const
{
  return m_briefProgress;
}


bool 
CommandLineParser::noTestProgress() const
{
  return m_noProgress;
}


bool 
CommandLineParser::useTextOutputter() const
{
  return m_useText;
}


bool 
CommandLineParser::useCoutStream() const
{
  return m_useCout;
}


bool 
CommandLineParser::waitBeforeExit() const
{
  return m_waitBeforeExit;
}


int 
CommandLineParser::getPlugInCount() const
{
  return m_plugIns.size(); 
}

CommandLinePlugInInfo 
CommandLineParser::getPlugInAt( int index ) const
{
  return m_plugIns[ index ];
}


std::string 
CommandLineParser::getTestPath() const
{
  return m_testPath;
}