diff options
| author | Baptiste Lepilleur <gaiacrtn@free.fr> | 2002-04-21 09:57:52 +0000 |
|---|---|---|
| committer | Baptiste Lepilleur <gaiacrtn@free.fr> | 2002-04-21 09:57:52 +0000 |
| commit | ca5f94534beb3fb395a397a2fe991f4c64fb2f84 (patch) | |
| tree | 1ab95c541266dc554500d201865f683616a11dbc /src/DllPlugInTester/CommandLineParser.cpp | |
| parent | aa3e46e802dc5139e3e2c32e6a00526697e8a0aa (diff) | |
| download | cppunit-ca5f94534beb3fb395a397a2fe991f4c64fb2f84.tar.gz | |
Src/DllPlugInTester/makefile.
src/DllPlugInTester/makefile.am: removed ld.so from LDADD flags.
* src/DllPlugInTester/CommandLineParser.h:
* src/DllPlugInTester/CommandLineParser.cpp: rewrote, fixed problem
with double quotes in command line...
* src/DllPlugInTester/CommandLineParserTest.h:
* src/DllPlugInTester/CommandLineParserTest.cpp:
* src/DllPlugInTester/DllPlugInTesterTest.cpp: added, unit tests for
CommandLineParser.
* src/msvc6/TestPlugIn/*: removed.
Diffstat (limited to 'src/DllPlugInTester/CommandLineParser.cpp')
| -rw-r--r-- | src/DllPlugInTester/CommandLineParser.cpp | 191 |
1 files changed, 59 insertions, 132 deletions
diff --git a/src/DllPlugInTester/CommandLineParser.cpp b/src/DllPlugInTester/CommandLineParser.cpp index e365ca3..1187266 100644 --- a/src/DllPlugInTester/CommandLineParser.cpp +++ b/src/DllPlugInTester/CommandLineParser.cpp @@ -3,7 +3,7 @@ CommandLineParser::CommandLineParser( int argc, char *argv[] ) - : m_currentIndex( 0 ) + : m_currentArgument( 0 ) , m_useCompiler( false ) , m_useXml( false ) , m_briefProgress( false ) @@ -13,9 +13,8 @@ CommandLineParser::CommandLineParser( int argc, { for ( int index =1; index < argc; ++index ) { - if ( index > 1 ) - m_line += " "; - m_line += argv[index]; + std::string argument( argv[index ] ); + m_arguments.push_back( argument ); } } @@ -28,7 +27,7 @@ CommandLineParser::~CommandLineParser() void CommandLineParser::parse() { - while ( hasNext() ) + while ( hasNextArgument() ) { getNextOption(); if ( isOption( "c", "compiler" ) ) @@ -36,12 +35,12 @@ CommandLineParser::parse() else if ( isOption( "x", "xml" ) ) { m_useXml = true; - m_xmlFileName = getOptionalParameter(); + m_xmlFileName = getNextOptionalParameter(); } else if ( isOption( "s", "xsl" ) ) - m_xsl = getParameter(); + m_xsl = getNextParameter(); else if ( isOption( "e", "encoding" ) ) - m_encoding = getParameter(); + m_encoding = getNextParameter(); else if ( isOption( "b", "brief-progress" ) ) m_briefProgress = true; else if ( isOption( "n", "no-progress" ) ) @@ -52,186 +51,114 @@ CommandLineParser::parse() m_useCout = true; else if ( !m_option.empty() ) fail( "Unknown option" ); - else if ( hasNext() ) + else if ( hasNextArgument() ) readNonOptionCommands(); } } -bool -CommandLineParser::isOption( const std::string &shortName, - const std::string &longName ) -{ - return (m_option == "-" + shortName) || - (m_option == "--" + longName); -} - - -bool -CommandLineParser::hasNext() const -{ - return m_currentIndex < m_line.length(); -} - - -std::string -CommandLineParser::getParameter() +void +CommandLineParser::readNonOptionCommands() { - if ( startsWith( "\"" ) ) - return getQuotedParameter(); + if ( argumentStartsWith( ":" ) ) + m_testPath = getNextArgument().substr( 1 ); else - return getUnquotedParameter(); -} - - -std::string -CommandLineParser::getUnquotedParameter() -{ - std::string parameter; - - if ( !hasNext() ) - fail( "missing option parameter" ); - - while ( hasNext() && !isSpace() ) - parameter += next(); - return parameter; -} - - -std::string -CommandLineParser::getQuotedParameter() -{ - std::string parameter; - while ( !startsWith( "\"" ) ) { - if ( !hasNext() ) - fail( "Unmatched \" in option parameter" ); - - if ( startsWith( "\\" ) ) + CommandLinePlugInInfo plugIn; + int indexParameter = getCurrentArgument().find( '=' ); + if ( indexParameter < 0 ) + plugIn.m_fileName = getCurrentArgument(); + else { - skipNext(); - if ( !hasNext() ) - fail( "Missing escaped character in option parameter" ); + plugIn.m_fileName = getCurrentArgument().substr( 0, indexParameter ); + std::string parameters = getCurrentArgument().substr( indexParameter +1 ); + plugIn.m_parameters.push_back( parameters ); } + + m_plugIns.push_back( plugIn ); - parameter += next(); + getNextArgument(); } - return parameter; } -std::string -CommandLineParser::getOptionalParameter() +bool +CommandLineParser::hasNextArgument() const { - if ( !hasNext() || startsWith( "-" ) || startsWith( ":" ) ) - return ""; - return getParameter(); + return m_currentArgument < m_arguments.size(); } -void -CommandLineParser::getNextOption() +std::string +CommandLineParser::getNextArgument() { - skipSpaces(); - m_option = ""; - if ( startsWith( "-" ) || startsWith( "--" ) ) - { - while ( hasNext() && !isSpace() ) - m_option += next(); - - skipSpaces(); - } + if ( hasNextArgument() ) + return m_arguments[ m_currentArgument++ ]; + return ""; } -void -CommandLineParser::readNonOptionCommands() +std::string +CommandLineParser::getCurrentArgument() const { - if ( startsWith( ":" ) ) - { - skipNext(); - m_testPath = getParameter(); - } - else - { - CommandLinePlugInInfo plugIn; - while ( hasNext() && !isSpace() && !startsWith( "=" ) ) - plugIn.m_fileName += next(); - - std::string parameters; - if ( startsWith( "=" ) ) - { - m_option = plugIn.m_fileName; - skipNext(); - parameters = getParameter(); - } - - plugIn.m_parameters.push_back( parameters ); - m_plugIns.push_back( plugIn ); - } + if ( m_currentArgument < m_arguments.size() ) + return m_arguments[ m_currentArgument ]; + return ""; } bool -CommandLineParser::startsWith( const std::string &expected ) const +CommandLineParser::argumentStartsWith( const std::string &expected ) const { - return m_line.substr( m_currentIndex, expected.length() ) == expected; + return getCurrentArgument().substr( 0, expected.length() ) == expected; } void -CommandLineParser::skipSpaces() -{ - while ( hasNext() && isSpace() ) - skipNext(); -} - - -bool -CommandLineParser::isSpace() const +CommandLineParser::getNextOption() { - if ( !hasNext() ) - return true; - - return isSpace( m_line[m_currentIndex] ); + if ( argumentStartsWith( "-" ) || argumentStartsWith( "--" ) ) + m_option = getNextArgument(); + else + m_option = ""; } bool -CommandLineParser::isSpace( unsigned char c ) +CommandLineParser::isOption( const std::string &shortName, + const std::string &longName ) { - return c <= 32; + return (m_option == "-" + shortName) || + (m_option == "--" + longName); } -char -CommandLineParser::next() +std::string +CommandLineParser::getNextParameter() { - if ( !hasNext() ) - fail( "unexpected error while parsing option" ); - - return m_line[ m_currentIndex++ ]; + if ( !hasNextArgument() ) + fail( "missing parameter" ); + return getNextArgument(); } -void -CommandLineParser::skipNext( int count ) +std::string +CommandLineParser::getNextOptionalParameter() { - m_currentIndex += count; - if ( m_currentIndex > m_line.length() ) - m_currentIndex = m_line.length(); + if ( argumentStartsWith( "-" ) || argumentStartsWith( ":" ) ) + return ""; + return getNextArgument(); } void CommandLineParser::fail( std::string message ) { - throw CommandLineParserException( "Error while parsing option: " + m_option+ - "\n" + message ); + throw CommandLineParserException( "while parsing option " + m_option+ + ",\n" + message ); } - bool CommandLineParser::useCompilerOutputter() const { |
