From 758f356c53a3f473f642361ec09422405046148d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 7 Sep 2009 23:00:05 +0000 Subject: [PATCH] Fix bug with parsing concatenated switches in wxCmdLineParser. The constructs such as "-abcd" were not parsed correctly, i.e. the same as "-a -b -c -d" because the code tried to parse a non-existent option "abcd" even if it was supposed to not do this -- fix this. Closes #11180. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61851 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + src/common/cmdline.cpp | 4 +++ tests/cmdline/cmdlinetest.cpp | 67 +++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/docs/changes.txt b/docs/changes.txt index 2a841d11e9..b609c73dc4 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -374,6 +374,7 @@ All: - Fix output buffer overflow in wxBase64Decode() (Eric W. Savage). - Added bilinear image resizing algorithm to wxImage (bishop). - Fix bug with position argument in wxImage::Size() (Byron Sorgdrager). +- Fix bug with parsing concatenated switches in wxCmdLineParser (Mike Funduc). All (GUI): diff --git a/src/common/cmdline.cpp b/src/common/cmdline.cpp index e8058e2979..1a143d476c 100644 --- a/src/common/cmdline.cpp +++ b/src/common/cmdline.cpp @@ -729,6 +729,10 @@ int wxCmdLineParser::Parse(bool showUsage) m_data->m_arguments.insert (m_data->m_arguments.begin() + n + 1, arg2); count++; + + // only leave the part which wasn't extracted into the + // next argument in this one + arg = arg.Left(len + 1); } //else: it's our value, we'll deal with it below } diff --git a/tests/cmdline/cmdlinetest.cpp b/tests/cmdline/cmdlinetest.cpp index 04e5c2ac83..87d02b2d16 100644 --- a/tests/cmdline/cmdlinetest.cpp +++ b/tests/cmdline/cmdlinetest.cpp @@ -21,6 +21,8 @@ #endif // WX_PRECOMP #include "wx/cmdline.h" +#include "wx/msgout.h" +#include "wx/scopeguard.h" // -------------------------------------------------------------------------- // test class @@ -34,10 +36,12 @@ public: private: CPPUNIT_TEST_SUITE( CmdLineTestCase ); CPPUNIT_TEST( ConvertStringTestCase ); + CPPUNIT_TEST( ParseSwitches ); CPPUNIT_TEST( Usage ); CPPUNIT_TEST_SUITE_END(); void ConvertStringTestCase(); + void ParseSwitches(); void Usage(); DECLARE_NO_COPY_CLASS(CmdLineTestCase) @@ -117,6 +121,69 @@ void CmdLineTestCase::ConvertStringTestCase() #undef WX_ASSERT_ARGS_EQUAL } +void CmdLineTestCase::ParseSwitches() +{ + // install a dummy message output object just suppress error messages from + // wxCmdLineParser::Parse() + class NoMessageOutput : public wxMessageOutput + { + public: + virtual void Output(const wxString& WXUNUSED(str)) { } + } noMessages; + + wxMessageOutput * const old = wxMessageOutput::Set(&noMessages); + wxON_BLOCK_EXIT1( wxMessageOutput::Set, old ); + + wxCmdLineParser p; + p.AddSwitch("a"); + p.AddSwitch("b"); + p.AddSwitch("c"); + p.AddSwitch("d"); + + p.SetCmdLine(""); + CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); + CPPUNIT_ASSERT( !p.Found("a") ); + + p.SetCmdLine("-z"); + CPPUNIT_ASSERT( p.Parse(false) != 0 ); + + p.SetCmdLine("-a"); + CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); + CPPUNIT_ASSERT( p.Found("a") ); + CPPUNIT_ASSERT( !p.Found("b") ); + + p.SetCmdLine("-a -d"); + CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); + CPPUNIT_ASSERT( p.Found("a") ); + CPPUNIT_ASSERT( !p.Found("b") ); + CPPUNIT_ASSERT( !p.Found("c") ); + CPPUNIT_ASSERT( p.Found("d") ); + + p.SetCmdLine("-abd"); + CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); + CPPUNIT_ASSERT( p.Found("a") ); + CPPUNIT_ASSERT( p.Found("b") ); + CPPUNIT_ASSERT( !p.Found("c") ); + CPPUNIT_ASSERT( p.Found("d") ); + + p.SetCmdLine("-abdz"); + CPPUNIT_ASSERT( p.Parse(false) != 0 ); + + p.SetCmdLine("-ab -cd"); + CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); + CPPUNIT_ASSERT( p.Found("a") ); + CPPUNIT_ASSERT( p.Found("b") ); + CPPUNIT_ASSERT( p.Found("c") ); + CPPUNIT_ASSERT( p.Found("d") ); + + p.SetCmdLine("-da"); + CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); + CPPUNIT_ASSERT( p.Found("a") ); + CPPUNIT_ASSERT( !p.Found("b") ); + CPPUNIT_ASSERT( !p.Found("c") ); + CPPUNIT_ASSERT( p.Found("d") ); +} + void CmdLineTestCase::Usage() { // check that Usage() returns roughly what we expect (don't check all the -- 2.47.2