]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/cmdline.cpp
Added chapter on collection and container classes to contents
[wxWidgets.git] / src / common / cmdline.cpp
index 6fc133bc4e760bb02b9308ab403df2ca0ea79133..965d1143deb36f346267b1b27deb3a67f82a6e49 100644 (file)
@@ -37,6 +37,8 @@
     #include "wx/filefn.h"
 #endif //WX_PRECOMP
 
     #include "wx/filefn.h"
 #endif //WX_PRECOMP
 
+#include <ctype.h>
+
 #include "wx/datetime.h"
 #include "wx/cmdline.h"
 
 #include "wx/datetime.h"
 #include "wx/cmdline.h"
 
@@ -77,7 +79,7 @@ struct wxCmdLineOption
     // types increases, so always use the accessor functions and don't access
     // the fields directly!)
 
     // types increases, so always use the accessor functions and don't access
     // the fields directly!)
 
-    void Check(wxCmdLineParamType typ) const
+    void Check(wxCmdLineParamType WXUNUSED_UNLESS_DEBUG(typ)) const
     {
         wxASSERT_MSG( type == typ, _T("type mismatch in wxCmdLineOption") );
     }
     {
         wxASSERT_MSG( type == typ, _T("type mismatch in wxCmdLineOption") );
     }
@@ -142,8 +144,8 @@ struct wxCmdLineParserData
 {
     // options
     wxString m_switchChars;     // characters which may start an option
 {
     // options
     wxString m_switchChars;     // characters which may start an option
-
     bool m_enableLongOptions;   // TRUE if long options are enabled
     bool m_enableLongOptions;   // TRUE if long options are enabled
+    wxString m_logo;            // some extra text to show in Usage()
 
     // cmd line data
     wxArrayString m_arguments;  // == argv, argc == m_arguments.GetCount()
 
     // cmd line data
     wxArrayString m_arguments;  // == argv, argc == m_arguments.GetCount()
@@ -188,7 +190,7 @@ void wxCmdLineParserData::SetArguments(int argc, char **argv)
     }
 }
 
     }
 }
 
-void wxCmdLineParserData::SetArguments(const wxString& cmdline)
+void wxCmdLineParserData::SetArguments(const wxString& WXUNUSED(cmdline))
 {
     // either use wxMSW wxApp::ConvertToStandardCommandArgs() or move its logic
     // here and use this method from it - but don't duplicate the code
 {
     // either use wxMSW wxApp::ConvertToStandardCommandArgs() or move its logic
     // here and use this method from it - but don't duplicate the code
@@ -264,6 +266,11 @@ void wxCmdLineParser::EnableLongOptions(bool enable)
     m_data->m_enableLongOptions = enable;
 }
 
     m_data->m_enableLongOptions = enable;
 }
 
+void wxCmdLineParser::SetLogo(const wxString& logo)
+{
+    m_data->m_logo = logo;
+}
+
 // ----------------------------------------------------------------------------
 // command line construction
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // command line construction
 // ----------------------------------------------------------------------------
@@ -275,7 +282,8 @@ void wxCmdLineParser::SetDesc(const wxCmdLineEntryDesc *desc)
         switch ( desc->kind )
         {
             case wxCMD_LINE_SWITCH:
         switch ( desc->kind )
         {
             case wxCMD_LINE_SWITCH:
-                AddSwitch(desc->shortName, desc->longName, desc->description);
+                AddSwitch(desc->shortName, desc->longName, desc->description,
+                          desc->flags);
                 break;
 
             case wxCMD_LINE_OPTION:
                 break;
 
             case wxCMD_LINE_OPTION:
@@ -340,14 +348,12 @@ void wxCmdLineParser::AddParam(const wxString& desc,
         wxCmdLineParam& param = m_data->m_paramDesc.Last();
 
         wxASSERT_MSG( !(param.flags & wxCMD_LINE_PARAM_MULTIPLE),
         wxCmdLineParam& param = m_data->m_paramDesc.Last();
 
         wxASSERT_MSG( !(param.flags & wxCMD_LINE_PARAM_MULTIPLE),
-                      _T("all parameters after the one with "
-                         "wxCMD_LINE_PARAM_MULTIPLE style will be ignored") );
+                      _T("all parameters after the one with wxCMD_LINE_PARAM_MULTIPLE style will be ignored") );
 
         if ( !(flags & wxCMD_LINE_PARAM_OPTIONAL) )
         {
             wxASSERT_MSG( !(param.flags & wxCMD_LINE_PARAM_OPTIONAL),
 
         if ( !(flags & wxCMD_LINE_PARAM_OPTIONAL) )
         {
             wxASSERT_MSG( !(param.flags & wxCMD_LINE_PARAM_OPTIONAL),
-                          _T("a required parameter can't follow an "
-                             "optional one") );
+                          _T("a required parameter can't follow an optional one") );
         }
     }
 #endif // Debug
         }
     }
 #endif // Debug
@@ -479,7 +485,7 @@ int wxCmdLineParser::Parse()
                 isLong = TRUE;
 
                 const wxChar *p = arg.c_str() + 2;
                 isLong = TRUE;
 
                 const wxChar *p = arg.c_str() + 2;
-                while ( wxIsalpha(*p) || (*p == _T('-')) )
+                while ( wxIsalnum(*p) || (*p == _T('_')) || (*p == _T('-')) )
                 {
                     name += *p++;
                 }
                 {
                     name += *p++;
                 }
@@ -497,7 +503,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;
                 // 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++;
                 }
                 {
                     name += *p++;
                 }
