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"
32 #include "wx/string.h"
36 #include "wx/dynarray.h"
37 #include "wx/filefn.h"
42 #include "wx/datetime.h"
43 #include "wx/cmdline.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 static wxString
GetTypeName(wxCmdLineParamType type
);
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // an internal representation of an option
56 struct wxCmdLineOption
58 wxCmdLineOption(wxCmdLineEntryType k
,
62 wxCmdLineParamType typ
,
77 // can't use union easily here, so just store all possible data fields, we
78 // don't waste much (might still use union later if the number of supported
79 // types increases, so always use the accessor functions and don't access
80 // the fields directly!)
82 void Check(wxCmdLineParamType
WXUNUSED_UNLESS_DEBUG(typ
)) const
84 wxASSERT_MSG( type
== typ
, _T("type mismatch in wxCmdLineOption") );
87 long GetLongVal() const
88 { Check(wxCMD_LINE_VAL_NUMBER
); return m_longVal
; }
89 const wxString
& GetStrVal() const
90 { Check(wxCMD_LINE_VAL_STRING
); return m_strVal
; }
91 const wxDateTime
& GetDateVal() const
92 { Check(wxCMD_LINE_VAL_DATE
); return m_dateVal
; }
94 void SetLongVal(long val
)
95 { Check(wxCMD_LINE_VAL_NUMBER
); m_longVal
= val
; m_hasVal
= TRUE
; }
96 void SetStrVal(const wxString
& val
)
97 { Check(wxCMD_LINE_VAL_STRING
); m_strVal
= val
; m_hasVal
= TRUE
; }
98 void SetDateVal(const wxDateTime val
)
99 { Check(wxCMD_LINE_VAL_DATE
); m_dateVal
= val
; m_hasVal
= TRUE
; }
101 void SetHasValue() { m_hasVal
= TRUE
; }
102 bool HasValue() const { return m_hasVal
; }
105 wxCmdLineEntryType kind
;
106 wxString shortName
, longName
, description
;
107 wxCmdLineParamType type
;
115 wxDateTime m_dateVal
;
118 struct wxCmdLineParam
120 wxCmdLineParam(const wxString
& desc
,
121 wxCmdLineParamType typ
,
129 wxString description
;
130 wxCmdLineParamType type
;
134 WX_DECLARE_OBJARRAY(wxCmdLineOption
, wxArrayOptions
);
135 WX_DECLARE_OBJARRAY(wxCmdLineParam
, wxArrayParams
);
137 #include "wx/arrimpl.cpp"
139 WX_DEFINE_OBJARRAY(wxArrayOptions
);
140 WX_DEFINE_OBJARRAY(wxArrayParams
);
142 // the parser internal state
143 struct wxCmdLineParserData
146 wxString m_switchChars
; // characters which may start an option
147 bool m_enableLongOptions
; // TRUE if long options are enabled
148 wxString m_logo
; // some extra text to show in Usage()
151 wxArrayString m_arguments
; // == argv, argc == m_arguments.GetCount()
152 wxArrayOptions m_options
; // all possible options and switchrs
153 wxArrayParams m_paramDesc
; // description of all possible params
154 wxArrayString m_parameters
; // all params found
157 wxCmdLineParserData();
158 void SetArguments(int argc
, char **argv
);
159 void SetArguments(const wxString
& cmdline
);
161 int FindOption(const wxString
& name
);
162 int FindOptionByLongName(const wxString
& name
);
165 // ============================================================================
167 // ============================================================================
169 // ----------------------------------------------------------------------------
170 // wxCmdLineParserData
171 // ----------------------------------------------------------------------------
173 wxCmdLineParserData::wxCmdLineParserData()
175 m_enableLongOptions
= TRUE
;
177 m_switchChars
= _T("-");
179 m_switchChars
= _T("/-");
183 void wxCmdLineParserData::SetArguments(int argc
, char **argv
)
187 for ( int n
= 0; n
< argc
; n
++ )
189 m_arguments
.Add(argv
[n
]);
193 void wxCmdLineParserData::SetArguments(const wxString
& WXUNUSED(cmdline
))
195 // either use wxMSW wxApp::ConvertToStandardCommandArgs() or move its logic
196 // here and use this method from it - but don't duplicate the code
198 wxFAIL_MSG(_T("TODO"));
201 int wxCmdLineParserData::FindOption(const wxString
& name
)
203 size_t count
= m_options
.GetCount();
204 for ( size_t n
= 0; n
< count
; n
++ )
206 if ( m_options
[n
].shortName
== name
)
216 int wxCmdLineParserData::FindOptionByLongName(const wxString
& name
)
218 size_t count
= m_options
.GetCount();
219 for ( size_t n
= 0; n
< count
; n
++ )
221 if ( m_options
[n
].longName
== name
)
231 // ----------------------------------------------------------------------------
232 // construction and destruction
233 // ----------------------------------------------------------------------------
235 void wxCmdLineParser::Init()
237 m_data
= new wxCmdLineParserData
;
240 void wxCmdLineParser::SetCmdLine(int argc
, char **argv
)
242 m_data
->SetArguments(argc
, argv
);
245 void wxCmdLineParser::SetCmdLine(const wxString
& cmdline
)
247 m_data
->SetArguments(cmdline
);
250 wxCmdLineParser::~wxCmdLineParser()
255 // ----------------------------------------------------------------------------
257 // ----------------------------------------------------------------------------
259 void wxCmdLineParser::SetSwitchChars(const wxString
& switchChars
)
261 m_data
->m_switchChars
= switchChars
;
264 void wxCmdLineParser::EnableLongOptions(bool enable
)
266 m_data
->m_enableLongOptions
= enable
;
269 void wxCmdLineParser::SetLogo(const wxString
& logo
)
271 m_data
->m_logo
= logo
;
274 // ----------------------------------------------------------------------------
275 // command line construction
276 // ----------------------------------------------------------------------------
278 void wxCmdLineParser::SetDesc(const wxCmdLineEntryDesc
*desc
)
282 switch ( desc
->kind
)
284 case wxCMD_LINE_SWITCH
:
285 AddSwitch(desc
->shortName
, desc
->longName
, desc
->description
,
289 case wxCMD_LINE_OPTION
:
290 AddOption(desc
->shortName
, desc
->longName
, desc
->description
,
291 desc
->type
, desc
->flags
);
294 case wxCMD_LINE_PARAM
:
295 AddParam(desc
->description
, desc
->type
, desc
->flags
);
299 wxFAIL_MSG( _T("unknown command line entry type") );
300 // still fall through
302 case wxCMD_LINE_NONE
:
308 void wxCmdLineParser::AddSwitch(const wxString
& shortName
,
309 const wxString
& longName
,
310 const wxString
& desc
,
313 wxASSERT_MSG( m_data
->FindOption(shortName
) == wxNOT_FOUND
,
314 _T("duplicate switch") );
316 wxCmdLineOption
*option
= new wxCmdLineOption(wxCMD_LINE_SWITCH
,
317 shortName
, longName
, desc
,
318 wxCMD_LINE_VAL_NONE
, flags
);
320 m_data
->m_options
.Add(option
);
323 void wxCmdLineParser::AddOption(const wxString
& shortName
,
324 const wxString
& longName
,
325 const wxString
& desc
,
326 wxCmdLineParamType type
,
329 wxASSERT_MSG( m_data
->FindOption(shortName
) == wxNOT_FOUND
,
330 _T("duplicate option") );
332 wxCmdLineOption
*option
= new wxCmdLineOption(wxCMD_LINE_OPTION
,
333 shortName
, longName
, desc
,
336 m_data
->m_options
.Add(option
);
339 void wxCmdLineParser::AddParam(const wxString
& desc
,
340 wxCmdLineParamType type
,
343 // do some consistency checks: a required parameter can't follow an
344 // optional one and nothing should follow a parameter with MULTIPLE flag
346 if ( !m_data
->m_paramDesc
.IsEmpty() )
348 wxCmdLineParam
& param
= m_data
->m_paramDesc
.Last();
350 wxASSERT_MSG( !(param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
),
351 _T("all parameters after the one with wxCMD_LINE_PARAM_MULTIPLE style will be ignored") );
353 if ( !(flags
& wxCMD_LINE_PARAM_OPTIONAL
) )
355 wxASSERT_MSG( !(param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
),
356 _T("a required parameter can't follow an optional one") );
361 wxCmdLineParam
*param
= new wxCmdLineParam(desc
, type
, flags
);
363 m_data
->m_paramDesc
.Add(param
);
366 // ----------------------------------------------------------------------------
367 // access to parse command line
368 // ----------------------------------------------------------------------------
370 bool wxCmdLineParser::Found(const wxString
& name
) const
372 int i
= m_data
->FindOption(name
);
373 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown switch") );
375 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
376 if ( !opt
.HasValue() )
382 bool wxCmdLineParser::Found(const wxString
& name
, wxString
*value
) const
384 int i
= m_data
->FindOption(name
);
385 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
387 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
388 if ( !opt
.HasValue() )
391 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
393 *value
= opt
.GetStrVal();
398 bool wxCmdLineParser::Found(const wxString
& name
, long *value
) const
400 int i
= m_data
->FindOption(name
);
401 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
403 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
404 if ( !opt
.HasValue() )
407 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
409 *value
= opt
.GetLongVal();
414 bool wxCmdLineParser::Found(const wxString
& name
, wxDateTime
*value
) const
416 int i
= m_data
->FindOption(name
);
417 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
419 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
420 if ( !opt
.HasValue() )
423 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
425 *value
= opt
.GetDateVal();
430 size_t wxCmdLineParser::GetParamCount() const
432 return m_data
->m_parameters
.GetCount();
435 wxString
wxCmdLineParser::GetParam(size_t n
) const
437 return m_data
->m_parameters
[n
];
440 // ----------------------------------------------------------------------------
441 // the real work is done here
442 // ----------------------------------------------------------------------------
444 int wxCmdLineParser::Parse()
446 bool maybeOption
= TRUE
; // can the following arg be an option?
447 bool ok
= TRUE
; // TRUE until an error is detected
448 bool helpRequested
= FALSE
; // TRUE if "-h" was given
449 bool hadRepeatableParam
= FALSE
; // TRUE if found param with MULTIPLE flag
451 size_t currentParam
= 0; // the index in m_paramDesc
453 size_t countParam
= m_data
->m_paramDesc
.GetCount();
457 size_t count
= m_data
->m_arguments
.GetCount();
458 for ( size_t n
= 1; ok
&& (n
< count
); n
++ ) // 0 is program name
460 arg
= m_data
->m_arguments
[n
];
462 // special case: "--" should be discarded and all following arguments
463 // should be considered as parameters, even if they start with '-' and
464 // not like options (this is POSIX-like)
465 if ( arg
== _T("--") )
472 // empty argument or just '-' is not an option but a parameter
473 if ( maybeOption
&& arg
.length() > 1 &&
474 wxStrchr(m_data
->m_switchChars
, arg
[0u]) )
478 int optInd
= wxNOT_FOUND
; // init to suppress warnings
480 // an option or a switch: find whether it's a long or a short one
481 if ( m_data
->m_enableLongOptions
&&
482 arg
[0u] == _T('-') && arg
[1u] == _T('-') )
487 const wxChar
*p
= arg
.c_str() + 2;
488 while ( wxIsalnum(*p
) || (*p
== _T('_')) || (*p
== _T('-')) )
493 optInd
= m_data
->FindOptionByLongName(name
);
494 if ( optInd
== wxNOT_FOUND
)
496 wxLogError(_("Unknown long option '%s'"), name
.c_str());
503 // a short one: as they can be cumulated, we try to find the
504 // longest substring which is a valid option
505 const wxChar
*p
= arg
.c_str() + 1;
506 while ( wxIsalnum(*p
) || (*p
== _T('_')) )
511 size_t len
= name
.length();
516 // we couldn't find a valid option name in the
517 // beginning of this string
518 wxLogError(_("Unknown option '%s'"), name
.c_str());
524 optInd
= m_data
->FindOption(name
.Left(len
));
526 // will try with one character less the next time
530 while ( optInd
== wxNOT_FOUND
);
532 len
++; // compensates extra len-- above
533 if ( (optInd
!= wxNOT_FOUND
) && (len
!= name
.length()) )
535 // first of all, the option name is only part of this
537 name
= name
.Left(len
);
539 // our option is only part of this argument, there is
540 // something else in it - it is either the value of this
541 // option or other switches if it is a switch
542 if ( m_data
->m_options
[(size_t)optInd
].kind
543 == wxCMD_LINE_SWITCH
)
545 // pretend that all the rest of the argument is the
546 // next argument, in fact
547 wxString arg2
= arg
[0u];
548 arg2
+= arg
.Mid(len
+ 1); // +1 for leading '-'
550 m_data
->m_arguments
.Insert(arg2
, n
+ 1);
553 //else: it's our value, we'll deal with it below
557 if ( optInd
== wxNOT_FOUND
)
561 continue; // will break, in fact
564 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)optInd
];
565 if ( opt
.kind
== wxCMD_LINE_SWITCH
)
567 // nothing more to do
570 if ( opt
.flags
& wxCMD_LINE_OPTION_HELP
)
572 helpRequested
= TRUE
;
574 // it's not an error, but we still stop here
582 // +1 for leading '-'
583 const wxChar
*p
= arg
.c_str() + 1 + name
.length();
586 p
++; // for another leading '-'
588 if ( *p
++ != _T('=') )
590 wxLogError(_("Option '%s' requires a value, '=' expected."), name
.c_str());
606 // the value is in the next argument
609 // ... but there is none
610 wxLogError(_("Option '%s' requires a value."),
617 // ... take it from there
618 p
= m_data
->m_arguments
[n
].c_str();
623 // the value is right here: this may be legal or
624 // not depending on the option style
625 if ( opt
.flags
& wxCMD_LINE_NEEDS_SEPARATOR
)
627 wxLogError(_("Separator expected after the option '%s'."),
641 wxFAIL_MSG( _T("unknown option type") );
642 // still fall through
644 case wxCMD_LINE_VAL_STRING
:
645 opt
.SetStrVal(value
);
648 case wxCMD_LINE_VAL_NUMBER
:
651 if ( value
.ToLong(&val
) )
657 wxLogError(_("'%s' is not a correct numeric value for option '%s'."),
658 value
.c_str(), name
.c_str());
665 case wxCMD_LINE_VAL_DATE
:
668 const wxChar
*res
= dt
.ParseDate(value
);
671 wxLogError(_("Option '%s': '%s' cannot be converted to a date."),
672 name
.c_str(), value
.c_str());
689 if ( currentParam
< countParam
)
691 wxCmdLineParam
& param
= m_data
->m_paramDesc
[currentParam
];
693 // TODO check the param type
695 m_data
->m_parameters
.Add(arg
);
697 if ( !(param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
) )
703 wxASSERT_MSG( currentParam
== countParam
- 1,
704 _T("all parameters after the one with wxCMD_LINE_PARAM_MULTIPLE style are ignored") );
706 // remember that we did have this last repeatable parameter
707 hadRepeatableParam
= TRUE
;
712 wxLogError(_("Unexpected parameter '%s'"), arg
.c_str());
719 // verify that all mandatory options were given
722 size_t countOpt
= m_data
->m_options
.GetCount();
723 for ( size_t n
= 0; ok
&& (n
< countOpt
); n
++ )
725 wxCmdLineOption
& opt
= m_data
->m_options
[n
];
726 if ( (opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) && !opt
.HasValue() )
731 optName
= opt
.shortName
;
735 optName
.Printf(_("%s (or %s)"),
736 opt
.shortName
.c_str(),
737 opt
.longName
.c_str());
740 wxLogError(_("The value for the option '%s' must be specified."),
747 for ( ; ok
&& (currentParam
< countParam
); currentParam
++ )
749 wxCmdLineParam
& param
= m_data
->m_paramDesc
[currentParam
];
750 if ( (currentParam
== countParam
- 1) &&
751 (param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
) &&
754 // special case: currentParam wasn't incremented, but we did
755 // have it, so don't give error
759 if ( !(param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
) )
761 wxLogError(_("The required parameter '%s' was not specified."),
762 param
.description
.c_str());
774 return ok
? 0 : helpRequested
? -1 : 1;
777 // ----------------------------------------------------------------------------
778 // give the usage message
779 // ----------------------------------------------------------------------------
781 void wxCmdLineParser::Usage()
783 wxString appname
= wxTheApp
->GetAppName();
786 wxCHECK_RET( !m_data
->m_arguments
.IsEmpty(), _T("no program name") );
788 appname
= wxFileNameFromPath(m_data
->m_arguments
[0]);
789 wxStripExtension(appname
);
792 // we construct the brief cmd line desc on the fly, but not the detailed
793 // help message below because we want to align the options descriptions
794 // and for this we must first know the longest one of them
796 wxArrayString namesOptions
, descOptions
;
797 brief
.Printf(_("Usage: %s"), appname
.c_str());
799 // the switch char is usually '-' but this can be changed with
800 // SetSwitchChars() and then the first one of possible chars is used
801 wxChar chSwitch
= !m_data
->m_switchChars
? _T('-')
802 : m_data
->m_switchChars
[0u];
804 size_t n
, count
= m_data
->m_options
.GetCount();
805 for ( n
= 0; n
< count
; n
++ )
807 wxCmdLineOption
& opt
= m_data
->m_options
[n
];
810 if ( !(opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) )
815 brief
<< chSwitch
<< opt
.shortName
;
818 option
<< _T(" ") << chSwitch
<< opt
.shortName
;
819 if ( !!opt
.longName
)
821 option
<< _T(" --") << opt
.longName
;
824 if ( opt
.kind
!= wxCMD_LINE_SWITCH
)
827 val
<< _T('<') << GetTypeName(opt
.type
) << _T('>');
828 brief
<< _T(' ') << val
;
829 option
<< (!opt
.longName
? _T(':') : _T('=')) << val
;
832 if ( !(opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) )
837 namesOptions
.Add(option
);
838 descOptions
.Add(opt
.description
);
841 count
= m_data
->m_paramDesc
.GetCount();
842 for ( n
= 0; n
< count
; n
++ )
844 wxCmdLineParam
& param
= m_data
->m_paramDesc
[n
];
847 if ( param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
)
852 brief
<< param
.description
;
854 if ( param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
)
859 if ( param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
)
865 if ( !!m_data
->m_logo
)
867 wxLogMessage(m_data
->m_logo
);
872 // now construct the detailed help message
873 size_t len
, lenMax
= 0;
874 count
= namesOptions
.GetCount();
875 for ( n
= 0; n
< count
; n
++ )
877 len
= namesOptions
[n
].length();
883 for ( n
= 0; n
< count
; n
++ )
885 len
= namesOptions
[n
].length();
886 detailed
<< namesOptions
[n
]
887 << wxString(_T(' '), lenMax
- len
) << _T('\t')
892 wxLogMessage(detailed
);
895 // ----------------------------------------------------------------------------
897 // ----------------------------------------------------------------------------
899 static wxString
GetTypeName(wxCmdLineParamType type
)
905 wxFAIL_MSG( _T("unknown option type") );
906 // still fall through
908 case wxCMD_LINE_VAL_STRING
: s
= _("str"); break;
909 case wxCMD_LINE_VAL_NUMBER
: s
= _("num"); break;
910 case wxCMD_LINE_VAL_DATE
: s
= _("date"); break;