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 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // an internal representation of an option
59 struct wxCmdLineOption
61 wxCmdLineOption(wxCmdLineEntryType k
,
65 wxCmdLineParamType typ
,
68 wxASSERT_MSG( !shrt
.empty() || !lng
.empty(),
69 _T("option should have at least one name") );
83 // can't use union easily here, so just store all possible data fields, we
84 // don't waste much (might still use union later if the number of supported
85 // types increases, so always use the accessor functions and don't access
86 // the fields directly!)
88 void Check(wxCmdLineParamType
WXUNUSED_UNLESS_DEBUG(typ
)) const
90 wxASSERT_MSG( type
== typ
, _T("type mismatch in wxCmdLineOption") );
93 long GetLongVal() const
94 { Check(wxCMD_LINE_VAL_NUMBER
); return m_longVal
; }
95 const wxString
& GetStrVal() const
96 { Check(wxCMD_LINE_VAL_STRING
); return m_strVal
; }
97 const wxDateTime
& GetDateVal() const
98 { Check(wxCMD_LINE_VAL_DATE
); return m_dateVal
; }
100 void SetLongVal(long val
)
101 { Check(wxCMD_LINE_VAL_NUMBER
); m_longVal
= val
; m_hasVal
= TRUE
; }
102 void SetStrVal(const wxString
& val
)
103 { Check(wxCMD_LINE_VAL_STRING
); m_strVal
= val
; m_hasVal
= TRUE
; }
104 void SetDateVal(const wxDateTime val
)
105 { Check(wxCMD_LINE_VAL_DATE
); m_dateVal
= val
; m_hasVal
= TRUE
; }
107 void SetHasValue(bool hasValue
= TRUE
) { m_hasVal
= hasValue
; }
108 bool HasValue() const { return m_hasVal
; }
111 wxCmdLineEntryType kind
;
115 wxCmdLineParamType type
;
123 wxDateTime m_dateVal
;
126 struct wxCmdLineParam
128 wxCmdLineParam(const wxString
& desc
,
129 wxCmdLineParamType typ
,
137 wxString description
;
138 wxCmdLineParamType type
;
142 WX_DECLARE_OBJARRAY(wxCmdLineOption
, wxArrayOptions
);
143 WX_DECLARE_OBJARRAY(wxCmdLineParam
, wxArrayParams
);
145 #include "wx/arrimpl.cpp"
147 WX_DEFINE_OBJARRAY(wxArrayOptions
);
148 WX_DEFINE_OBJARRAY(wxArrayParams
);
150 // the parser internal state
151 struct wxCmdLineParserData
154 wxString m_switchChars
; // characters which may start an option
155 bool m_enableLongOptions
; // TRUE if long options are enabled
156 wxString m_logo
; // some extra text to show in Usage()
159 wxArrayString m_arguments
; // == argv, argc == m_arguments.GetCount()
160 wxArrayOptions m_options
; // all possible options and switchrs
161 wxArrayParams m_paramDesc
; // description of all possible params
162 wxArrayString m_parameters
; // all params found
165 wxCmdLineParserData();
166 void SetArguments(int argc
, wxChar
**argv
);
167 void SetArguments(const wxString
& cmdline
);
169 int FindOption(const wxString
& name
);
170 int FindOptionByLongName(const wxString
& name
);
173 // ============================================================================
175 // ============================================================================
177 // ----------------------------------------------------------------------------
178 // wxCmdLineParserData
179 // ----------------------------------------------------------------------------
181 wxCmdLineParserData::wxCmdLineParserData()
183 m_enableLongOptions
= TRUE
;
185 m_switchChars
= _T("-");
187 m_switchChars
= _T("/-");
191 void wxCmdLineParserData::SetArguments(int argc
, wxChar
**argv
)
195 for ( int n
= 0; n
< argc
; n
++ )
197 m_arguments
.Add(argv
[n
]);
201 void wxCmdLineParserData::SetArguments(const wxString
& cmdLine
)
205 m_arguments
.Add(wxTheApp
->GetAppName());
207 wxArrayString args
= wxCmdLineParser::ConvertStringToArgs(cmdLine
);
209 WX_APPEND_ARRAY(m_arguments
, args
);
212 int wxCmdLineParserData::FindOption(const wxString
& name
)
216 size_t count
= m_options
.GetCount();
217 for ( size_t n
= 0; n
< count
; n
++ )
219 if ( m_options
[n
].shortName
== name
)
230 int wxCmdLineParserData::FindOptionByLongName(const wxString
& name
)
232 size_t count
= m_options
.GetCount();
233 for ( size_t n
= 0; n
< count
; n
++ )
235 if ( m_options
[n
].longName
== name
)
245 // ----------------------------------------------------------------------------
246 // construction and destruction
247 // ----------------------------------------------------------------------------
249 void wxCmdLineParser::Init()
251 m_data
= new wxCmdLineParserData
;
254 void wxCmdLineParser::SetCmdLine(int argc
, wxChar
**argv
)
256 m_data
->SetArguments(argc
, argv
);
259 void wxCmdLineParser::SetCmdLine(const wxString
& cmdline
)
261 m_data
->SetArguments(cmdline
);
264 wxCmdLineParser::~wxCmdLineParser()
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 void wxCmdLineParser::SetSwitchChars(const wxString
& switchChars
)
275 m_data
->m_switchChars
= switchChars
;
278 void wxCmdLineParser::EnableLongOptions(bool enable
)
280 m_data
->m_enableLongOptions
= enable
;
283 void wxCmdLineParser::SetLogo(const wxString
& logo
)
285 m_data
->m_logo
= logo
;
288 // ----------------------------------------------------------------------------
289 // command line construction
290 // ----------------------------------------------------------------------------
292 void wxCmdLineParser::SetDesc(const wxCmdLineEntryDesc
*desc
)
296 switch ( desc
->kind
)
298 case wxCMD_LINE_SWITCH
:
299 AddSwitch(desc
->shortName
, desc
->longName
, desc
->description
,
303 case wxCMD_LINE_OPTION
:
304 AddOption(desc
->shortName
, desc
->longName
, desc
->description
,
305 desc
->type
, desc
->flags
);
308 case wxCMD_LINE_PARAM
:
309 AddParam(desc
->description
, desc
->type
, desc
->flags
);
313 wxFAIL_MSG( _T("unknown command line entry type") );
314 // still fall through
316 case wxCMD_LINE_NONE
:
322 void wxCmdLineParser::AddSwitch(const wxString
& shortName
,
323 const wxString
& longName
,
324 const wxString
& desc
,
327 wxASSERT_MSG( m_data
->FindOption(shortName
) == wxNOT_FOUND
,
328 _T("duplicate switch") );
330 wxCmdLineOption
*option
= new wxCmdLineOption(wxCMD_LINE_SWITCH
,
331 shortName
, longName
, desc
,
332 wxCMD_LINE_VAL_NONE
, flags
);
334 m_data
->m_options
.Add(option
);
337 void wxCmdLineParser::AddOption(const wxString
& shortName
,
338 const wxString
& longName
,
339 const wxString
& desc
,
340 wxCmdLineParamType type
,
343 wxASSERT_MSG( m_data
->FindOption(shortName
) == wxNOT_FOUND
,
344 _T("duplicate option") );
346 wxCmdLineOption
*option
= new wxCmdLineOption(wxCMD_LINE_OPTION
,
347 shortName
, longName
, desc
,
350 m_data
->m_options
.Add(option
);
353 void wxCmdLineParser::AddParam(const wxString
& desc
,
354 wxCmdLineParamType type
,
357 // do some consistency checks: a required parameter can't follow an
358 // optional one and nothing should follow a parameter with MULTIPLE flag
360 if ( !m_data
->m_paramDesc
.IsEmpty() )
362 wxCmdLineParam
& param
= m_data
->m_paramDesc
.Last();
364 wxASSERT_MSG( !(param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
),
365 _T("all parameters after the one with wxCMD_LINE_PARAM_MULTIPLE style will be ignored") );
367 if ( !(flags
& wxCMD_LINE_PARAM_OPTIONAL
) )
369 wxASSERT_MSG( !(param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
),
370 _T("a required parameter can't follow an optional one") );
375 wxCmdLineParam
*param
= new wxCmdLineParam(desc
, type
, flags
);
377 m_data
->m_paramDesc
.Add(param
);
380 // ----------------------------------------------------------------------------
381 // access to parse command line
382 // ----------------------------------------------------------------------------
384 bool wxCmdLineParser::Found(const wxString
& name
) const
386 int i
= m_data
->FindOption(name
);
387 if ( i
== wxNOT_FOUND
)
388 i
= m_data
->FindOptionByLongName(name
);
390 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown switch") );
392 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
393 if ( !opt
.HasValue() )
399 bool wxCmdLineParser::Found(const wxString
& name
, wxString
*value
) const
401 int i
= m_data
->FindOption(name
);
402 if ( i
== wxNOT_FOUND
)
403 i
= m_data
->FindOptionByLongName(name
);
405 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
407 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
408 if ( !opt
.HasValue() )
411 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
413 *value
= opt
.GetStrVal();
418 bool wxCmdLineParser::Found(const wxString
& name
, long *value
) const
420 int i
= m_data
->FindOption(name
);
421 if ( i
== wxNOT_FOUND
)
422 i
= m_data
->FindOptionByLongName(name
);
424 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
426 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
427 if ( !opt
.HasValue() )
430 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
432 *value
= opt
.GetLongVal();
437 bool wxCmdLineParser::Found(const wxString
& name
, wxDateTime
*value
) const
439 int i
= m_data
->FindOption(name
);
440 if ( i
== wxNOT_FOUND
)
441 i
= m_data
->FindOptionByLongName(name
);
443 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
445 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
446 if ( !opt
.HasValue() )
449 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
451 *value
= opt
.GetDateVal();
456 size_t wxCmdLineParser::GetParamCount() const
458 return m_data
->m_parameters
.GetCount();
461 wxString
wxCmdLineParser::GetParam(size_t n
) const
463 return m_data
->m_parameters
[n
];
466 // Resets switches and options
467 void wxCmdLineParser::Reset()
469 for ( size_t i
= 0; i
< m_data
->m_options
.Count(); i
++ )
471 wxCmdLineOption
& opt
= m_data
->m_options
[i
];
472 opt
.SetHasValue(FALSE
);
477 // ----------------------------------------------------------------------------
478 // the real work is done here
479 // ----------------------------------------------------------------------------
481 int wxCmdLineParser::Parse(bool showUsage
)
483 bool maybeOption
= TRUE
; // can the following arg be an option?
484 bool ok
= TRUE
; // TRUE until an error is detected
485 bool helpRequested
= FALSE
; // TRUE if "-h" was given
486 bool hadRepeatableParam
= FALSE
; // TRUE if found param with MULTIPLE flag
488 size_t currentParam
= 0; // the index in m_paramDesc
490 size_t countParam
= m_data
->m_paramDesc
.GetCount();
496 size_t count
= m_data
->m_arguments
.GetCount();
497 for ( size_t n
= 1; ok
&& (n
< count
); n
++ ) // 0 is program name
499 arg
= m_data
->m_arguments
[n
];
501 // special case: "--" should be discarded and all following arguments
502 // should be considered as parameters, even if they start with '-' and
503 // not like options (this is POSIX-like)
504 if ( arg
== _T("--") )
511 // empty argument or just '-' is not an option but a parameter
512 if ( maybeOption
&& arg
.length() > 1 &&
513 wxStrchr(m_data
->m_switchChars
, arg
[0u]) )
517 int optInd
= wxNOT_FOUND
; // init to suppress warnings
519 // an option or a switch: find whether it's a long or a short one
520 if ( m_data
->m_enableLongOptions
&&
521 arg
[0u] == _T('-') && arg
[1u] == _T('-') )
526 const wxChar
*p
= arg
.c_str() + 2;
527 while ( wxIsalnum(*p
) || (*p
== _T('_')) || (*p
== _T('-')) )
532 optInd
= m_data
->FindOptionByLongName(name
);
533 if ( optInd
== wxNOT_FOUND
)
535 wxLogError(_("Unknown long option '%s'"), name
.c_str());
542 // a short one: as they can be cumulated, we try to find the
543 // longest substring which is a valid option
544 const wxChar
*p
= arg
.c_str() + 1;
545 while ( wxIsalnum(*p
) || (*p
== _T('_')) )
550 size_t len
= name
.length();
555 // we couldn't find a valid option name in the
556 // beginning of this string
557 wxLogError(_("Unknown option '%s'"), name
.c_str());
563 optInd
= m_data
->FindOption(name
.Left(len
));
565 // will try with one character less the next time
569 while ( optInd
== wxNOT_FOUND
);
571 len
++; // compensates extra len-- above
572 if ( (optInd
!= wxNOT_FOUND
) && (len
!= name
.length()) )
574 // first of all, the option name is only part of this
576 name
= name
.Left(len
);
578 // our option is only part of this argument, there is
579 // something else in it - it is either the value of this
580 // option or other switches if it is a switch
581 if ( m_data
->m_options
[(size_t)optInd
].kind
582 == wxCMD_LINE_SWITCH
)
584 // pretend that all the rest of the argument is the
585 // next argument, in fact
586 wxString arg2
= arg
[0u];
587 arg2
+= arg
.Mid(len
+ 1); // +1 for leading '-'
589 m_data
->m_arguments
.Insert(arg2
, n
+ 1);
592 //else: it's our value, we'll deal with it below
596 if ( optInd
== wxNOT_FOUND
)
600 continue; // will break, in fact
603 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)optInd
];
604 if ( opt
.kind
== wxCMD_LINE_SWITCH
)
606 // nothing more to do
609 if ( opt
.flags
& wxCMD_LINE_OPTION_HELP
)
611 helpRequested
= TRUE
;
613 // it's not an error, but we still stop here
621 // +1 for leading '-'
622 const wxChar
*p
= arg
.c_str() + 1 + name
.length();
625 p
++; // for another leading '-'
627 if ( *p
++ != _T('=') )
629 wxLogError(_("Option '%s' requires a value, '=' expected."), name
.c_str());
645 // the value is in the next argument
648 // ... but there is none
649 wxLogError(_("Option '%s' requires a value."),
656 // ... take it from there
657 p
= m_data
->m_arguments
[n
].c_str();
662 // the value is right here: this may be legal or
663 // not depending on the option style
664 if ( opt
.flags
& wxCMD_LINE_NEEDS_SEPARATOR
)
666 wxLogError(_("Separator expected after the option '%s'."),
680 wxFAIL_MSG( _T("unknown option type") );
681 // still fall through
683 case wxCMD_LINE_VAL_STRING
:
684 opt
.SetStrVal(value
);
687 case wxCMD_LINE_VAL_NUMBER
:
690 if ( value
.ToLong(&val
) )
696 wxLogError(_("'%s' is not a correct numeric value for option '%s'."),
697 value
.c_str(), name
.c_str());
704 case wxCMD_LINE_VAL_DATE
:
707 const wxChar
*res
= dt
.ParseDate(value
);
710 wxLogError(_("Option '%s': '%s' cannot be converted to a date."),
711 name
.c_str(), value
.c_str());
728 if ( currentParam
< countParam
)
730 wxCmdLineParam
& param
= m_data
->m_paramDesc
[currentParam
];
732 // TODO check the param type
734 m_data
->m_parameters
.Add(arg
);
736 if ( !(param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
) )
742 wxASSERT_MSG( currentParam
== countParam
- 1,
743 _T("all parameters after the one with wxCMD_LINE_PARAM_MULTIPLE style are ignored") );
745 // remember that we did have this last repeatable parameter
746 hadRepeatableParam
= TRUE
;
751 wxLogError(_("Unexpected parameter '%s'"), arg
.c_str());
758 // verify that all mandatory options were given
761 size_t countOpt
= m_data
->m_options
.GetCount();
762 for ( size_t n
= 0; ok
&& (n
< countOpt
); n
++ )
764 wxCmdLineOption
& opt
= m_data
->m_options
[n
];
765 if ( (opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) && !opt
.HasValue() )
770 optName
= opt
.shortName
;
774 optName
.Printf(_("%s (or %s)"),
775 opt
.shortName
.c_str(),
776 opt
.longName
.c_str());
779 wxLogError(_("The value for the option '%s' must be specified."),
786 for ( ; ok
&& (currentParam
< countParam
); currentParam
++ )
788 wxCmdLineParam
& param
= m_data
->m_paramDesc
[currentParam
];
789 if ( (currentParam
== countParam
- 1) &&
790 (param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
) &&
793 // special case: currentParam wasn't incremented, but we did
794 // have it, so don't give error
798 if ( !(param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
) )
800 wxLogError(_("The required parameter '%s' was not specified."),
801 param
.description
.c_str());
808 if ( !ok
&& showUsage
)
813 return ok
? 0 : helpRequested
? -1 : 1;
816 // ----------------------------------------------------------------------------
817 // give the usage message
818 // ----------------------------------------------------------------------------
820 void wxCmdLineParser::Usage()
822 wxString appname
= wxTheApp
->GetAppName();
825 wxCHECK_RET( !m_data
->m_arguments
.IsEmpty(), _T("no program name") );
827 appname
= wxFileNameFromPath(m_data
->m_arguments
[0]);
828 wxStripExtension(appname
);
831 // we construct the brief cmd line desc on the fly, but not the detailed
832 // help message below because we want to align the options descriptions
833 // and for this we must first know the longest one of them
835 wxArrayString namesOptions
, descOptions
;
836 brief
.Printf(_("Usage: %s"), appname
.c_str());
838 // the switch char is usually '-' but this can be changed with
839 // SetSwitchChars() and then the first one of possible chars is used
840 wxChar chSwitch
= !m_data
->m_switchChars
? _T('-')
841 : m_data
->m_switchChars
[0u];
843 size_t n
, count
= m_data
->m_options
.GetCount();
844 for ( n
= 0; n
< count
; n
++ )
846 wxCmdLineOption
& opt
= m_data
->m_options
[n
];
849 if ( !(opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) )
854 if ( !opt
.shortName
.empty() )
856 brief
<< chSwitch
<< opt
.shortName
;
858 else if ( !opt
.longName
.empty() )
860 brief
<< _T("--") << opt
.longName
;
864 wxFAIL_MSG( _T("option without neither short nor long name?") );
869 if ( !opt
.shortName
.empty() )
871 option
<< _T(" ") << chSwitch
<< opt
.shortName
;
874 if ( !opt
.longName
.empty() )
876 option
<< (option
.empty() ? _T(" ") : _T(", "))
877 << _T("--") << opt
.longName
;
880 if ( opt
.kind
!= wxCMD_LINE_SWITCH
)
883 val
<< _T('<') << GetTypeName(opt
.type
) << _T('>');
884 brief
<< _T(' ') << val
;
885 option
<< (!opt
.longName
? _T(':') : _T('=')) << val
;
888 if ( !(opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) )
893 namesOptions
.Add(option
);
894 descOptions
.Add(opt
.description
);
897 count
= m_data
->m_paramDesc
.GetCount();
898 for ( n
= 0; n
< count
; n
++ )
900 wxCmdLineParam
& param
= m_data
->m_paramDesc
[n
];
903 if ( param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
)
908 brief
<< param
.description
;
910 if ( param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
)
915 if ( param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
)
921 if ( !!m_data
->m_logo
)
923 wxLogMessage(m_data
->m_logo
);
926 // in console mode we want to show the brief usage message first, then the
927 // detailed one but in GUI build we give the details first and then the
928 // summary - like this, the brief message appears in the wxLogGui dialog,
934 // now construct the detailed help message
935 size_t len
, lenMax
= 0;
936 count
= namesOptions
.GetCount();
937 for ( n
= 0; n
< count
; n
++ )
939 len
= namesOptions
[n
].length();
945 for ( n
= 0; n
< count
; n
++ )
947 len
= namesOptions
[n
].length();
948 detailed
<< namesOptions
[n
]
949 << wxString(_T(' '), lenMax
- len
) << _T('\t')
954 wxLogMessage(detailed
);
956 // do it now if not done above
962 // ----------------------------------------------------------------------------
964 // ----------------------------------------------------------------------------
966 static wxString
GetTypeName(wxCmdLineParamType type
)
972 wxFAIL_MSG( _T("unknown option type") );
973 // still fall through
975 case wxCMD_LINE_VAL_STRING
: s
= _("str"); break;
976 case wxCMD_LINE_VAL_NUMBER
: s
= _("num"); break;
977 case wxCMD_LINE_VAL_DATE
: s
= _("date"); break;
983 #endif // wxUSE_CMDLINE_PARSER
985 // ----------------------------------------------------------------------------
987 // ----------------------------------------------------------------------------
990 wxArrayString
wxCmdLineParser::ConvertStringToArgs(const wxChar
*p
)
997 bool isInsideQuotes
= FALSE
;
1001 while ( *p
== _T(' ') || *p
== _T('\t') )
1005 if ( *p
== _T('\0') )
1008 // parse this parameter
1012 // do we have a (lone) backslash?
1013 bool isQuotedByBS
= FALSE
;
1014 while ( *p
== _T('\\') )
1018 // if we have 2 backslashes in a row, output one
1022 isQuotedByBS
= FALSE
;
1024 else // the next char is quoted
1026 isQuotedByBS
= TRUE
;
1030 bool skipChar
= FALSE
,
1035 if ( !isQuotedByBS
)
1037 // don't put the quote itself in the arg
1040 isInsideQuotes
= !isInsideQuotes
;
1042 //else: insert a literal quote
1048 if ( isInsideQuotes
|| isQuotedByBS
)
1050 // preserve it, skip endParam below
1053 //else: fall through
1062 // ignore backslash before an ordinary character - this
1063 // is needed to properly handle the file names under
1064 // Windows appearing in the command line
1073 // otherwise copy this char to arg