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"
40 #include "wx/datetime.h"
41 #include "wx/cmdline.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 static wxString
GetTypeName(wxCmdLineParamType type
);
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // an internal representation of an option
54 struct wxCmdLineOption
56 wxCmdLineOption(wxCmdLineEntryType k
,
60 wxCmdLineParamType typ
,
75 // can't use union easily here, so just store all possible data fields, we
76 // don't waste much (might still use union later if the number of supported
77 // types increases, so always use the accessor functions and don't access
78 // the fields directly!)
80 void Check(wxCmdLineParamType typ
) const
82 wxASSERT_MSG( type
== typ
, _T("type mismatch in wxCmdLineOption") );
85 long GetLongVal() const
86 { Check(wxCMD_LINE_VAL_NUMBER
); return m_longVal
; }
87 const wxString
& GetStrVal() const
88 { Check(wxCMD_LINE_VAL_STRING
); return m_strVal
; }
89 const wxDateTime
& GetDateVal() const
90 { Check(wxCMD_LINE_VAL_DATE
); return m_dateVal
; }
92 void SetLongVal(long val
)
93 { Check(wxCMD_LINE_VAL_NUMBER
); m_longVal
= val
; m_hasVal
= TRUE
; }
94 void SetStrVal(const wxString
& val
)
95 { Check(wxCMD_LINE_VAL_STRING
); m_strVal
= val
; m_hasVal
= TRUE
; }
96 void SetDateVal(const wxDateTime val
)
97 { Check(wxCMD_LINE_VAL_DATE
); m_dateVal
= val
; m_hasVal
= TRUE
; }
99 void SetHasValue() { m_hasVal
= TRUE
; }
100 bool HasValue() const { return m_hasVal
; }
103 wxCmdLineEntryType kind
;
104 wxString shortName
, longName
, description
;
105 wxCmdLineParamType type
;
113 wxDateTime m_dateVal
;
116 struct wxCmdLineParam
118 wxCmdLineParam(const wxString
& desc
,
119 wxCmdLineParamType typ
,
127 wxString description
;
128 wxCmdLineParamType type
;
132 WX_DECLARE_OBJARRAY(wxCmdLineOption
, wxArrayOptions
);
133 WX_DECLARE_OBJARRAY(wxCmdLineParam
, wxArrayParams
);
135 #include "wx/arrimpl.cpp"
137 WX_DEFINE_OBJARRAY(wxArrayOptions
);
138 WX_DEFINE_OBJARRAY(wxArrayParams
);
140 // the parser internal state
141 struct wxCmdLineParserData
144 wxString m_switchChars
; // characters which may start an option
146 bool m_enableLongOptions
; // TRUE if long options are enabled
149 wxArrayString m_arguments
; // == argv, argc == m_arguments.GetCount()
150 wxArrayOptions m_options
; // all possible options and switchrs
151 wxArrayParams m_paramDesc
; // description of all possible params
152 wxArrayString m_parameters
; // all params found
155 wxCmdLineParserData();
156 void SetArguments(int argc
, char **argv
);
157 void SetArguments(const wxString
& cmdline
);
159 int FindOption(const wxString
& name
);
160 int FindOptionByLongName(const wxString
& name
);
163 // ============================================================================
165 // ============================================================================
167 // ----------------------------------------------------------------------------
168 // wxCmdLineParserData
169 // ----------------------------------------------------------------------------
171 wxCmdLineParserData::wxCmdLineParserData()
173 m_enableLongOptions
= TRUE
;
175 m_switchChars
= _T("-");
177 m_switchChars
= _T("/-");
181 void wxCmdLineParserData::SetArguments(int argc
, char **argv
)
185 for ( int n
= 0; n
< argc
; n
++ )
187 m_arguments
.Add(argv
[n
]);
191 void wxCmdLineParserData::SetArguments(const wxString
& cmdline
)
193 // either use wxMSW wxApp::ConvertToStandardCommandArgs() or move its logic
194 // here and use this method from it - but don't duplicate the code
196 wxFAIL_MSG(_T("TODO"));
199 int wxCmdLineParserData::FindOption(const wxString
& name
)
201 size_t count
= m_options
.GetCount();
202 for ( size_t n
= 0; n
< count
; n
++ )
204 if ( m_options
[n
].shortName
== name
)
214 int wxCmdLineParserData::FindOptionByLongName(const wxString
& name
)
216 size_t count
= m_options
.GetCount();
217 for ( size_t n
= 0; n
< count
; n
++ )
219 if ( m_options
[n
].longName
== name
)
229 // ----------------------------------------------------------------------------
230 // construction and destruction
231 // ----------------------------------------------------------------------------
233 void wxCmdLineParser::Init()
235 m_data
= new wxCmdLineParserData
;
238 void wxCmdLineParser::SetCmdLine(int argc
, char **argv
)
240 m_data
->SetArguments(argc
, argv
);
243 void wxCmdLineParser::SetCmdLine(const wxString
& cmdline
)
245 m_data
->SetArguments(cmdline
);
248 wxCmdLineParser::~wxCmdLineParser()
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
257 void wxCmdLineParser::SetSwitchChars(const wxString
& switchChars
)
259 m_data
->m_switchChars
= switchChars
;
262 void wxCmdLineParser::EnableLongOptions(bool enable
)
264 m_data
->m_enableLongOptions
= enable
;
267 // ----------------------------------------------------------------------------
268 // command line construction
269 // ----------------------------------------------------------------------------
271 void wxCmdLineParser::SetDesc(const wxCmdLineEntryDesc
*desc
)
275 switch ( desc
->kind
)
277 case wxCMD_LINE_SWITCH
:
278 AddSwitch(desc
->shortName
, desc
->longName
, desc
->description
);
281 case wxCMD_LINE_OPTION
:
282 AddOption(desc
->shortName
, desc
->longName
, desc
->description
,
283 desc
->type
, desc
->flags
);
286 case wxCMD_LINE_PARAM
:
287 AddParam(desc
->description
, desc
->type
, desc
->flags
);
291 wxFAIL_MSG( _T("unknown command line entry type") );
292 // still fall through
294 case wxCMD_LINE_NONE
:
300 void wxCmdLineParser::AddSwitch(const wxString
& shortName
,
301 const wxString
& longName
,
302 const wxString
& desc
,
305 wxASSERT_MSG( m_data
->FindOption(shortName
) == wxNOT_FOUND
,
306 _T("duplicate switch") );
308 wxCmdLineOption
*option
= new wxCmdLineOption(wxCMD_LINE_SWITCH
,
309 shortName
, longName
, desc
,
310 wxCMD_LINE_VAL_NONE
, flags
);
312 m_data
->m_options
.Add(option
);
315 void wxCmdLineParser::AddOption(const wxString
& shortName
,
316 const wxString
& longName
,
317 const wxString
& desc
,
318 wxCmdLineParamType type
,
321 wxASSERT_MSG( m_data
->FindOption(shortName
) == wxNOT_FOUND
,
322 _T("duplicate option") );
324 wxCmdLineOption
*option
= new wxCmdLineOption(wxCMD_LINE_OPTION
,
325 shortName
, longName
, desc
,
328 m_data
->m_options
.Add(option
);
331 void wxCmdLineParser::AddParam(const wxString
& desc
,
332 wxCmdLineParamType type
,
335 // do some consistency checks: a required parameter can't follow an
336 // optional one and nothing should follow a parameter with MULTIPLE flag
338 if ( !m_data
->m_paramDesc
.IsEmpty() )
340 wxCmdLineParam
& param
= m_data
->m_paramDesc
.Last();
342 wxASSERT_MSG( !(param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
),
343 _T("all parameters after the one with "
344 "wxCMD_LINE_PARAM_MULTIPLE style will be ignored") );
346 if ( !(flags
& wxCMD_LINE_PARAM_OPTIONAL
) )
348 wxASSERT_MSG( !(param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
),
349 _T("a required parameter can't follow an "
355 wxCmdLineParam
*param
= new wxCmdLineParam(desc
, type
, flags
);
357 m_data
->m_paramDesc
.Add(param
);
360 // ----------------------------------------------------------------------------
361 // access to parse command line
362 // ----------------------------------------------------------------------------
364 bool wxCmdLineParser::Found(const wxString
& name
) const
366 int i
= m_data
->FindOption(name
);
367 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown switch") );
369 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
370 if ( !opt
.HasValue() )
376 bool wxCmdLineParser::Found(const wxString
& name
, wxString
*value
) const
378 int i
= m_data
->FindOption(name
);
379 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
381 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
382 if ( !opt
.HasValue() )
385 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
387 *value
= opt
.GetStrVal();
392 bool wxCmdLineParser::Found(const wxString
& name
, long *value
) const
394 int i
= m_data
->FindOption(name
);
395 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
397 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
398 if ( !opt
.HasValue() )
401 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
403 *value
= opt
.GetLongVal();
408 bool wxCmdLineParser::Found(const wxString
& name
, wxDateTime
*value
) const
410 int i
= m_data
->FindOption(name
);
411 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
413 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
414 if ( !opt
.HasValue() )
417 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
419 *value
= opt
.GetDateVal();
424 size_t wxCmdLineParser::GetParamCount() const
426 return m_data
->m_parameters
.GetCount();
429 wxString
wxCmdLineParser::GetParam(size_t n
) const
431 return m_data
->m_parameters
[n
];
434 // ----------------------------------------------------------------------------
435 // the real work is done here
436 // ----------------------------------------------------------------------------
438 int wxCmdLineParser::Parse()
440 bool maybeOption
= TRUE
; // can the following arg be an option?
441 bool ok
= TRUE
; // TRUE until an error is detected
442 bool helpRequested
= FALSE
; // TRUE if "-h" was given
443 bool hadRepeatableParam
= FALSE
; // TRUE if found param with MULTIPLE flag
445 size_t currentParam
= 0; // the index in m_paramDesc
447 size_t countParam
= m_data
->m_paramDesc
.GetCount();
451 size_t count
= m_data
->m_arguments
.GetCount();
452 for ( size_t n
= 1; ok
&& (n
< count
); n
++ ) // 0 is program name
454 arg
= m_data
->m_arguments
[n
];
456 // special case: "--" should be discarded and all following arguments
457 // should be considered as parameters, even if they start with '-' and
458 // not like options (this is POSIX-like)
459 if ( arg
== _T("--") )
466 // empty argument or just '-' is not an option but a parameter
467 if ( maybeOption
&& arg
.length() > 1 &&
468 wxStrchr(m_data
->m_switchChars
, arg
[0u]) )
472 int optInd
= wxNOT_FOUND
; // init to suppress warnings
474 // an option or a switch: find whether it's a long or a short one
475 if ( m_data
->m_enableLongOptions
&&
476 arg
[0u] == _T('-') && arg
[1u] == _T('-') )
481 const wxChar
*p
= arg
.c_str() + 2;
482 while ( wxIsalpha(*p
) || (*p
== _T('-')) )
487 optInd
= m_data
->FindOptionByLongName(name
);
488 if ( optInd
== wxNOT_FOUND
)
490 wxLogError(_("Unknown long option '%s'"), name
.c_str());
497 // a short one: as they can be cumulated, we try to find the
498 // longest substring which is a valid option
499 const wxChar
*p
= arg
.c_str() + 1;
500 while ( wxIsalpha(*p
) )
505 size_t len
= name
.length();
510 // we couldn't find a valid option name in the
511 // beginning of this string
512 wxLogError(_("Unknown option '%s'"), name
.c_str());
518 optInd
= m_data
->FindOption(name
.Left(len
));
520 // will try with one character less the next time
524 while ( optInd
== wxNOT_FOUND
);
526 len
++; // compensates extra len-- above
527 if ( (optInd
!= wxNOT_FOUND
) && (len
!= name
.length()) )
529 // first of all, the option name is only part of this
531 name
= name
.Left(len
);
533 // our option is only part of this argument, there is
534 // something else in it - it is either the value of this
535 // option or other switches if it is a switch
536 if ( m_data
->m_options
[(size_t)optInd
].kind
537 == wxCMD_LINE_SWITCH
)
539 // pretend that all the rest of the argument is the
540 // next argument, in fact
541 wxString arg2
= arg
[0u];
542 arg2
+= arg
.Mid(len
+ 1); // +1 for leading '-'
544 m_data
->m_arguments
.Insert(arg2
, n
+ 1);
547 //else: it's our value, we'll deal with it below
551 if ( optInd
== wxNOT_FOUND
)
555 continue; // will break, in fact
558 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)optInd
];
559 if ( opt
.kind
== wxCMD_LINE_SWITCH
)
561 // nothing more to do
564 if ( opt
.flags
& wxCMD_LINE_OPTION_HELP
)
566 helpRequested
= TRUE
;
568 // it's not an error, but we still stop here
576 // +1 for leading '-'
577 const wxChar
*p
= arg
.c_str() + 1 + name
.length();
580 p
++; // for another leading '-'
582 if ( *p
++ != _T('=') )
584 wxLogError(_("Option '%s' requires a value, '=' "
585 "expected."), name
.c_str());
600 // the value is in the next argument
603 // ... but there is none
604 wxLogError(_("Option '%s' requires a value."),
611 // ... take it from there
612 p
= m_data
->m_arguments
[n
].c_str();
617 // the value is right here
628 wxFAIL_MSG( _T("unknown option type") );
629 // still fall through
631 case wxCMD_LINE_VAL_STRING
:
632 opt
.SetStrVal(value
);
635 case wxCMD_LINE_VAL_NUMBER
:
638 if ( value
.ToLong(&val
) )
644 wxLogError(_("'%s' is not a correct "
645 "numeric value for option "
647 value
.c_str(), name
.c_str());
654 case wxCMD_LINE_VAL_DATE
:
657 const wxChar
*res
= dt
.ParseDate(value
);
660 wxLogError(_("Option '%s': '%s' cannot "
661 "be converted to a date."),
662 name
.c_str(), value
.c_str());
679 if ( currentParam
< countParam
)
681 wxCmdLineParam
& param
= m_data
->m_paramDesc
[currentParam
];
683 // TODO check the param type
685 m_data
->m_parameters
.Add(arg
);
687 if ( !(param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
) )
693 wxASSERT_MSG( currentParam
== countParam
- 1,
694 _T("all parameters after the one with "
695 "wxCMD_LINE_PARAM_MULTIPLE style "
698 // remember that we did have this last repeatable parameter
699 hadRepeatableParam
= TRUE
;
704 wxLogError(_("Unexpected parameter '%s'"), arg
.c_str());
711 // verify that all mandatory options were given
714 size_t countOpt
= m_data
->m_options
.GetCount();
715 for ( size_t n
= 0; ok
&& (n
< countOpt
); n
++ )
717 wxCmdLineOption
& opt
= m_data
->m_options
[n
];
718 if ( (opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) && !opt
.HasValue() )
723 optName
= opt
.shortName
;
727 optName
.Printf(_("%s (or %s)"),
728 opt
.shortName
.c_str(),
729 opt
.longName
.c_str());
732 wxLogError(_("The value for the option '%s' must be specified."),
739 for ( ; ok
&& (currentParam
< countParam
); currentParam
++ )
741 wxCmdLineParam
& param
= m_data
->m_paramDesc
[currentParam
];
742 if ( (currentParam
== countParam
- 1) &&
743 (param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
) &&
746 // special case: currentParam wasn't incremented, but we did
747 // have it, so don't give error
751 if ( !(param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
) )
753 wxLogError(_("The required parameter '%s' was not specified."),
754 param
.description
.c_str());
766 return ok
? 0 : helpRequested
? -1 : 1;
769 // ----------------------------------------------------------------------------
770 // give the usage message
771 // ----------------------------------------------------------------------------
773 void wxCmdLineParser::Usage()
775 wxString appname
= wxTheApp
->GetAppName();
778 wxCHECK_RET( !m_data
->m_arguments
.IsEmpty(), _T("no program name") );
780 appname
= wxFileNameFromPath(m_data
->m_arguments
[0]);
781 wxStripExtension(appname
);
784 wxString brief
, detailed
;
785 brief
.Printf(_("Usage: %s"), appname
.c_str());
787 size_t n
, count
= m_data
->m_options
.GetCount();
788 for ( n
= 0; n
< count
; n
++ )
790 wxCmdLineOption
& opt
= m_data
->m_options
[n
];
793 if ( !(opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) )
798 brief
<< _T('-') << opt
.shortName
;
799 detailed
<< _T(" -") << opt
.shortName
;
800 if ( !!opt
.longName
)
802 detailed
<< _T(" --") << opt
.longName
;
805 if ( opt
.kind
!= wxCMD_LINE_SWITCH
)
808 val
<< _T('<') << GetTypeName(opt
.type
) << _T('>');
809 brief
<< _T(' ') << val
;
810 detailed
<< (!opt
.longName
? _T(':') : _T('=')) << val
;
813 if ( !(opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) )
818 detailed
<< _T('\t') << opt
.description
<< _T('\n');
821 count
= m_data
->m_paramDesc
.GetCount();
822 for ( n
= 0; n
< count
; n
++ )
824 wxCmdLineParam
& param
= m_data
->m_paramDesc
[n
];
827 if ( param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
)
832 brief
<< param
.description
;
834 if ( param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
)
839 if ( param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
)
846 wxLogMessage(detailed
);
849 // ----------------------------------------------------------------------------
851 // ----------------------------------------------------------------------------
853 static wxString
GetTypeName(wxCmdLineParamType type
)
859 wxFAIL_MSG( _T("unknown option type") );
860 // still fall through
862 case wxCMD_LINE_VAL_STRING
: s
= _("str"); break;
863 case wxCMD_LINE_VAL_NUMBER
: s
= _("num"); break;
864 case wxCMD_LINE_VAL_DATE
: s
= _("date"); break;