@@ -581,8 +587,7 @@ int wxCmdLineParser::Parse()
 
                     if ( *p++ != _T('=') )
                     {
 
                     if ( *p++ != _T('=') )
                     {
-                        wxLogError(_("Option '%s' requires a value, '=' "
-                                     "expected."), name.c_str());
+                        wxLogError(_("Option '%s' requires a value, '=' expected."), name.c_str());
 
                         ok = FALSE;
                     }
 
                         ok = FALSE;
                     }
@@ -591,6 +596,7 @@ int wxCmdLineParser::Parse()
                 {
                     switch ( *p )
                     {
                 {
                     switch ( *p )
                     {
+                        case _T('='):
                         case _T(':'):
                             // the value follows
                             p++;
                         case _T(':'):
                             // the value follows
                             p++;
@@ -614,8 +620,15 @@ int wxCmdLineParser::Parse()
                             break;
 
                         default:
                             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;
+                            }
                     }
                 }
 
                     }
                 }
 
@@ -641,9 +654,7 @@ int wxCmdLineParser::Parse()
                                 }
                                 else
                                 {
                                 }
                                 else
                                 {
-                                    wxLogError(_("'%s' is not a correct "
-                                                 "numeric value for option "
-                                                 "'%s'."),
+                                    wxLogError(_("'%s' is not a correct numeric value for option '%s'."),
                                                value.c_str(), name.c_str());
 
                                     ok = FALSE;
                                                value.c_str(), name.c_str());
 
                                     ok = FALSE;
@@ -657,8 +668,7 @@ int wxCmdLineParser::Parse()
                                 const wxChar *res = dt.ParseDate(value);
                                 if ( !res || *res )
                                 {
                                 const wxChar *res = dt.ParseDate(value);
                                 if ( !res || *res )
                                 {
-                                    wxLogError(_("Option '%s': '%s' cannot "
-                                                  "be converted to a date."),
+                                    wxLogError(_("Option '%s': '%s' cannot be converted to a date."),
                                                name.c_str(), value.c_str());
 
                                     ok = FALSE;
                                                name.c_str(), value.c_str());
 
                                     ok = FALSE;
@@ -691,9 +701,7 @@ int wxCmdLineParser::Parse()
                 else
                 {
                     wxASSERT_MSG( currentParam == countParam - 1,
                 else
                 {
                     wxASSERT_MSG( currentParam == countParam - 1,
-                                  _T("all parameters after the one with "
-                                     "wxCMD_LINE_PARAM_MULTIPLE style "
-                                     "are ignored") );
+                                  _T("all parameters after the one with wxCMD_LINE_PARAM_MULTIPLE style are ignored") );
 
                     // remember that we did have this last repeatable parameter
                     hadRepeatableParam = TRUE;
 
                     // remember that we did have this last repeatable parameter
                     hadRepeatableParam = TRUE;
@@ -781,9 +789,18 @@ void wxCmdLineParser::Usage()
         wxStripExtension(appname);
     }
 
         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());
 
     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++ )
     {
     size_t n, count = m_data->m_options.GetCount();
     for ( n = 0; n < count; n++ )
     {
@@ -795,11 +812,13 @@ void wxCmdLineParser::Usage()
             brief << _T('[');
         }
 
             brief << _T('[');
         }
 
-        brief << _T('-') << opt.shortName;
-        detailed << _T("  -") << opt.shortName;
+        brief << chSwitch << opt.shortName;
+
+        wxString option;
+        option << _T("  ") << chSwitch << opt.shortName;
         if ( !!opt.longName )
         {
         if ( !!opt.longName )
         {
-            detailed << _T("  --") << opt.longName;
+            option << _T("  --") << opt.longName;
         }
 
         if ( opt.kind != wxCMD_LINE_SWITCH )
         }
 
         if ( opt.kind != wxCMD_LINE_SWITCH )
@@ -807,7 +826,7 @@ void wxCmdLineParser::Usage()
             wxString val;
             val << _T('<') << GetTypeName(opt.type) << _T('>');
             brief << _T(' ') << val;
             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) )
         }
 
         if ( !(opt.flags & wxCMD_LINE_OPTION_MANDATORY) )
@@ -815,7 +834,8 @@ void wxCmdLineParser::Usage()
             brief << _T(']');
         }
 
             brief << _T(']');
         }
 
-        detailed << _T('\t') << opt.description << _T('\n');
+        namesOptions.Add(option);
+        descOptions.Add(opt.description);
     }
 
     count = m_data->m_paramDesc.GetCount();
     }
 
     count = m_data->m_paramDesc.GetCount();
@@ -842,7 +862,33 @@ void wxCmdLineParser::Usage()
         }
     }
 
         }
     }
 
+    if ( !!m_data->m_logo )
+    {
+        wxLogMessage(m_data->m_logo);
+    }
+
     wxLogMessage(brief);
     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);
 }
 
     wxLogMessage(detailed);
 }