]> git.saurik.com Git - wxWidgets.git/commitdiff
fixes error/usage messages given by the cmd line parsing logic in wxApp;
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 17 Dec 2001 12:21:03 +0000 (12:21 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 17 Dec 2001 12:21:03 +0000 (12:21 +0000)
also fixed the handling of long-only options in the usage messages

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13058 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
docs/latex/wx/cmdlpars.tex
include/wx/cmdline.h
src/common/appcmn.cpp
src/common/cmdline.cpp

index 988bb291ed67a8ba73b7df170314e9d53272a84f..f17c5346fa5a2e06ea0dfdcee96fbc45f8cab01d 100644 (file)
@@ -63,6 +63,10 @@ Unix ports:
 2.3.3
 -----
 
+All:
+
+- fixes to the command line parsing error and usage messages
+
 wxMSW:
 
 - fixed flicker in wxTreeCtrl::SetItemXXX()
index 8b3d0252c9412b2fcedf4ba4ae0ea1a929526ff3..d2a9373fd6b6947d0a8996536003e2f9bd7f5df4 100644 (file)
@@ -405,12 +405,20 @@ Add a parameter of the given {\it type} to the command line description.
 
 \membersection{wxCmdLineParser::Parse}\label{wxcmdlineparserparse}
 
-\func{int}{Parse}{\void}
+\func{int}{Parse}{\param{bool }{giveUsage = {\tt TRUE}}}
 
 Parse the command line, return $0$ if ok, $-1$ if {\tt "-h"} or {\tt "--help"} 
 option was encountered and the help message was given or a positive value if a
 syntax error occured.
 
+\wxheading{Parameters}
+
+\docparam{giveUsage}{If {\tt TRUE} (default), the usage message is given if a
+syntax error was encountered while parsing the command line or if help was
+requested. If {\tt FALSE}, only error messages about possible syntax errors
+are given, use \helpref{Usage}{wxcmdlineparserusage} to show the usage message
+from the caller if needed.}
+
 \membersection{wxCmdLineParser::Usage}\label{wxcmdlineparserusage}
 
 \func{void}{Usage}{\void}
index dccfc4068758b21f061ddd84b65697fab534b45d..498752cb476f781825aaa1d9d55c28ac540c38fd 100644 (file)
@@ -162,7 +162,10 @@ public:
     // parse the command line, return 0 if ok, -1 if "-h" or "--help" option
     // was encountered and the help message was given or a positive value if a
     // syntax error occured
-    int Parse();
+    //
+    // if showUsage is true, Usage() is called in case of syntax error or if
+    // help was requested
+    int Parse(bool showUsage = TRUE);
 
     // give the usage message describing all program options
     void Usage();
index ce42b0dd1fff6c405027dc07630e39fb3565114a..b1ab25eaf1f87e8d846f24aa3f390b0d8063167d 100644 (file)
@@ -166,7 +166,7 @@ bool wxAppBase::OnInit()
     OnInitCmdLine(parser);
 
     bool cont;
-    switch ( parser.Parse() )
+    switch ( parser.Parse(FALSE /* don't show usage */) )
     {
         case -1:
             cont = OnCmdLineHelp(parser);
@@ -287,7 +287,7 @@ bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
         if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) )
             return FALSE;
     }
-#endif
+#endif // __WXMGL__
 
     return TRUE;
 }
index efb4a3e9664c50ead898e5fdd547c3093c6c659d..2b550d15ec8ff82dac3bca6045f137aa16a07f90 100644 (file)
@@ -109,7 +109,9 @@ struct wxCmdLineOption
 
 public:
     wxCmdLineEntryType kind;
-    wxString shortName, longName, description;
+    wxString shortName,
+             longName,
+             description;
     wxCmdLineParamType type;
     int flags;
 
@@ -476,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
@@ -803,7 +805,7 @@ int wxCmdLineParser::Parse()
         }
     }
 
-    if ( !ok )
+    if ( !ok && showUsage )
     {
         Usage();
     }
@@ -849,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 )
@@ -904,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;
@@ -927,6 +952,11 @@ void wxCmdLineParser::Usage()
     }
 
     wxLogMessage(detailed);
+
+    // do it now if not done above
+#if wxUSE_GUI
+    wxLogMessage(brief);
+#endif // wxUSE_GUI
 }
 
 // ----------------------------------------------------------------------------