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(bool hasValue
= TRUE
) { m_hasVal
= hasValue
; }
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
& cmdLine
)
197 m_arguments
.Add(wxTheApp
->GetAppName());
200 // Treat strings enclosed in double-quotes as single arguments
202 int len
= cmdLine
.Length();
206 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
211 if (cmdLine
.GetChar(i
) == wxT('"')) // We found the start of a string
215 while ((i
< len
) && (cmdLine
.GetChar(i
) != wxT('"')))
218 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
220 m_arguments
.Add(arg
);
223 i
++; // Skip past 2nd quote
225 else // Unquoted argument
228 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
231 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
233 m_arguments
.Add(arg
);
239 int wxCmdLineParserData::FindOption(const wxString
& name
)
241 size_t count
= m_options
.GetCount();
242 for ( size_t n
= 0; n
< count
; n
++ )
244 if ( m_options
[n
].shortName
== name
)
254 int wxCmdLineParserData::FindOptionByLongName(const wxString
& name
)
256 size_t count
= m_options
.GetCount();
257 for ( size_t n
= 0; n
< count
; n
++ )
259 if ( m_options
[n
].longName
== name
)
269 // ----------------------------------------------------------------------------
270 // construction and destruction
271 // ----------------------------------------------------------------------------
273 void wxCmdLineParser::Init()
275 m_data
= new wxCmdLineParserData
;
278 void wxCmdLineParser::SetCmdLine(int argc
, char **argv
)
280 m_data
->SetArguments(argc
, argv
);
283 void wxCmdLineParser::SetCmdLine(const wxString
& cmdline
)
285 m_data
->SetArguments(cmdline
);
288 wxCmdLineParser::~wxCmdLineParser()
293 // ----------------------------------------------------------------------------
295 // ----------------------------------------------------------------------------
297 void wxCmdLineParser::SetSwitchChars(const wxString
& switchChars
)
299 m_data
->m_switchChars
= switchChars
;
302 void wxCmdLineParser::EnableLongOptions(bool enable
)
304 m_data
->m_enableLongOptions
= enable
;
307 void wxCmdLineParser::SetLogo(const wxString
& logo
)
309 m_data
->m_logo
= logo
;
312 // ----------------------------------------------------------------------------
313 // command line construction
314 // ----------------------------------------------------------------------------
316 void wxCmdLineParser::SetDesc(const wxCmdLineEntryDesc
*desc
)
320 switch ( desc
->kind
)
322 case wxCMD_LINE_SWITCH
:
323 AddSwitch(desc
->shortName
, desc
->longName
, desc
->description
,
327 case wxCMD_LINE_OPTION
:
328 AddOption(desc
->shortName
, desc
->longName
, desc
->description
,
329 desc
->type
, desc
->flags
);
332 case wxCMD_LINE_PARAM
:
333 AddParam(desc
->description
, desc
->type
, desc
->flags
);
337 wxFAIL_MSG( _T("unknown command line entry type") );
338 // still fall through
340 case wxCMD_LINE_NONE
:
346 void wxCmdLineParser::AddSwitch(const wxString
& shortName
,
347 const wxString
& longName
,
348 const wxString
& desc
,
351 wxASSERT_MSG( m_data
->FindOption(shortName
) == wxNOT_FOUND
,
352 _T("duplicate switch") );
354 wxCmdLineOption
*option
= new wxCmdLineOption(wxCMD_LINE_SWITCH
,
355 shortName
, longName
, desc
,
356 wxCMD_LINE_VAL_NONE
, flags
);
358 m_data
->m_options
.Add(option
);
361 void wxCmdLineParser::AddOption(const wxString
& shortName
,
362 const wxString
& longName
,
363 const wxString
& desc
,
364 wxCmdLineParamType type
,
367 wxASSERT_MSG( m_data
->FindOption(shortName
) == wxNOT_FOUND
,
368 _T("duplicate option") );
370 wxCmdLineOption
*option
= new wxCmdLineOption(wxCMD_LINE_OPTION
,
371 shortName
, longName
, desc
,
374 m_data
->m_options
.Add(option
);
377 void wxCmdLineParser::AddParam(const wxString
& desc
,
378 wxCmdLineParamType type
,
381 // do some consistency checks: a required parameter can't follow an
382 // optional one and nothing should follow a parameter with MULTIPLE flag
384 if ( !m_data
->m_paramDesc
.IsEmpty() )
386 wxCmdLineParam
& param
= m_data
->m_paramDesc
.Last();
388 wxASSERT_MSG( !(param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
),
389 _T("all parameters after the one with wxCMD_LINE_PARAM_MULTIPLE style will be ignored") );
391 if ( !(flags
& wxCMD_LINE_PARAM_OPTIONAL
) )
393 wxASSERT_MSG( !(param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
),
394 _T("a required parameter can't follow an optional one") );
399 wxCmdLineParam
*param
= new wxCmdLineParam(desc
, type
, flags
);
401 m_data
->m_paramDesc
.Add(param
);
404 // ----------------------------------------------------------------------------
405 // access to parse command line
406 // ----------------------------------------------------------------------------
408 bool wxCmdLineParser::Found(const wxString
& name
) const
410 int i
= m_data
->FindOption(name
);
411 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown switch") );
413 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
414 if ( !opt
.HasValue() )
420 bool wxCmdLineParser::Found(const wxString
& name
, wxString
*value
) const
422 int i
= m_data
->FindOption(name
);
423 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
425 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
426 if ( !opt
.HasValue() )
429 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
431 *value
= opt
.GetStrVal();
436 bool wxCmdLineParser::Found(const wxString
& name
, long *value
) const
438 int i
= m_data
->FindOption(name
);
439 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
441 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
442 if ( !opt
.HasValue() )
445 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
447 *value
= opt
.GetLongVal();
452 bool wxCmdLineParser::Found(const wxString
& name
, wxDateTime
*value
) const
454 int i
= m_data
->FindOption(name
);
455 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
457 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
458 if ( !opt
.HasValue() )
461 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
463 *value
= opt
.GetDateVal();
468 size_t wxCmdLineParser::GetParamCount() const
470 return m_data
->m_parameters
.GetCount();
473 wxString
wxCmdLineParser::GetParam(size_t n
) const
475 return m_data
->m_parameters
[n
];
478 // Resets switches and options
479 void wxCmdLineParser::Reset()
481 for ( size_t i
= 0; i
< m_data
->m_options
.Count(); i
++ )
483 wxCmdLineOption
& opt
= m_data
->m_options
[i
];
484 opt
.SetHasValue(FALSE
);
489 // ----------------------------------------------------------------------------
490 // the real work is done here
491 // ----------------------------------------------------------------------------
493 int wxCmdLineParser::Parse()
495 bool maybeOption
= TRUE
; // can the following arg be an option?
496 bool ok
= TRUE
; // TRUE until an error is detected
497 bool helpRequested
= FALSE
; // TRUE if "-h" was given
498 bool hadRepeatableParam
= FALSE
; // TRUE if found param with MULTIPLE flag
500 size_t currentParam
= 0; // the index in m_paramDesc
502 size_t countParam
= m_data
->m_paramDesc
.GetCount();
508 size_t count
= m_data
->m_arguments
.GetCount();
509 for ( size_t n
= 1; ok
&& (n
< count
); n
++ ) // 0 is program name
511 arg
= m_data
->m_arguments
[n
];
513 // special case: "--" should be discarded and all following arguments
514 // should be considered as parameters, even if they start with '-' and
515 // not like options (this is POSIX-like)
516 if ( arg
== _T("--") )
523 // empty argument or just '-' is not an option but a parameter
524 if ( maybeOption
&& arg
.length() > 1 &&
525 wxStrchr(m_data
->m_switchChars
, arg
[0u]) )
529 int optInd
= wxNOT_FOUND
; // init to suppress warnings
531 // an option or a switch: find whether it's a long or a short one
532 if ( m_data
->m_enableLongOptions
&&
533 arg
[0u] == _T('-') && arg
[1u] == _T('-') )
538 const wxChar
*p
= arg
.c_str() + 2;
539 while ( wxIsalnum(*p
) || (*p
== _T('_')) || (*p
== _T('-')) )
544 optInd
= m_data
->FindOptionByLongName(name
);
545 if ( optInd
== wxNOT_FOUND
)
547 wxLogError(_("Unknown long option '%s'"), name
.c_str());
554 // a short one: as they can be cumulated, we try to find the
555 // longest substring which is a valid option
556 const wxChar
*p
= arg
.c_str() + 1;
557 while ( wxIsalnum(*p
) || (*p
== _T('_')) )
562 size_t len
= name
.length();
567 // we couldn't find a valid option name in the
568 // beginning of this string
569 wxLogError(_("Unknown option '%s'"), name
.c_str());
575 optInd
= m_data
->FindOption(name
.Left(len
));
577 // will try with one character less the next time
581 while ( optInd
== wxNOT_FOUND
);
583 len
++; // compensates extra len-- above
584 if ( (optInd
!= wxNOT_FOUND
) && (len
!= name
.length()) )
586 // first of all, the option name is only part of this
588 name
= name
.Left(len
);
590 // our option is only part of this argument, there is
591 // something else in it - it is either the value of this
592 // option or other switches if it is a switch
593 if ( m_data
->m_options
[(size_t)optInd
].kind
594 == wxCMD_LINE_SWITCH
)
596 // pretend that all the rest of the argument is the
597 // next argument, in fact
598 wxString arg2
= arg
[0u];
599 arg2
+= arg
.Mid(len
+ 1); // +1 for leading '-'
601 m_data
->m_arguments
.Insert(arg2
, n
+ 1);
604 //else: it's our value, we'll deal with it below
608 if ( optInd
== wxNOT_FOUND
)
612 continue; // will break, in fact
615 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)optInd
];
616 if ( opt
.kind
== wxCMD_LINE_SWITCH
)
618 // nothing more to do
621 if ( opt
.flags
& wxCMD_LINE_OPTION_HELP
)
623 helpRequested
= TRUE
;
625 // it's not an error, but we still stop here
633 // +1 for leading '-'
634 const wxChar
*p
= arg
.c_str() + 1 + name
.length();
637 p
++; // for another leading '-'
639 if ( *p
++ != _T('=') )
641 wxLogError(_("Option '%s' requires a value, '=' expected."), name
.c_str());
657 // the value is in the next argument
660 // ... but there is none
661 wxLogError(_("Option '%s' requires a value."),
668 // ... take it from there
669 p
= m_data
->m_arguments
[n
].c_str();
674 // the value is right here: this may be legal or
675 // not depending on the option style
676 if ( opt
.flags
& wxCMD_LINE_NEEDS_SEPARATOR
)
678 wxLogError(_("Separator expected after the option '%s'."),
692 wxFAIL_MSG( _T("unknown option type") );
693 // still fall through
695 case wxCMD_LINE_VAL_STRING
:
696 opt
.SetStrVal(value
);
699 case wxCMD_LINE_VAL_NUMBER
:
702 if ( value
.ToLong(&val
) )
708 wxLogError(_("'%s' is not a correct numeric value for option '%s'."),
709 value
.c_str(), name
.c_str());
716 case wxCMD_LINE_VAL_DATE
:
719 const wxChar
*res
= dt
.ParseDate(value
);
722 wxLogError(_("Option '%s': '%s' cannot be converted to a date."),
723 name
.c_str(), value
.c_str());
740 if ( currentParam
< countParam
)
742 wxCmdLineParam
& param
= m_data
->m_paramDesc
[currentParam
];
744 // TODO check the param type
746 m_data
->m_parameters
.Add(arg
);
748 if ( !(param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
) )
754 wxASSERT_MSG( currentParam
== countParam
- 1,
755 _T("all parameters after the one with wxCMD_LINE_PARAM_MULTIPLE style are ignored") );
757 // remember that we did have this last repeatable parameter
758 hadRepeatableParam
= TRUE
;
763 wxLogError(_("Unexpected parameter '%s'"), arg
.c_str());
770 // verify that all mandatory options were given
773 size_t countOpt
= m_data
->m_options
.GetCount();
774 for ( size_t n
= 0; ok
&& (n
< countOpt
); n
++ )
776 wxCmdLineOption
& opt
= m_data
->m_options
[n
];
777 if ( (opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) && !opt
.HasValue() )
782 optName
= opt
.shortName
;
786 optName
.Printf(_("%s (or %s)"),
787 opt
.shortName
.c_str(),
788 opt
.longName
.c_str());
791 wxLogError(_("The value for the option '%s' must be specified."),
798 for ( ; ok
&& (currentParam
< countParam
); currentParam
++ )
800 wxCmdLineParam
& param
= m_data
->m_paramDesc
[currentParam
];
801 if ( (currentParam
== countParam
- 1) &&
802 (param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
) &&
805 // special case: currentParam wasn't incremented, but we did
806 // have it, so don't give error
810 if ( !(param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
) )
812 wxLogError(_("The required parameter '%s' was not specified."),
813 param
.description
.c_str());
825 return ok
? 0 : helpRequested
? -1 : 1;
828 // ----------------------------------------------------------------------------
829 // give the usage message
830 // ----------------------------------------------------------------------------
832 void wxCmdLineParser::Usage()
834 wxString appname
= wxTheApp
->GetAppName();
837 wxCHECK_RET( !m_data
->m_arguments
.IsEmpty(), _T("no program name") );
839 appname
= wxFileNameFromPath(m_data
->m_arguments
[0]);
840 wxStripExtension(appname
);
843 // we construct the brief cmd line desc on the fly, but not the detailed
844 // help message below because we want to align the options descriptions
845 // and for this we must first know the longest one of them
847 wxArrayString namesOptions
, descOptions
;
848 brief
.Printf(_("Usage: %s"), appname
.c_str());
850 // the switch char is usually '-' but this can be changed with
851 // SetSwitchChars() and then the first one of possible chars is used
852 wxChar chSwitch
= !m_data
->m_switchChars
? _T('-')
853 : m_data
->m_switchChars
[0u];
855 size_t n
, count
= m_data
->m_options
.GetCount();
856 for ( n
= 0; n
< count
; n
++ )
858 wxCmdLineOption
& opt
= m_data
->m_options
[n
];
861 if ( !(opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) )
866 brief
<< chSwitch
<< opt
.shortName
;
869 option
<< _T(" ") << chSwitch
<< opt
.shortName
;
870 if ( !!opt
.longName
)
872 option
<< _T(" --") << opt
.longName
;
875 if ( opt
.kind
!= wxCMD_LINE_SWITCH
)
878 val
<< _T('<') << GetTypeName(opt
.type
) << _T('>');
879 brief
<< _T(' ') << val
;
880 option
<< (!opt
.longName
? _T(':') : _T('=')) << val
;
883 if ( !(opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) )
888 namesOptions
.Add(option
);
889 descOptions
.Add(opt
.description
);
892 count
= m_data
->m_paramDesc
.GetCount();
893 for ( n
= 0; n
< count
; n
++ )
895 wxCmdLineParam
& param
= m_data
->m_paramDesc
[n
];
898 if ( param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
)
903 brief
<< param
.description
;
905 if ( param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
)
910 if ( param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
)
916 if ( !!m_data
->m_logo
)
918 wxLogMessage(m_data
->m_logo
);
923 // now construct the detailed help message
924 size_t len
, lenMax
= 0;
925 count
= namesOptions
.GetCount();
926 for ( n
= 0; n
< count
; n
++ )
928 len
= namesOptions
[n
].length();
934 for ( n
= 0; n
< count
; n
++ )
936 len
= namesOptions
[n
].length();
937 detailed
<< namesOptions
[n
]
938 << wxString(_T(' '), lenMax
- len
) << _T('\t')
943 wxLogMessage(detailed
);
946 // ----------------------------------------------------------------------------
948 // ----------------------------------------------------------------------------
950 static wxString
GetTypeName(wxCmdLineParamType type
)
956 wxFAIL_MSG( _T("unknown option type") );
957 // still fall through
959 case wxCMD_LINE_VAL_STRING
: s
= _("str"); break;
960 case wxCMD_LINE_VAL_NUMBER
: s
= _("num"); break;
961 case wxCMD_LINE_VAL_DATE
: s
= _("date"); break;