From b1859b1a1b35194abb9d55a35d8bad202db39776 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 15 Mar 2008 02:33:25 +0000 Subject: [PATCH] added support for double arguments to wxCmdLineParser (patch 1907289) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@52530 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/cmdline.h | 5 +++++ interface/cmdline.h | 7 ++++--- samples/console/console.cpp | 5 +++++ src/common/cmdline.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 3 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 1575490..ecb672e 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -198,6 +198,7 @@ All: - Added wxMessageQueue class for inter-thread communications - Use UTF-8 for Unicode data in wxIPC classes (Anders Larsen) - Added support for user-defined types to wxConfig (Marcin Wojdyr). +- Added numeric options support to wxCmdLineParser (crjjrc) - Added wxJoin() and wxSplit() functions (Francesco Montorsi). - Added wxDateTime::FormatISOCombined() and ParseISODate/Time/Combined() - Added wxMutex::LockTimeout() (Aleksandr Napylov). diff --git a/include/wx/cmdline.h b/include/wx/cmdline.h index 44d4b17..8df967b 100644 --- a/include/wx/cmdline.h +++ b/include/wx/cmdline.h @@ -45,6 +45,7 @@ enum wxCmdLineParamType wxCMD_LINE_VAL_STRING, // should be 0 (default) wxCMD_LINE_VAL_NUMBER, wxCMD_LINE_VAL_DATE, + wxCMD_LINE_VAL_DOUBLE, wxCMD_LINE_VAL_NONE }; @@ -205,6 +206,10 @@ public: // the value in the provided pointer bool Found(const wxString& name, long *value) const; + // returns true if an option taking a double value was found and stores + // the value in the provided pointer + bool Found(const wxString& name, double *value) const; + #if wxUSE_DATETIME // returns true if an option taking a date value was found and stores the // value in the provided pointer diff --git a/interface/cmdline.h b/interface/cmdline.h index a3d69f0..4a4ea4a 100644 --- a/interface/cmdline.h +++ b/interface/cmdline.h @@ -185,9 +185,10 @@ public: value in the provided pointer (which should not be @NULL). */ bool Found(const wxString& name) const; - const bool Found(const wxString& name, wxString* value) const; - const bool Found(const wxString& name, long* value) const; - const bool Found(const wxString& name, wxDateTime* value) const; + bool Found(const wxString& name, wxString* value) const; + bool Found(const wxString& name, long* value) const; + bool Found(const wxString& name, double* value) const; + bool Found(const wxString& name, wxDateTime* value) const; //@} /** diff --git a/samples/console/console.cpp b/samples/console/console.cpp index 4eed13c..1744e90 100644 --- a/samples/console/console.cpp +++ b/samples/console/console.cpp @@ -150,6 +150,7 @@ static void ShowCmdLine(const wxCmdLineParser& parser) wxString strVal; long lVal; + double dVal; wxDateTime dt; if ( parser.Found(_T("o"), &strVal) ) s << _T("Output file:\t") << strVal << '\n'; @@ -157,6 +158,8 @@ static void ShowCmdLine(const wxCmdLineParser& parser) s << _T("Input dir:\t") << strVal << '\n'; if ( parser.Found(_T("s"), &lVal) ) s << _T("Size:\t") << lVal << '\n'; + if ( parser.Found(_T("f"), &dVal) ) + s << _T("Double:\t") << dVal << '\n'; if ( parser.Found(_T("d"), &dt) ) s << _T("Date:\t") << dt.FormatISODate() << '\n'; if ( parser.Found(_T("project_name"), &strVal) ) @@ -4262,6 +4265,8 @@ int main(int argc, char **argv) wxCMD_LINE_VAL_NUMBER }, { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE }, + { wxCMD_LINE_OPTION, "f", "double", "output double", + wxCMD_LINE_VAL_DOUBLE }, { wxCMD_LINE_PARAM, NULL, NULL, "input file", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE }, diff --git a/src/common/cmdline.cpp b/src/common/cmdline.cpp index c934ae4..63ee43c 100644 --- a/src/common/cmdline.cpp +++ b/src/common/cmdline.cpp @@ -111,6 +111,8 @@ struct wxCmdLineOption wxASSERT_MSG( type == typ, _T("type mismatch in wxCmdLineOption") ); } + double GetDoubleVal() const + { Check(wxCMD_LINE_VAL_DOUBLE); return m_doubleVal; } long GetLongVal() const { Check(wxCMD_LINE_VAL_NUMBER); return m_longVal; } const wxString& GetStrVal() const @@ -120,6 +122,8 @@ struct wxCmdLineOption { Check(wxCMD_LINE_VAL_DATE); return m_dateVal; } #endif // wxUSE_DATETIME + void SetDoubleVal(double val) + { Check(wxCMD_LINE_VAL_DOUBLE); m_doubleVal = val; m_hasVal = true; } void SetLongVal(long val) { Check(wxCMD_LINE_VAL_NUMBER); m_longVal = val; m_hasVal = true; } void SetStrVal(const wxString& val) @@ -143,6 +147,7 @@ public: private: bool m_hasVal; + double m_doubleVal; long m_longVal; wxString m_strVal; #if wxUSE_DATETIME @@ -510,6 +515,25 @@ bool wxCmdLineParser::Found(const wxString& name, long *value) const return true; } +bool wxCmdLineParser::Found(const wxString& name, double *value) const +{ + int i = m_data->FindOption(name); + if ( i == wxNOT_FOUND ) + i = m_data->FindOptionByLongName(name); + + wxCHECK_MSG( i != wxNOT_FOUND, false, _T("unknown option") ); + + wxCmdLineOption& opt = m_data->m_options[(size_t)i]; + if ( !opt.HasValue() ) + return false; + + wxCHECK_MSG( value, false, _T("NULL pointer in wxCmdLineOption::Found") ); + + *value = opt.GetDoubleVal(); + + return true; +} + #if wxUSE_DATETIME bool wxCmdLineParser::Found(const wxString& name, wxDateTime *value) const { @@ -802,6 +826,24 @@ int wxCmdLineParser::Parse(bool showUsage) } break; + case wxCMD_LINE_VAL_DOUBLE: + { + double val; + if ( value.ToDouble(&val) ) + { + opt.SetDoubleVal(val); + } + else + { + errorMsg << wxString::Format(_("'%s' is not a correct numeric value for option '%s'."), + value.c_str(), name.c_str()) + << _T('\n'); + + ok = false; + } + } + break; + #if wxUSE_DATETIME case wxCMD_LINE_VAL_DATE: { @@ -1132,6 +1174,10 @@ static wxString GetTypeName(wxCmdLineParamType type) s = _("num"); break; + case wxCMD_LINE_VAL_DOUBLE: + s = _("double"); + break; + case wxCMD_LINE_VAL_DATE: s = _("date"); break; -- 2.7.4