]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/cmdline.cpp
check the week day for consistency in ParseFormat() (replaces patch 788052)
[wxWidgets.git] / src / common / cmdline.cpp
index ec1d43bdb48a59faad5c42acee193a7d5360ef1d..dc3a857e661c75281c7ee15dc3b22241cbccd5d3 100644 (file)
@@ -6,7 +6,7 @@
 // Created:     05.01.00
 // RCS-ID:      $Id$
 // Copyright:   (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
-// Licence:     wxWindows license
+// Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
 
 // ============================================================================
@@ -17,7 +17,7 @@
 // headers
 // ----------------------------------------------------------------------------
 
-#ifdef __GNUG__
+#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
     #pragma implementation "cmdline.h"
 #endif
 
@@ -86,6 +86,7 @@ struct wxCmdLineOption
                 GetLongOptionName(lng).Len() == lng.Len(),
                 wxT("Long option contains invalid characters")
             );
+            
 
         kind = k;
 
@@ -113,15 +114,19 @@ struct wxCmdLineOption
         { Check(wxCMD_LINE_VAL_NUMBER); return m_longVal; }
     const wxString& GetStrVal() const
         { Check(wxCMD_LINE_VAL_STRING); return m_strVal;  }
+#if wxUSE_DATETIME
     const wxDateTime& GetDateVal() const
         { Check(wxCMD_LINE_VAL_DATE);   return m_dateVal; }
+#endif // wxUSE_DATETIME
 
     void SetLongVal(long val)
         { Check(wxCMD_LINE_VAL_NUMBER); m_longVal = val; m_hasVal = TRUE; }
     void SetStrVal(const wxString& val)
         { Check(wxCMD_LINE_VAL_STRING); m_strVal = val; m_hasVal = TRUE; }
+#if wxUSE_DATETIME
     void SetDateVal(const wxDateTime val)
         { Check(wxCMD_LINE_VAL_DATE); m_dateVal = val; m_hasVal = TRUE; }
+#endif // wxUSE_DATETIME
 
     void SetHasValue(bool hasValue = TRUE) { m_hasVal = hasValue; }
     bool HasValue() const { return m_hasVal; }
@@ -139,7 +144,9 @@ private:
 
     long m_longVal;
     wxString m_strVal;
+#if wxUSE_DATETIME
     wxDateTime m_dateVal;
+#endif // wxUSE_DATETIME
 };
 
 struct wxCmdLineParam
@@ -209,19 +216,19 @@ wxCmdLineParserData::wxCmdLineParserData()
 
 void wxCmdLineParserData::SetArguments(int argc, wxChar **argv)
 {
-    m_arguments.Empty();
+    m_arguments.clear();
 
     for ( int n = 0; n < argc; n++ )
     {
-        m_arguments.Add(argv[n]);
+        m_arguments.push_back(argv[n]);
     }
 }
 
 void wxCmdLineParserData::SetArguments(const wxString& cmdLine)
 {
-    m_arguments.Empty();
+    m_arguments.clear();
 
-    m_arguments.Add(wxTheApp->GetAppName());
+    m_arguments.push_back(wxTheApp->GetAppName());
 
     wxArrayString args = wxCmdLineParser::ConvertStringToArgs(cmdLine);
 
@@ -458,6 +465,7 @@ bool wxCmdLineParser::Found(const wxString& name, long *value) const
     return TRUE;
 }
 
+#if wxUSE_DATETIME
 bool wxCmdLineParser::Found(const wxString& name, wxDateTime *value) const
 {
     int i = m_data->FindOption(name);
@@ -476,10 +484,11 @@ bool wxCmdLineParser::Found(const wxString& name, wxDateTime *value) const
 
     return TRUE;
 }
+#endif // wxUSE_DATETIME
 
 size_t wxCmdLineParser::GetParamCount() const
 {
-    return m_data->m_parameters.GetCount();
+    return m_data->m_parameters.size();
 }
 
 wxString wxCmdLineParser::GetParam(size_t n) const
@@ -520,7 +529,7 @@ int wxCmdLineParser::Parse(bool showUsage)
 
     // parse everything
     wxString arg;
-    size_t count = m_data->m_arguments.GetCount();
+    size_t count = m_data->m_arguments.size();
     for ( size_t n = 1; ok && (n < count); n++ )    // 0 is program name
     {
         arg = m_data->m_arguments[n];
@@ -561,7 +570,7 @@ int wxCmdLineParser::Parse(bool showUsage)
                     optInd = m_data->FindOptionByLongName(name);
                     if ( optInd == wxNOT_FOUND )
                     {
-                        errorMsg << wxString::Format(_("Unknown long option '%s'"), name.c_str()) << "\n";
+                        errorMsg << wxString::Format(_("Unknown long option '%s'"), name.c_str()) << wxT("\n");
                     }
                 }
                 else
