1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/cmdline.cpp
3 // Purpose: wxCmdLineParser implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "cmdline.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #include "wx/cmdline.h"
33 #if wxUSE_CMDLINE_PARSER
36 #include "wx/string.h"
40 #include "wx/dynarray.h"
41 #include "wx/filefn.h"
46 #include "wx/datetime.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 static wxString
GetTypeName(wxCmdLineParamType type
);
54 static wxString
GetOptionName(const wxChar
*p
, const wxChar
*allowedChars
);
56 static wxString
GetShortOptionName(const wxChar
*p
);
58 static wxString
GetLongOptionName(const wxChar
*p
);
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // an internal representation of an option
65 struct wxCmdLineOption
67 wxCmdLineOption(wxCmdLineEntryType k
,
71 wxCmdLineParamType typ
,
74 wxASSERT_MSG( !shrt
.empty() || !lng
.empty(),
75 _T("option should have at least one name") );
79 GetShortOptionName(shrt
).Len() == shrt
.Len(),
80 wxT("Short option contains invalid characters")
85 GetLongOptionName(lng
).Len() == lng
.Len(),
86 wxT("Long option contains invalid characters")
101 // can't use union easily here, so just store all possible data fields, we
102 // don't waste much (might still use union later if the number of supported
103 // types increases, so always use the accessor functions and don't access
104 // the fields directly!)
106 void Check(wxCmdLineParamType
WXUNUSED_UNLESS_DEBUG(typ
)) const
108 wxASSERT_MSG( type
== typ
, _T("type mismatch in wxCmdLineOption") );
111 long GetLongVal() const
112 { Check(wxCMD_LINE_VAL_NUMBER
); return m_longVal
; }
113 const wxString
& GetStrVal() const
114 { Check(wxCMD_LINE_VAL_STRING
); return m_strVal
; }
115 const wxDateTime
& GetDateVal() const
116 { Check(wxCMD_LINE_VAL_DATE
); return m_dateVal
; }
118 void SetLongVal(long val
)
119 { Check(wxCMD_LINE_VAL_NUMBER
); m_longVal
= val
; m_hasVal
= TRUE
; }
120 void SetStrVal(const wxString
& val
)
121 { Check(wxCMD_LINE_VAL_STRING
); m_strVal
= val
; m_hasVal
= TRUE
; }
122 void SetDateVal(const wxDateTime val
)
123 { Check(wxCMD_LINE_VAL_DATE
); m_dateVal
= val
; m_hasVal
= TRUE
; }
125 void SetHasValue(bool hasValue
= TRUE
) { m_hasVal
= hasValue
; }
126 bool HasValue() const { return m_hasVal
; }
129 wxCmdLineEntryType kind
;
133 wxCmdLineParamType type
;
141 wxDateTime m_dateVal
;
144 struct wxCmdLineParam
146 wxCmdLineParam(const wxString
& desc
,
147 wxCmdLineParamType typ
,
155 wxString description
;
156 wxCmdLineParamType type
;
160 WX_DECLARE_OBJARRAY(wxCmdLineOption
, wxArrayOptions
);
161 WX_DECLARE_OBJARRAY(wxCmdLineParam
, wxArrayParams
);
163 #include "wx/arrimpl.cpp"
165 WX_DEFINE_OBJARRAY(wxArrayOptions
);
166 WX_DEFINE_OBJARRAY(wxArrayParams
);
168 // the parser internal state
169 struct wxCmdLineParserData
172 wxString m_switchChars
; // characters which may start an option
173 bool m_enableLongOptions
; // TRUE if long options are enabled
174 wxString m_logo
; // some extra text to show in Usage()
177 wxArrayString m_arguments
; // == argv, argc == m_arguments.GetCount()
178 wxArrayOptions m_options
; // all possible options and switchrs
179 wxArrayParams m_paramDesc
; // description of all possible params
180 wxArrayString m_parameters
; // all params found
183 wxCmdLineParserData();
184 void SetArguments(int argc
, wxChar
**argv
);
185 void SetArguments(const wxString
& cmdline
);
187 int FindOption(const wxString
& name
);
188 int FindOptionByLongName(const wxString
& name
);
191 // ============================================================================
193 // ============================================================================
195 // ----------------------------------------------------------------------------
196 // wxCmdLineParserData
197 // ----------------------------------------------------------------------------
199 wxCmdLineParserData::wxCmdLineParserData()
201 m_enableLongOptions
= TRUE
;
203 m_switchChars
= _T("-");
205 m_switchChars
= _T("/-");
209 void wxCmdLineParserData::SetArguments(int argc
, wxChar
**argv
)
213 for ( int n
= 0; n
< argc
; n
++ )
215 m_arguments
.Add(argv
[n
]);
219 void wxCmdLineParserData::SetArguments(const wxString
& cmdLine
)
223 m_arguments
.Add(wxTheApp
->GetAppName());
225 wxArrayString args
= wxCmdLineParser::ConvertStringToArgs(cmdLine
);
227 WX_APPEND_ARRAY(m_arguments
, args
);
230 int wxCmdLineParserData::FindOption(const wxString
& name
)
234 size_t count
= m_options
.GetCount();
235 for ( size_t n
= 0; n
< count
; n
++ )
237 if ( m_options
[n
].shortName
== name
)
248 int wxCmdLineParserData::FindOptionByLongName(const wxString
& name
)
250 size_t count
= m_options
.GetCount();
251 for ( size_t n
= 0; n
< count
; n
++ )
253 if ( m_options
[n
].longName
== name
)
263 // ----------------------------------------------------------------------------
264 // construction and destruction
265 // ----------------------------------------------------------------------------
267 void wxCmdLineParser::Init()
269 m_data
= new wxCmdLineParserData
;
272 void wxCmdLineParser::SetCmdLine(int argc
, wxChar
**argv
)
274 m_data
->SetArguments(argc
, argv
);
277 void wxCmdLineParser::SetCmdLine(const wxString
& cmdline
)
279 m_data
->SetArguments(cmdline
);
282 wxCmdLineParser::~wxCmdLineParser()
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
291 void wxCmdLineParser::SetSwitchChars(const wxString
& switchChars
)
293 m_data
->m_switchChars
= switchChars
;
296 void wxCmdLineParser::EnableLongOptions(bool enable
)
298 m_data
->m_enableLongOptions
= enable
;
301 bool wxCmdLineParser::AreLongOptionsEnabled()
303 return m_data
->m_enableLongOptions
;
306 void wxCmdLineParser::SetLogo(const wxString
& logo
)
308 m_data
->m_logo
= logo
;
311 // ----------------------------------------------------------------------------
312 // command line construction
313 // ----------------------------------------------------------------------------
315 void wxCmdLineParser::SetDesc(const wxCmdLineEntryDesc
*desc
)
319 switch ( desc
->kind
)
321 case wxCMD_LINE_SWITCH
:
322 AddSwitch(desc
->shortName
, desc
->longName
, desc
->description
,
326 case wxCMD_LINE_OPTION
:
327 AddOption(desc
->shortName
, desc
->longName
, desc
->description
,
328 desc
->type
, desc
->flags
);
331 case wxCMD_LINE_PARAM
:
332 AddParam(desc
->description
, desc
->type
, desc
->flags
);
336 wxFAIL_MSG( _T("unknown command line entry type") );
337 // still fall through
339 case wxCMD_LINE_NONE
:
345 void wxCmdLineParser::AddSwitch(const wxString
& shortName
,
346 const wxString
& longName
,
347 const wxString
& desc
,
350 wxASSERT_MSG( m_data
->FindOption(shortName
) == wxNOT_FOUND
,
351 _T("duplicate switch") );
353 wxCmdLineOption
*option
= new wxCmdLineOption(wxCMD_LINE_SWITCH
,
354 shortName
, longName
, desc
,
355 wxCMD_LINE_VAL_NONE
, flags
);
357 m_data
->m_options
.Add(option
);
360 void wxCmdLineParser::AddOption(const wxString
& shortName
,
361 const wxString
& longName
,
362 const wxString
& desc
,
363 wxCmdLineParamType type
,
366 wxASSERT_MSG( m_data
->FindOption(shortName
) == wxNOT_FOUND
,
367 _T("duplicate option") );
369 wxCmdLineOption
*option
= new wxCmdLineOption(wxCMD_LINE_OPTION
,
370 shortName
, longName
, desc
,
373 m_data
->m_options
.Add(option
);
376 void wxCmdLineParser::AddParam(const wxString
& desc
,
377 wxCmdLineParamType type
,
380 // do some consistency checks: a required parameter can't follow an
381 // optional one and nothing should follow a parameter with MULTIPLE flag
383 if ( !m_data
->m_paramDesc
.IsEmpty() )
385 wxCmdLineParam
& param
= m_data
->m_paramDesc
.Last();
387 wxASSERT_MSG( !(param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
),
388 _T("all parameters after the one with wxCMD_LINE_PARAM_MULTIPLE style will be ignored") );
390 if ( !(flags
& wxCMD_LINE_PARAM_OPTIONAL
) )
392 wxASSERT_MSG( !(param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
),
393 _T("a required parameter can't follow an optional one") );
398 wxCmdLineParam
*param
= new wxCmdLineParam(desc
, type
, flags
);
400 m_data
->m_paramDesc
.Add(param
);
403 // ----------------------------------------------------------------------------
404 // access to parse command line
405 // ----------------------------------------------------------------------------
407 bool wxCmdLineParser::Found(const wxString
& name
) const
409 int i
= m_data
->FindOption(name
);
410 if ( i
== wxNOT_FOUND
)
411 i
= m_data
->FindOptionByLongName(name
);
413 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown switch") );
415 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
416 if ( !opt
.HasValue() )
422 bool wxCmdLineParser::Found(const wxString
& name
, wxString
*value
) const
424 int i
= m_data
->FindOption(name
);
425 if ( i
== wxNOT_FOUND
)
426 i
= m_data
->FindOptionByLongName(name
);
428 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
430 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
431 if ( !opt
.HasValue() )
434 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
436 *value
= opt
.GetStrVal();
441 bool wxCmdLineParser::Found(const wxString
& name
, long *value
) const
443 int i
= m_data
->FindOption(name
);
444 if ( i
== wxNOT_FOUND
)
445 i
= m_data
->FindOptionByLongName(name
);
447 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
449 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
450 if ( !opt
.HasValue() )
453 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
455 *value
= opt
.GetLongVal();
460 bool wxCmdLineParser::Found(const wxString
& name
, wxDateTime
*value
) const
462 int i
= m_data
->FindOption(name
);
463 if ( i
== wxNOT_FOUND
)
464 i
= m_data
->FindOptionByLongName(name
);
466 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
468 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
469 if ( !opt
.HasValue() )
472 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
474 *value
= opt
.GetDateVal();
479 size_t wxCmdLineParser::GetParamCount() const
481 return m_data
->m_parameters
.GetCount();
484 wxString
wxCmdLineParser::GetParam(size_t n
) const
486 wxCHECK_MSG( n
< GetParamCount(), wxEmptyString
, _T("invalid param index") );
488 return m_data
->m_parameters
[n
];
491 // Resets switches and options
492 void wxCmdLineParser::Reset()
494 for ( size_t i
= 0; i
< m_data
->m_options
.Count(); i
++ )
496 wxCmdLineOption
& opt
= m_data
->m_options
[i
];
497 opt
.SetHasValue(FALSE
);
502 // ----------------------------------------------------------------------------
503 // the real work is done here
504 // ----------------------------------------------------------------------------
506 int wxCmdLineParser::Parse(bool showUsage
)
508 bool maybeOption
= TRUE
; // can the following arg be an option?
509 bool ok
= TRUE
; // TRUE until an error is detected
510 bool helpRequested
= FALSE
; // TRUE if "-h" was given
511 bool hadRepeatableParam
= FALSE
; // TRUE if found param with MULTIPLE flag
513 size_t currentParam
= 0; // the index in m_paramDesc
515 size_t countParam
= m_data
->m_paramDesc
.GetCount();
521 size_t count
= m_data
->m_arguments
.GetCount();
522 for ( size_t n
= 1; ok
&& (n
< count
); n
++ ) // 0 is program name
524 arg
= m_data
->m_arguments
[n
];
526 // special case: "--" should be discarded and all following arguments
527 // should be considered as parameters, even if they start with '-' and
528 // not like options (this is POSIX-like)
529 if ( arg
== _T("--") )
536 // empty argument or just '-' is not an option but a parameter
537 if ( maybeOption
&& arg
.length() > 1 &&
538 wxStrchr(m_data
->m_switchChars
, arg
[0u]) )
542 int optInd
= wxNOT_FOUND
; // init to suppress warnings
544 // an option or a switch: find whether it's a long or a short one
545 if ( arg
[0u] == _T('-') && arg
[1u] == _T('-') )
551 const wxChar
*p
= arg
.c_str() + 2;
553 bool longOptionsEnabled
= AreLongOptionsEnabled();
555 name
= GetLongOptionName(p
);
557 if (longOptionsEnabled
)
559 optInd
= m_data
->FindOptionByLongName(name
);
560 if ( optInd
== wxNOT_FOUND
)
562 wxLogError(_("Unknown long option '%s'"), name
.c_str());
567 optInd
= wxNOT_FOUND
; // Sanity check
569 // Print the argument including leading "--"
570 name
.Prepend( wxT("--") );
571 wxLogError(_("Unknown option '%s'"), name
.c_str());
579 // a short one: as they can be cumulated, we try to find the
580 // longest substring which is a valid option
581 const wxChar
*p
= arg
.c_str() + 1;
583 name
= GetShortOptionName(p
);
585 size_t len
= name
.length();
590 // we couldn't find a valid option name in the
591 // beginning of this string
592 wxLogError(_("Unknown option '%s'"), name
.c_str());
598 optInd
= m_data
->FindOption(name
.Left(len
));
600 // will try with one character less the next time
604 while ( optInd
== wxNOT_FOUND
);
606 len
++; // compensates extra len-- above
607 if ( (optInd
!= wxNOT_FOUND
) && (len
!= name
.length()) )
609 // first of all, the option name is only part of this
611 name
= name
.Left(len
);
613 // our option is only part of this argument, there is
614 // something else in it - it is either the value of this
615 // option or other switches if it is a switch
616 if ( m_data
->m_options
[(size_t)optInd
].kind
617 == wxCMD_LINE_SWITCH
)
619 // pretend that all the rest of the argument is the
620 // next argument, in fact
621 wxString arg2
= arg
[0u];
622 arg2
+= arg
.Mid(len
+ 1); // +1 for leading '-'
624 m_data
->m_arguments
.Insert(arg2
, n
+ 1);
627 //else: it's our value, we'll deal with it below
631 if ( optInd
== wxNOT_FOUND
)
635 continue; // will break, in fact
638 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)optInd
];
639 if ( opt
.kind
== wxCMD_LINE_SWITCH
)
641 // nothing more to do
644 if ( opt
.flags
& wxCMD_LINE_OPTION_HELP
)
646 helpRequested
= TRUE
;
648 // it's not an error, but we still stop here
656 // +1 for leading '-'
657 const wxChar
*p
= arg
.c_str() + 1 + name
.length();
660 p
++; // for another leading '-'
662 if ( *p
++ != _T('=') )
664 wxLogError(_("Option '%s' requires a value, '=' expected."), name
.c_str());
680 // the value is in the next argument
683 // ... but there is none
684 wxLogError(_("Option '%s' requires a value."),
691 // ... take it from there
692 p
= m_data
->m_arguments
[n
].c_str();
697 // the value is right here: this may be legal or
698 // not depending on the option style
699 if ( opt
.flags
& wxCMD_LINE_NEEDS_SEPARATOR
)
701 wxLogError(_("Separator expected after the option '%s'."),
715 wxFAIL_MSG( _T("unknown option type") );
716 // still fall through
718 case wxCMD_LINE_VAL_STRING
:
719 opt
.SetStrVal(value
);
722 case wxCMD_LINE_VAL_NUMBER
:
725 if ( value
.ToLong(&val
) )
731 wxLogError(_("'%s' is not a correct numeric value for option '%s'."),
732 value
.c_str(), name
.c_str());
739 case wxCMD_LINE_VAL_DATE
:
742 const wxChar
*res
= dt
.ParseDate(value
);
745 wxLogError(_("Option '%s': '%s' cannot be converted to a date."),
746 name
.c_str(), value
.c_str());
763 if ( currentParam
< countParam
)
765 wxCmdLineParam
& param
= m_data
->m_paramDesc
[currentParam
];
767 // TODO check the param type
769 m_data
->m_parameters
.Add(arg
);
771 if ( !(param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
) )
777 wxASSERT_MSG( currentParam
== countParam
- 1,
778 _T("all parameters after the one with wxCMD_LINE_PARAM_MULTIPLE style are ignored") );
780 // remember that we did have this last repeatable parameter
781 hadRepeatableParam
= TRUE
;
786 wxLogError(_("Unexpected parameter '%s'"), arg
.c_str());
793 // verify that all mandatory options were given
796 size_t countOpt
= m_data
->m_options
.GetCount();
797 for ( size_t n
= 0; ok
&& (n
< countOpt
); n
++ )
799 wxCmdLineOption
& opt
= m_data
->m_options
[n
];
800 if ( (opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) && !opt
.HasValue() )
805 optName
= opt
.shortName
;
809 optName
.Printf(_("%s (or %s)"),
810 opt
.shortName
.c_str(),
811 opt
.longName
.c_str());
814 wxLogError(_("The value for the option '%s' must be specified."),
821 for ( ; ok
&& (currentParam
< countParam
); currentParam
++ )
823 wxCmdLineParam
& param
= m_data
->m_paramDesc
[currentParam
];
824 if ( (currentParam
== countParam
- 1) &&
825 (param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
) &&
828 // special case: currentParam wasn't incremented, but we did
829 // have it, so don't give error
833 if ( !(param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
) )
835 wxLogError(_("The required parameter '%s' was not specified."),
836 param
.description
.c_str());
843 if ( !ok
&& showUsage
)
848 return ok
? 0 : helpRequested
? -1 : 1;
851 // ----------------------------------------------------------------------------
852 // give the usage message
853 // ----------------------------------------------------------------------------
855 void wxCmdLineParser::Usage()
857 wxString appname
= wxTheApp
->GetAppName();
860 wxCHECK_RET( !m_data
->m_arguments
.IsEmpty(), _T("no program name") );
862 appname
= wxFileNameFromPath(m_data
->m_arguments
[0]);
863 wxStripExtension(appname
);
866 // we construct the brief cmd line desc on the fly, but not the detailed
867 // help message below because we want to align the options descriptions
868 // and for this we must first know the longest one of them
870 wxArrayString namesOptions
, descOptions
;
871 brief
.Printf(_("Usage: %s"), appname
.c_str());
873 // the switch char is usually '-' but this can be changed with
874 // SetSwitchChars() and then the first one of possible chars is used
875 wxChar chSwitch
= !m_data
->m_switchChars
? _T('-')
876 : m_data
->m_switchChars
[0u];
878 bool areLongOptionsEnabled
= AreLongOptionsEnabled();
879 size_t n
, count
= m_data
->m_options
.GetCount();
880 for ( n
= 0; n
< count
; n
++ )
882 wxCmdLineOption
& opt
= m_data
->m_options
[n
];
885 if ( !(opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) )
890 if ( !opt
.shortName
.empty() )
892 brief
<< chSwitch
<< opt
.shortName
;
894 else if ( areLongOptionsEnabled
&& !opt
.longName
.empty() )
896 brief
<< _T("--") << opt
.longName
;
900 if (!opt
.longName
.empty())
902 wxFAIL_MSG( wxT("option with only a long name while long ")
903 wxT("options are disabled") );
907 wxFAIL_MSG( _T("option without neither short nor long name") );
913 if ( !opt
.shortName
.empty() )
915 option
<< _T(" ") << chSwitch
<< opt
.shortName
;
918 if ( areLongOptionsEnabled
&& !opt
.longName
.empty() )
920 option
<< (option
.empty() ? _T(" ") : _T(", "))
921 << _T("--") << opt
.longName
;
924 if ( opt
.kind
!= wxCMD_LINE_SWITCH
)
927 val
<< _T('<') << GetTypeName(opt
.type
) << _T('>');
928 brief
<< _T(' ') << val
;
929 option
<< (!opt
.longName
? _T(':') : _T('=')) << val
;
932 if ( !(opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) )
937 namesOptions
.Add(option
);
938 descOptions
.Add(opt
.description
);
941 count
= m_data
->m_paramDesc
.GetCount();
942 for ( n
= 0; n
< count
; n
++ )
944 wxCmdLineParam
& param
= m_data
->m_paramDesc
[n
];
947 if ( param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
)
952 brief
<< param
.description
;
954 if ( param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
)
959 if ( param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
)
965 if ( !!m_data
->m_logo
)
967 wxLogMessage(m_data
->m_logo
);
970 // in console mode we want to show the brief usage message first, then the
971 // detailed one but in GUI build we give the details first and then the
972 // summary - like this, the brief message appears in the wxLogGui dialog,
978 // now construct the detailed help message
979 size_t len
, lenMax
= 0;
980 count
= namesOptions
.GetCount();
981 for ( n
= 0; n
< count
; n
++ )
983 len
= namesOptions
[n
].length();
989 for ( n
= 0; n
< count
; n
++ )
991 len
= namesOptions
[n
].length();
992 detailed
<< namesOptions
[n
]
993 << wxString(_T(' '), lenMax
- len
) << _T('\t')
998 wxLogMessage(detailed
);
1000 // do it now if not done above
1002 wxLogMessage(brief
);
1006 // ----------------------------------------------------------------------------
1007 // private functions
1008 // ----------------------------------------------------------------------------
1010 static wxString
GetTypeName(wxCmdLineParamType type
)
1016 wxFAIL_MSG( _T("unknown option type") );
1017 // still fall through
1019 case wxCMD_LINE_VAL_STRING
: s
= _("str"); break;
1020 case wxCMD_LINE_VAL_NUMBER
: s
= _("num"); break;
1021 case wxCMD_LINE_VAL_DATE
: s
= _("date"); break;
1028 Returns a string which is equal to the string pointed to by p, but up to the
1029 point where p contains an character that's not allowed.
1030 Allowable characters are letters and numbers, and characters pointed to by
1031 the parameter allowedChars.
1033 For example, if p points to "abcde-@-_", and allowedChars is "-_",
1034 this function returns "abcde-".
1036 static wxString
GetOptionName(const wxChar
*p
,
1037 const wxChar
*allowedChars
)
1041 while ( *p
&& (wxIsalnum(*p
) || wxStrchr(allowedChars
, *p
)) )
1049 // Besides alphanumeric characters, short and long options can
1050 // have other characters.
1052 // A short option additionally can have these
1053 #define wxCMD_LINE_CHARS_ALLOWED_BY_SHORT_OPTION wxT("_?")
1055 // A long option can have the same characters as a short option and a '-'.
1056 #define wxCMD_LINE_CHARS_ALLOWED_BY_LONG_OPTION \
1057 wxCMD_LINE_CHARS_ALLOWED_BY_SHORT_OPTION wxT("-")
1059 static wxString
GetShortOptionName(const wxChar
*p
)
1061 return GetOptionName(p
, wxCMD_LINE_CHARS_ALLOWED_BY_SHORT_OPTION
);
1064 static wxString
GetLongOptionName(const wxChar
*p
)
1066 return GetOptionName(p
, wxCMD_LINE_CHARS_ALLOWED_BY_LONG_OPTION
);
1069 #endif // wxUSE_CMDLINE_PARSER
1071 // ----------------------------------------------------------------------------
1073 // ----------------------------------------------------------------------------
1076 This function is mainly used under Windows (as under Unix we always get the
1077 command line arguments as argc/argv anyhow) and so it tries to handle the
1078 Windows path names (separated by backslashes) correctly. For this it only
1079 considers that a backslash may be used to escape another backslash (but
1080 normally this is _not_ needed) or a quote but nothing else.
1082 In particular, to pass a single argument containing a space to the program
1083 it should be quoted:
1085 myprog.exe foo bar -> argc = 3, argv[1] = "foo", argv[2] = "bar"
1086 myprog.exe "foo bar" -> argc = 2, argv[1] = "foo bar"
1088 To pass an argument containing spaces and quotes, the latter should be
1089 escaped with a backslash:
1091 myprog.exe "foo \"bar\"" -> argc = 2, argv[1] = "foo "bar""
1093 This hopefully matches the conventions used by Explorer/command line
1094 interpreter under Windows. If not, this function should be fixed.
1098 wxArrayString
wxCmdLineParser::ConvertStringToArgs(const wxChar
*p
)
1105 bool isInsideQuotes
= FALSE
;
1109 while ( *p
== _T(' ') || *p
== _T('\t') )
1113 if ( *p
== _T('\0') )
1116 // parse this parameter
1120 // do we have a (lone) backslash?
1121 bool isQuotedByBS
= FALSE
;
1122 while ( *p
== _T('\\') )
1126 // if we have 2 backslashes in a row, output one
1127 // unless it looks like a UNC path \\machine\dir\file.ext
1128 if ( isQuotedByBS
|| arg
.Len() == 0 )
1131 isQuotedByBS
= FALSE
;
1133 else // the next char is quoted
1135 isQuotedByBS
= TRUE
;
1139 bool skipChar
= FALSE
,
1144 if ( !isQuotedByBS
)
1146 // don't put the quote itself in the arg
1149 isInsideQuotes
= !isInsideQuotes
;
1151 //else: insert a literal quote
1157 // we intentionally don't check for preceding backslash
1158 // here as if we allowed it to be used to escape spaces the
1159 // cmd line of the form "foo.exe a:\ c:\bar" wouldn't be
1161 if ( isInsideQuotes
)
1163 // preserve it, skip endParam below
1166 //else: fall through
1175 // ignore backslash before an ordinary character - this
1176 // is needed to properly handle the file names under
1177 // Windows appearing in the command line
1186 // otherwise copy this char to arg