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"
47 #include "wx/msgout.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 static wxString
GetTypeName(wxCmdLineParamType type
);
55 static wxString
GetOptionName(const wxChar
*p
, const wxChar
*allowedChars
);
57 static wxString
GetShortOptionName(const wxChar
*p
);
59 static wxString
GetLongOptionName(const wxChar
*p
);
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // an internal representation of an option
66 struct wxCmdLineOption
68 wxCmdLineOption(wxCmdLineEntryType k
,
72 wxCmdLineParamType typ
,
75 wxASSERT_MSG( !shrt
.empty() || !lng
.empty(),
76 _T("option should have at least one name") );
80 GetShortOptionName(shrt
).Len() == shrt
.Len(),
81 wxT("Short option contains invalid characters")
86 GetLongOptionName(lng
).Len() == lng
.Len(),
87 wxT("Long option contains invalid characters")
103 // can't use union easily here, so just store all possible data fields, we
104 // don't waste much (might still use union later if the number of supported
105 // types increases, so always use the accessor functions and don't access
106 // the fields directly!)
108 void Check(wxCmdLineParamType
WXUNUSED_UNLESS_DEBUG(typ
)) const
110 wxASSERT_MSG( type
== typ
, _T("type mismatch in wxCmdLineOption") );
113 long GetLongVal() const
114 { Check(wxCMD_LINE_VAL_NUMBER
); return m_longVal
; }
115 const wxString
& GetStrVal() const
116 { Check(wxCMD_LINE_VAL_STRING
); return m_strVal
; }
117 const wxDateTime
& GetDateVal() const
118 { Check(wxCMD_LINE_VAL_DATE
); return m_dateVal
; }
120 void SetLongVal(long val
)
121 { Check(wxCMD_LINE_VAL_NUMBER
); m_longVal
= val
; m_hasVal
= TRUE
; }
122 void SetStrVal(const wxString
& val
)
123 { Check(wxCMD_LINE_VAL_STRING
); m_strVal
= val
; m_hasVal
= TRUE
; }
124 void SetDateVal(const wxDateTime val
)
125 { Check(wxCMD_LINE_VAL_DATE
); m_dateVal
= val
; m_hasVal
= TRUE
; }
127 void SetHasValue(bool hasValue
= TRUE
) { m_hasVal
= hasValue
; }
128 bool HasValue() const { return m_hasVal
; }
131 wxCmdLineEntryType kind
;
135 wxCmdLineParamType type
;
143 wxDateTime m_dateVal
;
146 struct wxCmdLineParam
148 wxCmdLineParam(const wxString
& desc
,
149 wxCmdLineParamType typ
,
157 wxString description
;
158 wxCmdLineParamType type
;
162 WX_DECLARE_OBJARRAY(wxCmdLineOption
, wxArrayOptions
);
163 WX_DECLARE_OBJARRAY(wxCmdLineParam
, wxArrayParams
);
165 #include "wx/arrimpl.cpp"
167 WX_DEFINE_OBJARRAY(wxArrayOptions
);
168 WX_DEFINE_OBJARRAY(wxArrayParams
);
170 // the parser internal state
171 struct wxCmdLineParserData
174 wxString m_switchChars
; // characters which may start an option
175 bool m_enableLongOptions
; // TRUE if long options are enabled
176 wxString m_logo
; // some extra text to show in Usage()
179 wxArrayString m_arguments
; // == argv, argc == m_arguments.GetCount()
180 wxArrayOptions m_options
; // all possible options and switchrs
181 wxArrayParams m_paramDesc
; // description of all possible params
182 wxArrayString m_parameters
; // all params found
185 wxCmdLineParserData();
186 void SetArguments(int argc
, wxChar
**argv
);
187 void SetArguments(const wxString
& cmdline
);
189 int FindOption(const wxString
& name
);
190 int FindOptionByLongName(const wxString
& name
);
193 // ============================================================================
195 // ============================================================================
197 // ----------------------------------------------------------------------------
198 // wxCmdLineParserData
199 // ----------------------------------------------------------------------------
201 wxCmdLineParserData::wxCmdLineParserData()
203 m_enableLongOptions
= TRUE
;
205 m_switchChars
= _T("-");
207 m_switchChars
= _T("/-");
211 void wxCmdLineParserData::SetArguments(int argc
, wxChar
**argv
)
215 for ( int n
= 0; n
< argc
; n
++ )
217 m_arguments
.Add(argv
[n
]);
221 void wxCmdLineParserData::SetArguments(const wxString
& cmdLine
)
225 m_arguments
.Add(wxTheApp
->GetAppName());
227 wxArrayString args
= wxCmdLineParser::ConvertStringToArgs(cmdLine
);
229 WX_APPEND_ARRAY(m_arguments
, args
);
232 int wxCmdLineParserData::FindOption(const wxString
& name
)
236 size_t count
= m_options
.GetCount();
237 for ( size_t n
= 0; n
< count
; n
++ )
239 if ( m_options
[n
].shortName
== name
)
250 int wxCmdLineParserData::FindOptionByLongName(const wxString
& name
)
252 size_t count
= m_options
.GetCount();
253 for ( size_t n
= 0; n
< count
; n
++ )
255 if ( m_options
[n
].longName
== name
)
265 // ----------------------------------------------------------------------------
266 // construction and destruction
267 // ----------------------------------------------------------------------------
269 void wxCmdLineParser::Init()
271 m_data
= new wxCmdLineParserData
;
274 void wxCmdLineParser::SetCmdLine(int argc
, wxChar
**argv
)
276 m_data
->SetArguments(argc
, argv
);
279 void wxCmdLineParser::SetCmdLine(const wxString
& cmdline
)
281 m_data
->SetArguments(cmdline
);
284 wxCmdLineParser::~wxCmdLineParser()
289 // ----------------------------------------------------------------------------
291 // ----------------------------------------------------------------------------
293 void wxCmdLineParser::SetSwitchChars(const wxString
& switchChars
)
295 m_data
->m_switchChars
= switchChars
;
298 void wxCmdLineParser::EnableLongOptions(bool enable
)
300 m_data
->m_enableLongOptions
= enable
;
303 bool wxCmdLineParser::AreLongOptionsEnabled()
305 return m_data
->m_enableLongOptions
;
308 void wxCmdLineParser::SetLogo(const wxString
& logo
)
310 m_data
->m_logo
= logo
;
313 // ----------------------------------------------------------------------------
314 // command line construction
315 // ----------------------------------------------------------------------------
317 void wxCmdLineParser::SetDesc(const wxCmdLineEntryDesc
*desc
)
321 switch ( desc
->kind
)
323 case wxCMD_LINE_SWITCH
:
324 AddSwitch(desc
->shortName
, desc
->longName
, desc
->description
,
328 case wxCMD_LINE_OPTION
:
329 AddOption(desc
->shortName
, desc
->longName
, desc
->description
,
330 desc
->type
, desc
->flags
);
333 case wxCMD_LINE_PARAM
:
334 AddParam(desc
->description
, desc
->type
, desc
->flags
);
338 wxFAIL_MSG( _T("unknown command line entry type") );
339 // still fall through
341 case wxCMD_LINE_NONE
:
347 void wxCmdLineParser::AddSwitch(const wxString
& shortName
,
348 const wxString
& longName
,
349 const wxString
& desc
,
352 wxASSERT_MSG( m_data
->FindOption(shortName
) == wxNOT_FOUND
,
353 _T("duplicate switch") );
355 wxCmdLineOption
*option
= new wxCmdLineOption(wxCMD_LINE_SWITCH
,
356 shortName
, longName
, desc
,
357 wxCMD_LINE_VAL_NONE
, flags
);
359 m_data
->m_options
.Add(option
);
362 void wxCmdLineParser::AddOption(const wxString
& shortName
,
363 const wxString
& longName
,
364 const wxString
& desc
,
365 wxCmdLineParamType type
,
368 wxASSERT_MSG( m_data
->FindOption(shortName
) == wxNOT_FOUND
,
369 _T("duplicate option") );
371 wxCmdLineOption
*option
= new wxCmdLineOption(wxCMD_LINE_OPTION
,
372 shortName
, longName
, desc
,
375 m_data
->m_options
.Add(option
);
378 void wxCmdLineParser::AddParam(const wxString
& desc
,
379 wxCmdLineParamType type
,
382 // do some consistency checks: a required parameter can't follow an
383 // optional one and nothing should follow a parameter with MULTIPLE flag
385 if ( !m_data
->m_paramDesc
.IsEmpty() )
387 wxCmdLineParam
& param
= m_data
->m_paramDesc
.Last();
389 wxASSERT_MSG( !(param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
),
390 _T("all parameters after the one with wxCMD_LINE_PARAM_MULTIPLE style will be ignored") );
392 if ( !(flags
& wxCMD_LINE_PARAM_OPTIONAL
) )
394 wxASSERT_MSG( !(param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
),
395 _T("a required parameter can't follow an optional one") );
400 wxCmdLineParam
*param
= new wxCmdLineParam(desc
, type
, flags
);
402 m_data
->m_paramDesc
.Add(param
);
405 // ----------------------------------------------------------------------------
406 // access to parse command line
407 // ----------------------------------------------------------------------------
409 bool wxCmdLineParser::Found(const wxString
& name
) const
411 int i
= m_data
->FindOption(name
);
412 if ( i
== wxNOT_FOUND
)
413 i
= m_data
->FindOptionByLongName(name
);
415 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown switch") );
417 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
418 if ( !opt
.HasValue() )
424 bool wxCmdLineParser::Found(const wxString
& name
, wxString
*value
) const
426 int i
= m_data
->FindOption(name
);
427 if ( i
== wxNOT_FOUND
)
428 i
= m_data
->FindOptionByLongName(name
);
430 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
432 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
433 if ( !opt
.HasValue() )
436 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
438 *value
= opt
.GetStrVal();
443 bool wxCmdLineParser::Found(const wxString
& name
, long *value
) const
445 int i
= m_data
->FindOption(name
);
446 if ( i
== wxNOT_FOUND
)
447 i
= m_data
->FindOptionByLongName(name
);
449 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
451 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
452 if ( !opt
.HasValue() )
455 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
457 *value
= opt
.GetLongVal();
462 bool wxCmdLineParser::Found(const wxString
& name
, wxDateTime
*value
) const
464 int i
= m_data
->FindOption(name
);
465 if ( i
== wxNOT_FOUND
)
466 i
= m_data
->FindOptionByLongName(name
);
468 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
470 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
471 if ( !opt
.HasValue() )
474 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
476 *value
= opt
.GetDateVal();
481 size_t wxCmdLineParser::GetParamCount() const
483 return m_data
->m_parameters
.GetCount();
486 wxString
wxCmdLineParser::GetParam(size_t n
) const
488 wxCHECK_MSG( n
< GetParamCount(), wxEmptyString
, _T("invalid param index") );
490 return m_data
->m_parameters
[n
];
493 // Resets switches and options
494 void wxCmdLineParser::Reset()
496 for ( size_t i
= 0; i
< m_data
->m_options
.Count(); i
++ )
498 wxCmdLineOption
& opt
= m_data
->m_options
[i
];
499 opt
.SetHasValue(FALSE
);
504 // ----------------------------------------------------------------------------
505 // the real work is done here
506 // ----------------------------------------------------------------------------
508 int wxCmdLineParser::Parse(bool showUsage
)
510 bool maybeOption
= TRUE
; // can the following arg be an option?
511 bool ok
= TRUE
; // TRUE until an error is detected
512 bool helpRequested
= FALSE
; // TRUE if "-h" was given
513 bool hadRepeatableParam
= FALSE
; // TRUE if found param with MULTIPLE flag
515 size_t currentParam
= 0; // the index in m_paramDesc
517 size_t countParam
= m_data
->m_paramDesc
.GetCount();
524 size_t count
= m_data
->m_arguments
.GetCount();
525 for ( size_t n
= 1; ok
&& (n
< count
); n
++ ) // 0 is program name
527 arg
= m_data
->m_arguments
[n
];
529 // special case: "--" should be discarded and all following arguments
530 // should be considered as parameters, even if they start with '-' and
531 // not like options (this is POSIX-like)
532 if ( arg
== _T("--") )
539 // empty argument or just '-' is not an option but a parameter
540 if ( maybeOption
&& arg
.length() > 1 &&
541 wxStrchr(m_data
->m_switchChars
, arg
[0u]) )
545 int optInd
= wxNOT_FOUND
; // init to suppress warnings
547 // an option or a switch: find whether it's a long or a short one
548 if ( arg
[0u] == _T('-') && arg
[1u] == _T('-') )
554 const wxChar
*p
= arg
.c_str() + 2;
556 bool longOptionsEnabled
= AreLongOptionsEnabled();
558 name
= GetLongOptionName(p
);
560 if (longOptionsEnabled
)
562 optInd
= m_data
->FindOptionByLongName(name
);
563 if ( optInd
== wxNOT_FOUND
)
565 errorMsg
<< wxString::Format(_("Unknown long option '%s'"), name
.c_str()) << wxT("\n");
570 optInd
= wxNOT_FOUND
; // Sanity check
572 // Print the argument including leading "--"
573 name
.Prepend( wxT("--") );
574 errorMsg
<< wxString::Format(_("Unknown option '%s'"), name
.c_str()) << wxT("\n");
582 // a short one: as they can be cumulated, we try to find the
583 // longest substring which is a valid option
584 const wxChar
*p
= arg
.c_str() + 1;
586 name
= GetShortOptionName(p
);
588 size_t len
= name
.length();
593 // we couldn't find a valid option name in the
594 // beginning of this string
595 errorMsg
<< wxString::Format(_("Unknown option '%s'"), name
.c_str()) << wxT("\n");
601 optInd
= m_data
->FindOption(name
.Left(len
));
603 // will try with one character less the next time
607 while ( optInd
== wxNOT_FOUND
);
609 len
++; // compensates extra len-- above
610 if ( (optInd
!= wxNOT_FOUND
) && (len
!= name
.length()) )
612 // first of all, the option name is only part of this
614 name
= name
.Left(len
);
616 // our option is only part of this argument, there is
617 // something else in it - it is either the value of this
618 // option or other switches if it is a switch
619 if ( m_data
->m_options
[(size_t)optInd
].kind
620 == wxCMD_LINE_SWITCH
)
622 // pretend that all the rest of the argument is the
623 // next argument, in fact
624 wxString arg2
= arg
[0u];
625 arg2
+= arg
.Mid(len
+ 1); // +1 for leading '-'
627 m_data
->m_arguments
.Insert(arg2
, n
+ 1);
630 //else: it's our value, we'll deal with it below
634 if ( optInd
== wxNOT_FOUND
)
638 continue; // will break, in fact
641 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)optInd
];
642 if ( opt
.kind
== wxCMD_LINE_SWITCH
)
644 // nothing more to do
647 if ( opt
.flags
& wxCMD_LINE_OPTION_HELP
)
649 helpRequested
= TRUE
;
651 // it's not an error, but we still stop here
659 // +1 for leading '-'
660 const wxChar
*p
= arg
.c_str() + 1 + name
.length();
663 p
++; // for another leading '-'
665 if ( *p
++ != _T('=') )
667 errorMsg
<< wxString::Format(_("Option '%s' requires a value, '=' expected."), name
.c_str()) << wxT("\n");
683 // the value is in the next argument
686 // ... but there is none
687 errorMsg
<< wxString::Format(_("Option '%s' requires a value."),
688 name
.c_str()) << wxT("\n");
694 // ... take it from there
695 p
= m_data
->m_arguments
[n
].c_str();
700 // the value is right here: this may be legal or
701 // not depending on the option style
702 if ( opt
.flags
& wxCMD_LINE_NEEDS_SEPARATOR
)
704 errorMsg
<< wxString::Format(_("Separator expected after the option '%s'."),
705 name
.c_str()) << wxT("\n");
718 wxFAIL_MSG( _T("unknown option type") );
719 // still fall through
721 case wxCMD_LINE_VAL_STRING
:
722 opt
.SetStrVal(value
);
725 case wxCMD_LINE_VAL_NUMBER
:
728 if ( value
.ToLong(&val
) )
734 errorMsg
<< wxString::Format(_("'%s' is not a correct numeric value for option '%s'."),
735 value
.c_str(), name
.c_str()) << wxT("\n");
742 case wxCMD_LINE_VAL_DATE
:
745 const wxChar
*res
= dt
.ParseDate(value
);
748 errorMsg
<< wxString::Format(_("Option '%s': '%s' cannot be converted to a date."),
749 name
.c_str(), value
.c_str()) << wxT("\n");
766 if ( currentParam
< countParam
)
768 wxCmdLineParam
& param
= m_data
->m_paramDesc
[currentParam
];
770 // TODO check the param type
772 m_data
->m_parameters
.Add(arg
);
774 if ( !(param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
) )
780 wxASSERT_MSG( currentParam
== countParam
- 1,
781 _T("all parameters after the one with wxCMD_LINE_PARAM_MULTIPLE style are ignored") );
783 // remember that we did have this last repeatable parameter
784 hadRepeatableParam
= TRUE
;
789 errorMsg
<< wxString::Format(_("Unexpected parameter '%s'"), arg
.c_str()) << wxT("\n");
796 // verify that all mandatory options were given
799 size_t countOpt
= m_data
->m_options
.GetCount();
800 for ( size_t n
= 0; ok
&& (n
< countOpt
); n
++ )
802 wxCmdLineOption
& opt
= m_data
->m_options
[n
];
803 if ( (opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) && !opt
.HasValue() )
808 optName
= opt
.shortName
;
812 if ( AreLongOptionsEnabled() )
814 optName
.Printf( _("%s (or %s)"),
815 opt
.shortName
.c_str(),
816 opt
.longName
.c_str() );
820 optName
.Printf( wxT("%s"),
821 opt
.shortName
.c_str() );
825 errorMsg
<< wxString::Format(_("The value for the option '%s' must be specified."),
826 optName
.c_str()) << wxT("\n");
832 for ( ; ok
&& (currentParam
< countParam
); currentParam
++ )
834 wxCmdLineParam
& param
= m_data
->m_paramDesc
[currentParam
];
835 if ( (currentParam
== countParam
- 1) &&
836 (param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
) &&
839 // special case: currentParam wasn't incremented, but we did
840 // have it, so don't give error
844 if ( !(param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
) )
846 errorMsg
<< wxString::Format(_("The required parameter '%s' was not specified."),
847 param
.description
.c_str()) << wxT("\n");
854 // if there was an error during parsing the command line, show this error
855 // and also the usage message if it had been requested
856 if ( !ok
&& (!errorMsg
.empty() || (helpRequested
&& showUsage
)) )
858 wxMessageOutput
* msgOut
= wxMessageOutput::Get();
863 usage
= GetUsageString();
865 msgOut
->Printf( wxT("%s%s"), usage
.c_str(), errorMsg
.c_str() );
869 wxFAIL_MSG( _T("no wxMessageOutput object?") );
873 return ok
? 0 : helpRequested
? -1 : 1;
876 // ----------------------------------------------------------------------------
877 // give the usage message
878 // ----------------------------------------------------------------------------
880 void wxCmdLineParser::Usage()
882 wxMessageOutput
* msgOut
= wxMessageOutput::Get();
885 msgOut
->Printf( wxT("%s"), GetUsageString().c_str() );
889 wxFAIL_MSG( _T("no wxMessageOutput object?") );
893 wxString
wxCmdLineParser::GetUsageString()
895 wxString appname
= wxTheApp
->GetAppName();
898 wxCHECK_MSG( !m_data
->m_arguments
.IsEmpty(), wxEmptyString
,
899 _T("no program name") );
901 appname
= wxFileNameFromPath(m_data
->m_arguments
[0]);
902 wxStripExtension(appname
);
905 // we construct the brief cmd line desc on the fly, but not the detailed
906 // help message below because we want to align the options descriptions
907 // and for this we must first know the longest one of them
909 wxArrayString namesOptions
, descOptions
;
911 if ( !m_data
->m_logo
.empty() )
913 usage
<< m_data
->m_logo
<< _T('\n');
916 usage
<< wxString::Format(_("Usage: %s"), appname
.c_str());
918 // the switch char is usually '-' but this can be changed with
919 // SetSwitchChars() and then the first one of possible chars is used
920 wxChar chSwitch
= !m_data
->m_switchChars
? _T('-')
921 : m_data
->m_switchChars
[0u];
923 bool areLongOptionsEnabled
= AreLongOptionsEnabled();
924 size_t n
, count
= m_data
->m_options
.GetCount();
925 for ( n
= 0; n
< count
; n
++ )
927 wxCmdLineOption
& opt
= m_data
->m_options
[n
];
930 if ( !(opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) )
935 if ( !opt
.shortName
.empty() )
937 usage
<< chSwitch
<< opt
.shortName
;
939 else if ( areLongOptionsEnabled
&& !opt
.longName
.empty() )
941 usage
<< _T("--") << opt
.longName
;
945 if (!opt
.longName
.empty())
947 wxFAIL_MSG( wxT("option with only a long name while long ")
948 wxT("options are disabled") );
952 wxFAIL_MSG( _T("option without neither short nor long name") );
958 if ( !opt
.shortName
.empty() )
960 option
<< _T(" ") << chSwitch
<< opt
.shortName
;
963 if ( areLongOptionsEnabled
&& !opt
.longName
.empty() )
965 option
<< (option
.empty() ? _T(" ") : _T(", "))
966 << _T("--") << opt
.longName
;
969 if ( opt
.kind
!= wxCMD_LINE_SWITCH
)
972 val
<< _T('<') << GetTypeName(opt
.type
) << _T('>');
973 usage
<< _T(' ') << val
;
974 option
<< (!opt
.longName
? _T(':') : _T('=')) << val
;
977 if ( !(opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) )
982 namesOptions
.Add(option
);
983 descOptions
.Add(opt
.description
);
986 count
= m_data
->m_paramDesc
.GetCount();
987 for ( n
= 0; n
< count
; n
++ )
989 wxCmdLineParam
& param
= m_data
->m_paramDesc
[n
];
992 if ( param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
)
997 usage
<< param
.description
;
999 if ( param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
)
1004 if ( param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
)
1012 // now construct the detailed help message
1013 size_t len
, lenMax
= 0;
1014 count
= namesOptions
.GetCount();
1015 for ( n
= 0; n
< count
; n
++ )
1017 len
= namesOptions
[n
].length();
1022 for ( n
= 0; n
< count
; n
++ )
1024 len
= namesOptions
[n
].length();
1025 usage
<< namesOptions
[n
]
1026 << wxString(_T(' '), lenMax
- len
) << _T('\t')
1034 // ----------------------------------------------------------------------------
1035 // private functions
1036 // ----------------------------------------------------------------------------
1038 static wxString
GetTypeName(wxCmdLineParamType type
)
1044 wxFAIL_MSG( _T("unknown option type") );
1045 // still fall through
1047 case wxCMD_LINE_VAL_STRING
:
1051 case wxCMD_LINE_VAL_NUMBER
:
1055 case wxCMD_LINE_VAL_DATE
:
1064 Returns a string which is equal to the string pointed to by p, but up to the
1065 point where p contains an character that's not allowed.
1066 Allowable characters are letters and numbers, and characters pointed to by
1067 the parameter allowedChars.
1069 For example, if p points to "abcde-@-_", and allowedChars is "-_",
1070 this function returns "abcde-".
1072 static wxString
GetOptionName(const wxChar
*p
,
1073 const wxChar
*allowedChars
)
1077 while ( *p
&& (wxIsalnum(*p
) || wxStrchr(allowedChars
, *p
)) )
1085 // Besides alphanumeric characters, short and long options can
1086 // have other characters.
1088 // A short option additionally can have these
1089 #define wxCMD_LINE_CHARS_ALLOWED_BY_SHORT_OPTION wxT("_?")
1091 // A long option can have the same characters as a short option and a '-'.
1092 #define wxCMD_LINE_CHARS_ALLOWED_BY_LONG_OPTION \
1093 wxCMD_LINE_CHARS_ALLOWED_BY_SHORT_OPTION wxT("-")
1095 static wxString
GetShortOptionName(const wxChar
*p
)
1097 return GetOptionName(p
, wxCMD_LINE_CHARS_ALLOWED_BY_SHORT_OPTION
);
1100 static wxString
GetLongOptionName(const wxChar
*p
)
1102 return GetOptionName(p
, wxCMD_LINE_CHARS_ALLOWED_BY_LONG_OPTION
);
1105 #endif // wxUSE_CMDLINE_PARSER
1107 // ----------------------------------------------------------------------------
1109 // ----------------------------------------------------------------------------
1112 This function is mainly used under Windows (as under Unix we always get the
1113 command line arguments as argc/argv anyhow) and so it tries to handle the
1114 Windows path names (separated by backslashes) correctly. For this it only
1115 considers that a backslash may be used to escape another backslash (but
1116 normally this is _not_ needed) or a quote but nothing else.
1118 In particular, to pass a single argument containing a space to the program
1119 it should be quoted:
1121 myprog.exe foo bar -> argc = 3, argv[1] = "foo", argv[2] = "bar"
1122 myprog.exe "foo bar" -> argc = 2, argv[1] = "foo bar"
1124 To pass an argument containing spaces and quotes, the latter should be
1125 escaped with a backslash:
1127 myprog.exe "foo \"bar\"" -> argc = 2, argv[1] = "foo "bar""
1129 This hopefully matches the conventions used by Explorer/command line
1130 interpreter under Windows. If not, this function should be fixed.
1134 wxArrayString
wxCmdLineParser::ConvertStringToArgs(const wxChar
*p
)
1141 bool isInsideQuotes
= FALSE
;
1145 while ( *p
== _T(' ') || *p
== _T('\t') )
1149 if ( *p
== _T('\0') )
1152 // parse this parameter
1156 // do we have a (lone) backslash?
1157 bool isQuotedByBS
= FALSE
;
1158 while ( *p
== _T('\\') )
1162 // if we have 2 backslashes in a row, output one
1163 // unless it looks like a UNC path \\machine\dir\file.ext
1164 if ( isQuotedByBS
|| arg
.Len() == 0 )
1167 isQuotedByBS
= FALSE
;
1169 else // the next char is quoted
1171 isQuotedByBS
= TRUE
;
1175 bool skipChar
= FALSE
,
1180 if ( !isQuotedByBS
)
1182 // don't put the quote itself in the arg
1185 isInsideQuotes
= !isInsideQuotes
;
1187 //else: insert a literal quote
1193 // we intentionally don't check for preceding backslash
1194 // here as if we allowed it to be used to escape spaces the
1195 // cmd line of the form "foo.exe a:\ c:\bar" wouldn't be
1197 if ( isInsideQuotes
)
1199 // preserve it, skip endParam below
1202 //else: fall through
1211 // ignore backslash before an ordinary character - this
1212 // is needed to properly handle the file names under
1213 // Windows appearing in the command line
1222 // otherwise copy this char to arg