X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/fbdcff4a78e03963d750546b8a74752247b38d40..124193837f27d1fb75b9607f0e005b9897d1e320:/src/common/cmdline.cpp?ds=sidebyside diff --git a/src/common/cmdline.cpp b/src/common/cmdline.cpp index d98bb96d48..46517df3a2 100644 --- a/src/common/cmdline.cpp +++ b/src/common/cmdline.cpp @@ -28,6 +28,8 @@ #pragma hdrstop #endif +#if wxUSE_CMDLINE_PARSER + #ifndef WX_PRECOMP #include "wx/string.h" #include "wx/log.h" @@ -62,6 +64,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; @@ -98,7 +103,7 @@ 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: @@ -155,7 +160,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); @@ -180,7 +185,7 @@ wxCmdLineParserData::wxCmdLineParserData() #endif } -void wxCmdLineParserData::SetArguments(int argc, char **argv) +void wxCmdLineParserData::SetArguments(int argc, wxChar **argv) { m_arguments.Empty(); @@ -190,23 +195,64 @@ void wxCmdLineParserData::SetArguments(int argc, char **argv) } } -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 ++; + + 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))); - wxFAIL_MSG(_T("TODO")); + 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; + } } } @@ -237,7 +283,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); } @@ -370,6 +416,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]; @@ -382,6 +431,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]; @@ -398,6 +450,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]; @@ -414,6 +469,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]; @@ -437,6 +495,17 @@ wxString wxCmdLineParser::GetParam(size_t n) const 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 // ---------------------------------------------------------------------------- @@ -452,6 +521,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(); @@ -485,7 +556,7 @@ int wxCmdLineParser::Parse() isLong = TRUE; const wxChar *p = arg.c_str() + 2; - while ( wxIsalpha(*p) || (*p == _T('-')) ) + while ( wxIsalnum(*p) || (*p == _T('_')) || (*p == _T('-')) ) { name += *p++; } @@ -503,7 +574,7 @@ int wxCmdLineParser::Parse() // a short one: as they can be cumulated, we try to find the // longest substring which is a valid option const wxChar *p = arg.c_str() + 1; - while ( wxIsalpha(*p) ) + while ( wxIsalnum(*p) || (*p == _T('_')) ) { name += *p++; } @@ -596,6 +667,7 @@ int wxCmdLineParser::Parse() { switch ( *p ) { + case _T('='): case _T(':'): // the value follows p++; @@ -619,8 +691,15 @@ int wxCmdLineParser::Parse() break; default: - // the value is right here - ; + // the value is right here: this may be legal or + // not depending on the option style + if ( opt.flags & wxCMD_LINE_NEEDS_SEPARATOR ) + { + wxLogError(_("Separator expected after the option '%s'."), + name.c_str()); + + ok = FALSE; + } } } @@ -781,9 +860,18 @@ void wxCmdLineParser::Usage() wxStripExtension(appname); } - wxString brief, detailed; + // we construct the brief cmd line desc on the fly, but not the detailed + // help message below because we want to align the options descriptions + // and for this we must first know the longest one of them + wxString brief; + wxArrayString namesOptions, descOptions; brief.Printf(_("Usage: %s"), appname.c_str()); + // the switch char is usually '-' but this can be changed with + // SetSwitchChars() and then the first one of possible chars is used + wxChar chSwitch = !m_data->m_switchChars ? _T('-') + : m_data->m_switchChars[0u]; + size_t n, count = m_data->m_options.GetCount(); for ( n = 0; n < count; n++ ) { @@ -795,11 +883,13 @@ void wxCmdLineParser::Usage() brief << _T('['); } - brief << _T('-') << opt.shortName; - detailed << _T(" -") << opt.shortName; + brief << chSwitch << opt.shortName; + + wxString option; + option << _T(" ") << chSwitch << opt.shortName; if ( !!opt.longName ) { - detailed << _T(" --") << opt.longName; + option << _T(" --") << opt.longName; } if ( opt.kind != wxCMD_LINE_SWITCH ) @@ -807,7 +897,7 @@ void wxCmdLineParser::Usage() wxString val; val << _T('<') << GetTypeName(opt.type) << _T('>'); brief << _T(' ') << val; - detailed << (!opt.longName ? _T(':') : _T('=')) << val; + option << (!opt.longName ? _T(':') : _T('=')) << val; } if ( !(opt.flags & wxCMD_LINE_OPTION_MANDATORY) ) @@ -815,7 +905,8 @@ void wxCmdLineParser::Usage() brief << _T(']'); } - detailed << _T('\t') << opt.description << _T('\n'); + namesOptions.Add(option); + descOptions.Add(opt.description); } count = m_data->m_paramDesc.GetCount(); @@ -848,6 +939,27 @@ void wxCmdLineParser::Usage() } wxLogMessage(brief); + + // now construct the detailed help message + size_t len, lenMax = 0; + count = namesOptions.GetCount(); + for ( n = 0; n < count; n++ ) + { + len = namesOptions[n].length(); + if ( len > lenMax ) + lenMax = len; + } + + wxString detailed; + for ( n = 0; n < count; n++ ) + { + len = namesOptions[n].length(); + detailed << namesOptions[n] + << wxString(_T(' '), lenMax - len) << _T('\t') + << descOptions[n] + << _T('\n'); + } + wxLogMessage(detailed); } @@ -871,3 +983,5 @@ static wxString GetTypeName(wxCmdLineParamType type) return s; } + +#endif // wxUSE_CMDLINE_PARSER