@@ -570,7 +579,7 @@ int wxCmdLineParser::Parse(bool showUsage)
 
                     // Print the argument including leading "--"
                     name.Prepend( wxT("--") );
-                    errorMsg << wxString::Format(_("Unknown option '%s'"), name.c_str()) << "\n";
+                    errorMsg << wxString::Format(_("Unknown option '%s'"), name.c_str()) << wxT("\n");
                 }
 
             }
@@ -591,7 +600,7 @@ int wxCmdLineParser::Parse(bool showUsage)
                     {
                         // we couldn't find a valid option name in the
                         // beginning of this string
-                        errorMsg << wxString::Format(_("Unknown option '%s'"), name.c_str()) << "\n";
+                        errorMsg << wxString::Format(_("Unknown option '%s'"), name.c_str()) << wxT("\n");
 
                         break;
                     }
@@ -623,7 +632,8 @@ int wxCmdLineParser::Parse(bool showUsage)
                         wxString arg2 = arg[0u];
                         arg2 += arg.Mid(len + 1); // +1 for leading '-'
 
-                        m_data->m_arguments.Insert(arg2, n + 1);
+                        m_data->m_arguments.insert
+                            (m_data->m_arguments.begin() + n + 1, arg2);
                         count++;
                     }
                     //else: it's our value, we'll deal with it below
@@ -663,7 +673,7 @@ int wxCmdLineParser::Parse(bool showUsage)
 
                     if ( *p++ != _T('=') )
                     {
-                        errorMsg << wxString::Format(_("Option '%s' requires a value, '=' expected."), name.c_str()) << "\n";
+                        errorMsg << wxString::Format(_("Option '%s' requires a value, '=' expected."), name.c_str()) << wxT("\n");
 
                         ok = FALSE;
                     }
@@ -684,7 +694,7 @@ int wxCmdLineParser::Parse(bool showUsage)
                             {
                                 // ... but there is none
                                 errorMsg << wxString::Format(_("Option '%s' requires a value."),
-                                                             name.c_str()) << "\n";
+                                                             name.c_str()) << wxT("\n");
 
                                 ok = FALSE;
                             }
@@ -701,7 +711,7 @@ int wxCmdLineParser::Parse(bool showUsage)
                             if ( opt.flags & wxCMD_LINE_NEEDS_SEPARATOR )
                             {
                                 errorMsg << wxString::Format(_("Separator expected after the option '%s'."),
-                                                             name.c_str()) << "\n";
+                                                             name.c_str()) << wxT("\n");
 
                                 ok = FALSE;
                             }
@@ -731,13 +741,14 @@ int wxCmdLineParser::Parse(bool showUsage)
                                 else
                                 {
                                     errorMsg << wxString::Format(_("'%s' is not a correct numeric value for option '%s'."),
-                                                                 value.c_str(), name.c_str()) << "\n";
+                                                                 value.c_str(), name.c_str()) << wxT("\n");
 
                                     ok = FALSE;
                                 }
                             }
                             break;
 
+#if wxUSE_DATETIME
                         case wxCMD_LINE_VAL_DATE:
                             {
                                 wxDateTime dt;
@@ -745,7 +756,7 @@ int wxCmdLineParser::Parse(bool showUsage)
                                 if ( !res || *res )
                                 {
                                     errorMsg << wxString::Format(_("Option '%s': '%s' cannot be converted to a date."),
-                                                                 name.c_str(), value.c_str()) << "\n";
+                                                                 name.c_str(), value.c_str()) << wxT("\n");
 
                                     ok = FALSE;
                                 }
@@ -755,6 +766,7 @@ int wxCmdLineParser::Parse(bool showUsage)
                                 }
                             }
                             break;
+#endif // wxUSE_DATETIME
                     }
                 }
             }
@@ -768,7 +780,7 @@ int wxCmdLineParser::Parse(bool showUsage)
 
                 // TODO check the param type
 
-                m_data->m_parameters.Add(arg);
+                m_data->m_parameters.push_back(arg);
 
                 if ( !(param.flags & wxCMD_LINE_PARAM_MULTIPLE) )
                 {
@@ -785,7 +797,7 @@ int wxCmdLineParser::Parse(bool showUsage)
             }
             else
             {
-                errorMsg << wxString::Format(_("Unexpected parameter '%s'"), arg.c_str()) << "\n";
+                errorMsg << wxString::Format(_("Unexpected parameter '%s'"), arg.c_str()) << wxT("\n");
 
                 ok = FALSE;
             }
@@ -808,13 +820,21 @@ int wxCmdLineParser::Parse(bool showUsage)
                 }
                 else
                 {
-                    optName.Printf(_("%s (or %s)"),
-                                   opt.shortName.c_str(),
-                                   opt.longName.c_str());
+                    if ( AreLongOptionsEnabled() )
+                    {
+                        optName.Printf( _("%s (or %s)"),
+                                        opt.shortName.c_str(),
+                                        opt.longName.c_str() );
+                    }
+                    else
+                    {
+                        optName.Printf( wxT("%s"),
+                                        opt.shortName.c_str() );
+                    }
                 }
 
                 errorMsg << wxString::Format(_("The value for the option '%s' must be specified."),
-                                             optName.c_str()) << "\n";
+                                             optName.c_str()) << wxT("\n");
 
                 ok = FALSE;
             }
