#pragma hdrstop
#endif
+#include "wx/cmdline.h"
+
#if wxUSE_CMDLINE_PARSER
#ifndef WX_PRECOMP
#include <ctype.h>
#include "wx/datetime.h"
-#include "wx/cmdline.h"
// ----------------------------------------------------------------------------
// private functions
wxCmdLineParamType typ,
int fl)
{
+ wxASSERT_MSG( !shrt.empty() || !lng.empty(),
+ _T("option should have at least one name") );
+
kind = k;
shortName = shrt;
// 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();
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 ++;
-
- wxString arg(cmdLine.Mid(first, (i - first)));
-
- m_arguments.Add(arg);
+ wxArrayString args = wxCmdLineParser::ConvertStringToArgs(cmdLine);
- 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);
- }
- }
- }
+ WX_APPEND_ARRAY(m_arguments, args);
}
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];
}
// ----------------------------------------------------------------------------
-// global functions
+// private functions
// ----------------------------------------------------------------------------
static wxString GetTypeName(wxCmdLineParamType type)
}
#endif // wxUSE_CMDLINE_PARSER
+
+// ----------------------------------------------------------------------------
+// global functions
+// ----------------------------------------------------------------------------
+
+/* static */
+wxArrayString wxCmdLineParser::ConvertStringToArgs(const wxChar *p)
+{
+ wxArrayString args;
+
+ wxString arg;
+ arg.reserve(1024);
+
+ bool isInsideQuotes = FALSE;
+ for ( ;; )
+ {
+ // skip white space
+ while ( *p == _T(' ') || *p == _T('\t') )
+ p++;
+
+ // anything left?
+ if ( *p == _T('\0') )
+ break;
+
+ // parse this parameter
+ arg.clear();
+ for ( ;; p++ )
+ {
+ // do we have a (lone) backslash?
+ bool isQuotedByBS = FALSE;
+ while ( *p == _T('\\') )
+ {
+ p++;
+
+ // if we have 2 backslashes in a row, output one
+ if ( isQuotedByBS )
+ {
+ arg += _T('\\');
+ isQuotedByBS = FALSE;
+ }
+ else // the next char is quoted
+ {
+ isQuotedByBS = TRUE;
+ }
+ }
+
+ bool skipChar = FALSE,
+ endParam = FALSE;
+ switch ( *p )
+ {
+ case _T('"'):
+ if ( !isQuotedByBS )
+ {
+ // don't put the quote itself in the arg
+ skipChar = TRUE;
+
+ isInsideQuotes = !isInsideQuotes;
+ }
+ //else: insert a literal quote
+
+ break;
+
+ case _T(' '):
+ case _T('\t'):
+ if ( isInsideQuotes || isQuotedByBS )
+ {
+ // preserve it, skip endParam below
+ break;
+ }
+ //else: fall through
+
+ case _T('\0'):
+ endParam = TRUE;
+ break;
+
+ default:
+ if ( isQuotedByBS )
+ {
+ // ignore backslash before an ordinary character - this
+ // is needed to properly handle the file names under
+ // Windows appearing in the command line
+ arg += _T('\\');
+ }
+ }
+
+ // end of argument?
+ if ( endParam )
+ break;
+
+ // otherwise copy this char to arg
+ if ( !skipChar )
+ {
+ arg += *p;
+ }
+ }
+
+ args.Add(arg);
+ }
+
+ return args;
+}
+