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
This commit is contained in:
Vadim Zeitlin
2008-03-15 02:33:25 +00:00
parent 7999b83011
commit b1859b1a1b
5 changed files with 61 additions and 3 deletions

View File

@@ -198,6 +198,7 @@ All:
- Added wxMessageQueue class for inter-thread communications - Added wxMessageQueue class for inter-thread communications
- Use UTF-8 for Unicode data in wxIPC classes (Anders Larsen) - Use UTF-8 for Unicode data in wxIPC classes (Anders Larsen)
- Added support for user-defined types to wxConfig (Marcin Wojdyr). - 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 wxJoin() and wxSplit() functions (Francesco Montorsi).
- Added wxDateTime::FormatISOCombined() and ParseISODate/Time/Combined() - Added wxDateTime::FormatISOCombined() and ParseISODate/Time/Combined()
- Added wxMutex::LockTimeout() (Aleksandr Napylov). - Added wxMutex::LockTimeout() (Aleksandr Napylov).

View File

@@ -45,6 +45,7 @@ enum wxCmdLineParamType
wxCMD_LINE_VAL_STRING, // should be 0 (default) wxCMD_LINE_VAL_STRING, // should be 0 (default)
wxCMD_LINE_VAL_NUMBER, wxCMD_LINE_VAL_NUMBER,
wxCMD_LINE_VAL_DATE, wxCMD_LINE_VAL_DATE,
wxCMD_LINE_VAL_DOUBLE,
wxCMD_LINE_VAL_NONE wxCMD_LINE_VAL_NONE
}; };
@@ -205,6 +206,10 @@ public:
// the value in the provided pointer // the value in the provided pointer
bool Found(const wxString& name, long *value) const; 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 #if wxUSE_DATETIME
// returns true if an option taking a date value was found and stores the // returns true if an option taking a date value was found and stores the
// value in the provided pointer // value in the provided pointer

View File

@@ -185,9 +185,10 @@ public:
value in the provided pointer (which should not be @NULL). value in the provided pointer (which should not be @NULL).
*/ */
bool Found(const wxString& name) const; bool Found(const wxString& name) const;
const bool Found(const wxString& name, wxString* value) const; bool Found(const wxString& name, wxString* value) const;
const bool Found(const wxString& name, long* value) const; bool Found(const wxString& name, long* value) const;
const bool Found(const wxString& name, wxDateTime* value) const; bool Found(const wxString& name, double* value) const;
bool Found(const wxString& name, wxDateTime* value) const;
//@} //@}
/** /**

View File

@@ -150,6 +150,7 @@ static void ShowCmdLine(const wxCmdLineParser& parser)
wxString strVal; wxString strVal;
long lVal; long lVal;
double dVal;
wxDateTime dt; wxDateTime dt;
if ( parser.Found(_T("o"), &strVal) ) if ( parser.Found(_T("o"), &strVal) )
s << _T("Output file:\t") << strVal << '\n'; s << _T("Output file:\t") << strVal << '\n';
@@ -157,6 +158,8 @@ static void ShowCmdLine(const wxCmdLineParser& parser)
s << _T("Input dir:\t") << strVal << '\n'; s << _T("Input dir:\t") << strVal << '\n';
if ( parser.Found(_T("s"), &lVal) ) if ( parser.Found(_T("s"), &lVal) )
s << _T("Size:\t") << lVal << '\n'; s << _T("Size:\t") << lVal << '\n';
if ( parser.Found(_T("f"), &dVal) )
s << _T("Double:\t") << dVal << '\n';
if ( parser.Found(_T("d"), &dt) ) if ( parser.Found(_T("d"), &dt) )
s << _T("Date:\t") << dt.FormatISODate() << '\n'; s << _T("Date:\t") << dt.FormatISODate() << '\n';
if ( parser.Found(_T("project_name"), &strVal) ) if ( parser.Found(_T("project_name"), &strVal) )
@@ -4262,6 +4265,8 @@ int main(int argc, char **argv)
wxCMD_LINE_VAL_NUMBER }, wxCMD_LINE_VAL_NUMBER },
{ wxCMD_LINE_OPTION, "d", "date", "output file date", { wxCMD_LINE_OPTION, "d", "date", "output file date",
wxCMD_LINE_VAL_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_PARAM, NULL, NULL, "input file",
wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE }, wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },

View File

@@ -111,6 +111,8 @@ struct wxCmdLineOption
wxASSERT_MSG( type == typ, _T("type mismatch in wxCmdLineOption") ); wxASSERT_MSG( type == typ, _T("type mismatch in wxCmdLineOption") );
} }
double GetDoubleVal() const
{ Check(wxCMD_LINE_VAL_DOUBLE); return m_doubleVal; }
long GetLongVal() const long GetLongVal() const
{ Check(wxCMD_LINE_VAL_NUMBER); return m_longVal; } { Check(wxCMD_LINE_VAL_NUMBER); return m_longVal; }
const wxString& GetStrVal() const const wxString& GetStrVal() const
@@ -120,6 +122,8 @@ struct wxCmdLineOption
{ Check(wxCMD_LINE_VAL_DATE); return m_dateVal; } { Check(wxCMD_LINE_VAL_DATE); return m_dateVal; }
#endif // wxUSE_DATETIME #endif // wxUSE_DATETIME
void SetDoubleVal(double val)
{ Check(wxCMD_LINE_VAL_DOUBLE); m_doubleVal = val; m_hasVal = true; }
void SetLongVal(long val) void SetLongVal(long val)
{ Check(wxCMD_LINE_VAL_NUMBER); m_longVal = val; m_hasVal = true; } { Check(wxCMD_LINE_VAL_NUMBER); m_longVal = val; m_hasVal = true; }
void SetStrVal(const wxString& val) void SetStrVal(const wxString& val)
@@ -143,6 +147,7 @@ public:
private: private:
bool m_hasVal; bool m_hasVal;
double m_doubleVal;
long m_longVal; long m_longVal;
wxString m_strVal; wxString m_strVal;
#if wxUSE_DATETIME #if wxUSE_DATETIME
@@ -510,6 +515,25 @@ bool wxCmdLineParser::Found(const wxString& name, long *value) const
return true; 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 #if wxUSE_DATETIME
bool wxCmdLineParser::Found(const wxString& name, wxDateTime *value) const bool wxCmdLineParser::Found(const wxString& name, wxDateTime *value) const
{ {
@@ -802,6 +826,24 @@ int wxCmdLineParser::Parse(bool showUsage)
} }
break; 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 #if wxUSE_DATETIME
case wxCMD_LINE_VAL_DATE: case wxCMD_LINE_VAL_DATE:
{ {
@@ -1132,6 +1174,10 @@ static wxString GetTypeName(wxCmdLineParamType type)
s = _("num"); s = _("num");
break; break;
case wxCMD_LINE_VAL_DOUBLE:
s = _("double");
break;
case wxCMD_LINE_VAL_DATE: case wxCMD_LINE_VAL_DATE:
s = _("date"); s = _("date");
break; break;