#pragma hdrstop
#endif
+#if wxUSE_CMDLINE_PARSER
+
#ifndef WX_PRECOMP
#include "wx/string.h"
#include "wx/log.h"
wxCmdLineParamType typ,
int fl)
{
+ wxASSERT_MSG( !shrt.empty() || !lng.empty(),
+ _T("option should have at least one name") );
+
kind = k;
shortName = shrt;
void SetDateVal(const wxDateTime val)
{ Check(wxCMD_LINE_VAL_DATE); m_dateVal = val; m_hasVal = TRUE; }
- void SetHasValue() { m_hasVal = TRUE; }
+ void SetHasValue(bool hasValue = TRUE) { m_hasVal = hasValue; }
bool HasValue() const { return m_hasVal; }
public:
// methods
wxCmdLineParserData();
- void SetArguments(int argc, char **argv);
+ void SetArguments(int argc, wxChar **argv);
void SetArguments(const wxString& cmdline);
int FindOption(const wxString& name);
#endif
}
-void wxCmdLineParserData::SetArguments(int argc, char **argv)
+void wxCmdLineParserData::SetArguments(int argc, wxChar **argv)
{
m_arguments.Empty();
}
}
-void wxCmdLineParserData::SetArguments(const wxString& WXUNUSED(cmdline))
+void wxCmdLineParserData::SetArguments(const wxString& cmdLine)
{
- // either use wxMSW wxApp::ConvertToStandardCommandArgs() or move its logic
- // here and use this method from it - but don't duplicate the code
+ m_arguments.Empty();
+
+ m_arguments.Add(wxTheApp->GetAppName());
+
+ // Break up string
+ // Treat strings enclosed in double-quotes as single arguments
+ int i = 0;
+ int len = cmdLine.Length();
+ while (i < len)
+ {
+ // Skip whitespace
+ while ((i < len) && wxIsspace(cmdLine.GetChar(i)))
+ i ++;
+
+ if (i < len)
+ {
+ if (cmdLine.GetChar(i) == wxT('"')) // We found the start of a string
+ {
+ i ++;
+ int first = i;
+ while ((i < len) && (cmdLine.GetChar(i) != wxT('"')))
+ i ++;
- wxFAIL_MSG(_T("TODO"));
+ wxString arg(cmdLine.Mid(first, (i - first)));
+
+ m_arguments.Add(arg);
+
+ if (i < len)
+ i ++; // Skip past 2nd quote
+ }
+ else // Unquoted argument
+ {
+ int first = i;
+ while ((i < len) && !wxIsspace(cmdLine.GetChar(i)))
+ i ++;
+
+ wxString arg(cmdLine.Mid(first, (i - first)));
+
+ m_arguments.Add(arg);
+ }
+ }
+ }
}
int wxCmdLineParserData::FindOption(const wxString& name)
{
- size_t count = m_options.GetCount();
- for ( size_t n = 0; n < count; n++ )
+ if ( !name.empty() )
{
- if ( m_options[n].shortName == name )
+ size_t count = m_options.GetCount();
+ for ( size_t n = 0; n < count; n++ )
{
- // found
- return n;
+ if ( m_options[n].shortName == name )
+ {
+ // found
+ return n;
+ }
}
}
m_data = new wxCmdLineParserData;
}
-void wxCmdLineParser::SetCmdLine(int argc, char **argv)
+void wxCmdLineParser::SetCmdLine(int argc, wxChar **argv)
{
m_data->SetArguments(argc, argv);
}
bool wxCmdLineParser::Found(const wxString& name) const
{
int i = m_data->FindOption(name);
+ if ( i == wxNOT_FOUND )
+ i = m_data->FindOptionByLongName(name);
+
wxCHECK_MSG( i != wxNOT_FOUND, FALSE, _T("unknown switch") );
wxCmdLineOption& opt = m_data->m_options[(size_t)i];
bool wxCmdLineParser::Found(const wxString& name, wxString *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];
bool wxCmdLineParser::Found(const wxString& name, long *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];
bool wxCmdLineParser::Found(const wxString& name, wxDateTime *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];
return m_data->m_parameters[n];
}
+// Resets switches and options
+void wxCmdLineParser::Reset()
+{
+ for ( size_t i = 0; i < m_data->m_options.Count(); i++ )
+ {
+ wxCmdLineOption& opt = m_data->m_options[i];
+ opt.SetHasValue(FALSE);
+ }
+}
+
+
// ----------------------------------------------------------------------------
// the real work is done here
// ----------------------------------------------------------------------------
size_t countParam = m_data->m_paramDesc.GetCount();
+ Reset();
+
// parse everything
wxString arg;
size_t count = m_data->m_arguments.GetCount();
return s;
}
+
+#endif // wxUSE_CMDLINE_PARSER