X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/de53ac1ee1d7a5c283cff6fdd56af99b2a8e99f1..1f361cddbfcff9c54eef6b8c447d2b93ee2825f5:/src/common/cmdline.cpp diff --git a/src/common/cmdline.cpp b/src/common/cmdline.cpp index 9437f57147..2c5ea56421 100644 --- a/src/common/cmdline.cpp +++ b/src/common/cmdline.cpp @@ -28,6 +28,10 @@ #pragma hdrstop #endif +#include "wx/cmdline.h" + +#if wxUSE_CMDLINE_PARSER + #ifndef WX_PRECOMP #include "wx/string.h" #include "wx/log.h" @@ -40,8 +44,6 @@ #include #include "wx/datetime.h" -#include "wx/cmdline.h" -#include "wx/app.h" // ---------------------------------------------------------------------------- // private functions @@ -63,6 +65,9 @@ struct wxCmdLineOption wxCmdLineParamType typ, int fl) { + wxASSERT_MSG( !shrt.empty() || !lng.empty(), + _T("option should have at least one name") ); + kind = k; shortName = shrt; @@ -99,12 +104,14 @@ struct wxCmdLineOption 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: wxCmdLineEntryType kind; - wxString shortName, longName, description; + wxString shortName, + longName, + description; wxCmdLineParamType type; int flags; @@ -156,7 +163,7 @@ struct wxCmdLineParserData // methods wxCmdLineParserData(); - void SetArguments(int argc, char **argv); + void SetArguments(int argc, wxChar **argv); void SetArguments(const wxString& cmdline); int FindOption(const wxString& name); @@ -181,7 +188,7 @@ wxCmdLineParserData::wxCmdLineParserData() #endif } -void wxCmdLineParserData::SetArguments(int argc, char **argv) +void wxCmdLineParserData::SetArguments(int argc, wxChar **argv) { m_arguments.Empty(); @@ -197,55 +204,23 @@ void wxCmdLineParserData::SetArguments(const wxString& cmdLine) 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); - - 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))); + wxArrayString args = wxCmdLineParser::ConvertStringToArgs(cmdLine); - 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; + } } } @@ -276,7 +251,7 @@ void wxCmdLineParser::Init() m_data = new wxCmdLineParserData; } -void wxCmdLineParser::SetCmdLine(int argc, char **argv) +void wxCmdLineParser::SetCmdLine(int argc, wxChar **argv) { m_data->SetArguments(argc, argv); } @@ -409,6 +384,9 @@ void wxCmdLineParser::AddParam(const wxString& desc, 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]; @@ -421,6 +399,9 @@ bool wxCmdLineParser::Found(const wxString& name) const 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]; @@ -437,6 +418,9 @@ bool wxCmdLineParser::Found(const wxString& name, wxString *value) const 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]; @@ -453,6 +437,9 @@ bool wxCmdLineParser::Found(const wxString& name, long *value) const 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]; @@ -473,14 +460,27 @@ size_t wxCmdLineParser::GetParamCount() const wxString wxCmdLineParser::GetParam(size_t n) const { + wxCHECK_MSG( n < GetParamCount(), wxEmptyString, _T("invalid param index") ); + 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 // ---------------------------------------------------------------------------- -int wxCmdLineParser::Parse() +int wxCmdLineParser::Parse(bool showUsage) { bool maybeOption = TRUE; // can the following arg be an option? bool ok = TRUE; // TRUE until an error is detected @@ -491,6 +491,8 @@ int wxCmdLineParser::Parse() size_t countParam = m_data->m_paramDesc.GetCount(); + Reset(); + // parse everything wxString arg; size_t count = m_data->m_arguments.GetCount(); @@ -805,7 +807,7 @@ int wxCmdLineParser::Parse() } } - if ( !ok ) + if ( !ok && showUsage ) { Usage(); } @@ -851,13 +853,30 @@ void wxCmdLineParser::Usage() brief << _T('['); } - brief << chSwitch << opt.shortName; + if ( !opt.shortName.empty() ) + { + brief << chSwitch << opt.shortName; + } + else if ( !opt.longName.empty() ) + { + brief << _T("--") << opt.longName; + } + else + { + wxFAIL_MSG( _T("option without neither short nor long name?") ); + } wxString option; - option << _T(" ") << chSwitch << opt.shortName; - if ( !!opt.longName ) + + if ( !opt.shortName.empty() ) { - option << _T(" --") << opt.longName; + option << _T(" ") << chSwitch << opt.shortName; + } + + if ( !opt.longName.empty() ) + { + option << (option.empty() ? _T(" ") : _T(", ")) + << _T("--") << opt.longName; } if ( opt.kind != wxCMD_LINE_SWITCH ) @@ -906,7 +925,13 @@ void wxCmdLineParser::Usage() wxLogMessage(m_data->m_logo); } + // in console mode we want to show the brief usage message first, then the + // detailed one but in GUI build we give the details first and then the + // summary - like this, the brief message appears in the wxLogGui dialog, + // as expected +#if !wxUSE_GUI wxLogMessage(brief); +#endif // !wxUSE_GUI // now construct the detailed help message size_t len, lenMax = 0; @@ -929,10 +954,15 @@ void wxCmdLineParser::Usage() } wxLogMessage(detailed); + + // do it now if not done above +#if wxUSE_GUI + wxLogMessage(brief); +#endif // wxUSE_GUI } // ---------------------------------------------------------------------------- -// global functions +// private functions // ---------------------------------------------------------------------------- static wxString GetTypeName(wxCmdLineParamType type) @@ -951,3 +981,133 @@ static wxString GetTypeName(wxCmdLineParamType type) return s; } + +#endif // wxUSE_CMDLINE_PARSER + +// ---------------------------------------------------------------------------- +// global functions +// ---------------------------------------------------------------------------- + +/* + This function is mainly used under Windows (as under Unix we always get the + command line arguments as agrc/argv anyhow) and so it tries to handle the + Windows path names (separated by backslashes) correctly. For this it only + considers that a backslash may be used to escape another backslash (but + normally this is _not_ needed) or a quote but nothing else. + + In particular, to pass a single argument containing a space to the program + it should be quoted: + + myprog.exe foo bar -> argc = 3, argv[1] = "foo", argv[2] = "bar" + myprog.exe "foo bar" -> argc = 2, argv[1] = "foo bar" + + To pass an argument containing spaces and quotes, the latter should be + escaped with a backslash: + + myprog.exe "foo \"bar\"" -> argc = 2, argv[1] = "foo "bar"" + + This hopefully matches the conventions used by Explorer/command line + interpreter under Windows. If not, this function should be fixed. + */ + +/* 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'): + // we intentionally don't check for preceding backslash + // here as if we allowed it to be used to escape spaces the + // cmd line of the form "foo.exe a:\ c:\bar" wouldn't be + // parsed correctly + if ( isInsideQuotes ) + { + // 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; +} +