]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/cmdline.cpp
compilation fixes for !wxUSE_FONTMAP
[wxWidgets.git] / src / common / cmdline.cpp
index 46517df3a2700ad2668214ba7cd2d8d72580cde5..2b550d15ec8ff82dac3bca6045f137aa16a07f90 100644 (file)
@@ -28,6 +28,8 @@
     #pragma hdrstop
 #endif
 
+#include "wx/cmdline.h"
+
 #if wxUSE_CMDLINE_PARSER
 
 #ifndef WX_PRECOMP
@@ -42,7 +44,6 @@
 #include <ctype.h>
 
 #include "wx/datetime.h"
-#include "wx/cmdline.h"
 
 // ----------------------------------------------------------------------------
 // private functions
@@ -108,7 +109,9 @@ struct wxCmdLineOption
 
 public:
     wxCmdLineEntryType kind;
-    wxString shortName, longName, description;
+    wxString shortName,
+             longName,
+             description;
     wxCmdLineParamType type;
     int flags;
 
@@ -201,44 +204,9 @@ 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)
@@ -510,7 +478,7 @@ void wxCmdLineParser::Reset()
 // 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
@@ -837,7 +805,7 @@ int wxCmdLineParser::Parse()
         }
     }
 
-    if ( !ok )
+    if ( !ok && showUsage )
     {
         Usage();
     }
@@ -883,13 +851,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("  ") << chSwitch << opt.shortName;
+        }
+
+        if ( !opt.longName.empty() )
         {
-            option << _T("  --") << opt.longName;
+            option << (option.empty() ? _T("  ") : _T(", "))
+                   << _T("--") << opt.longName;
         }
 
         if ( opt.kind != wxCMD_LINE_SWITCH )
@@ -938,7 +923,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;
@@ -961,10 +952,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)
@@ -985,3 +981,105 @@ 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;
+}
+