From: Vadim Zeitlin Date: Thu, 21 Jul 2011 13:49:55 +0000 (+0000) Subject: Fix parsing of negated long options in wxCmdLineParser. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/1a63798c5a3c763a431f1fd3705e89249d28eb53 Fix parsing of negated long options in wxCmdLineParser. Negated long options were not recognized in wxCmdLineParser::Parse(), do check for them now. Also extend the unit test to check for negated options. Closes #13335. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@68316 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index 9c0c96f63e..a42d63c195 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -437,6 +437,10 @@ Major new features in this release 2.9.3: ------ +All: + +- Fix parsing of negated long options in wxCmdLineParser (roed_bis). + All (GUI): - Support float, double and file name values in wxGenericValidator (troelsk). diff --git a/src/common/cmdline.cpp b/src/common/cmdline.cpp index 4abba86ee8..2952c2d6ae 100644 --- a/src/common/cmdline.cpp +++ b/src/common/cmdline.cpp @@ -707,11 +707,45 @@ int wxCmdLineParser::Parse(bool showUsage) if (longOptionsEnabled) { + wxString errorOpt; + optInd = m_data->FindOptionByLongName(name); if ( optInd == wxNOT_FOUND ) { - errorMsg << wxString::Format(_("Unknown long option '%s'"), name.c_str()) - << wxT('\n'); + // Check if this could be a negatable long option. + if ( name.Last() == '-' ) + { + name.RemoveLast(); + + optInd = m_data->FindOptionByLongName(name); + if ( optInd != wxNOT_FOUND ) + { + if ( !(m_data->m_options[optInd].flags & + wxCMD_LINE_SWITCH_NEGATABLE) ) + { + errorOpt.Printf + ( + _("Option '%s' can't be negated"), + name + ); + optInd = wxNOT_FOUND; + } + } + } + + if ( optInd == wxNOT_FOUND ) + { + if ( errorOpt.empty() ) + { + errorOpt.Printf + ( + _("Unknown long option '%s'"), + name + ); + } + + errorMsg << errorOpt << wxT('\n'); + } } } else diff --git a/tests/cmdline/cmdlinetest.cpp b/tests/cmdline/cmdlinetest.cpp index 07b030ff37..82f6548e65 100644 --- a/tests/cmdline/cmdlinetest.cpp +++ b/tests/cmdline/cmdlinetest.cpp @@ -139,6 +139,8 @@ void CmdLineTestCase::ParseSwitches() p.AddSwitch("b"); p.AddSwitch("c"); p.AddSwitch("d"); + p.AddSwitch("n", "neg", "Switch that can be negated", + wxCMD_LINE_SWITCH_NEGATABLE); p.SetCmdLine(""); CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); @@ -182,6 +184,23 @@ void CmdLineTestCase::ParseSwitches() CPPUNIT_ASSERT( !p.Found("b") ); CPPUNIT_ASSERT( !p.Found("c") ); CPPUNIT_ASSERT( p.Found("d") ); + + p.SetCmdLine("-n"); + CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); + CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_NOT_FOUND, p.FoundSwitch("a") ); + CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_ON, p.FoundSwitch("n") ); + + p.SetCmdLine("-n-"); + CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); + CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_OFF, p.FoundSwitch("neg") ); + + p.SetCmdLine("--neg"); + CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); + CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_ON, p.FoundSwitch("neg") ); + + p.SetCmdLine("--neg-"); + CPPUNIT_ASSERT_EQUAL(0, p.Parse(false) ); + CPPUNIT_ASSERT_EQUAL(wxCMD_SWITCH_OFF, p.FoundSwitch("n") ); } void CmdLineTestCase::Usage()