@@ -835,21 +855,30 @@ int wxCmdLineParser::Parse(bool showUsage)
             if ( !(param.flags & wxCMD_LINE_PARAM_OPTIONAL) )
             {
                 errorMsg << wxString::Format(_("The required parameter '%s' was not specified."),
-                                             param.description.c_str()) << "\n";
+                                             param.description.c_str()) << wxT("\n");
 
                 ok = FALSE;
             }
         }
     }
 
-    if ( !ok && (errorMsg.length() != 0 || helpRequested) )
+    // if there was an error during parsing the command line, show this error
+    // and also the usage message if it had been requested
+    if ( !ok && (!errorMsg.empty() || (helpRequested && showUsage)) )
     {
-        wxString usage;
         wxMessageOutput* msgOut = wxMessageOutput::Get();
-        wxASSERT(msgOut);
-        if ( showUsage ) usage = GetUsageString();
         if ( msgOut )
+        {
+            wxString usage;
+            if ( showUsage )
+                usage = GetUsageString();
+
             msgOut->Printf( wxT("%s%s"), usage.c_str(), errorMsg.c_str() );
+        }
+        else
+        {
+            wxFAIL_MSG( _T("no wxMessageOutput object?") );
+        }
     }
 
     return ok ? 0 : helpRequested ? -1 : 1;
@@ -861,10 +890,15 @@ int wxCmdLineParser::Parse(bool showUsage)
 
 void wxCmdLineParser::Usage()
 {
-    wxString usage = GetUsageString();
     wxMessageOutput* msgOut = wxMessageOutput::Get();
     if ( msgOut )
-        msgOut->Printf( wxT("%s"), usage.c_str() );
+    {
+        msgOut->Printf( wxT("%s"), GetUsageString().c_str() );
+    }
+    else
+    {
+        wxFAIL_MSG( _T("no wxMessageOutput object?") );
+    }
 }
 
 wxString wxCmdLineParser::GetUsageString()
@@ -872,7 +906,7 @@ wxString wxCmdLineParser::GetUsageString()
     wxString appname = wxTheApp->GetAppName();
     if ( !appname )
     {
-        wxCHECK_MSG( !m_data->m_arguments.IsEmpty(), wxEmptyString,
+        wxCHECK_MSG( m_data->m_arguments.size() != 0, wxEmptyString,
                      _T("no program name") );
 
         appname = wxFileNameFromPath(m_data->m_arguments[0]);
@@ -885,7 +919,7 @@ wxString wxCmdLineParser::GetUsageString()
     wxString usage;
     wxArrayString namesOptions, descOptions;
 
-    if ( !!m_data->m_logo )
+    if ( !m_data->m_logo.empty() )
     {
         usage << m_data->m_logo << _T('\n');
     }
@@ -956,8 +990,8 @@ wxString wxCmdLineParser::GetUsageString()
             usage << _T(']');
         }
 
-        namesOptions.Add(option);
-        descOptions.Add(opt.description);
+        namesOptions.push_back(option);
+        descOptions.push_back(opt.description);
     }
 
     count = m_data->m_paramDesc.GetCount();
@@ -988,7 +1022,7 @@ wxString wxCmdLineParser::GetUsageString()
 
     // now construct the detailed help message
     size_t len, lenMax = 0;
-    count = namesOptions.GetCount();
+    count = namesOptions.size();
     for ( n = 0; n < count; n++ )
     {
         len = namesOptions[n].length();
@@ -1021,9 +1055,17 @@ static wxString GetTypeName(wxCmdLineParamType type)
             wxFAIL_MSG( _T("unknown option type") );
             // still fall through
 
-        case wxCMD_LINE_VAL_STRING: s = _("str"); break;
-        case wxCMD_LINE_VAL_NUMBER: s = _("num"); break;
-        case wxCMD_LINE_VAL_DATE:   s = _("date"); break;
+        case wxCMD_LINE_VAL_STRING:
+            s = _("str");
+            break;
+
+        case wxCMD_LINE_VAL_NUMBER:
+            s = _("num");
+            break;
+
+        case wxCMD_LINE_VAL_DATE:
+            s = _("date");
+            break;
     }
 
     return s;
@@ -1086,7 +1128,7 @@ static wxString GetLongOptionName(const wxChar *p)
 
    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"
 
@@ -1195,7 +1237,7 @@ wxArrayString wxCmdLineParser::ConvertStringToArgs(const wxChar *p)
             }
         }
 
-        args.Add(arg);
+        args.push_back(arg);
     }
 
     return args;