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 #if wxUSE_CMDLINE_PARSER
34 #include "wx/string.h"
38 #include "wx/dynarray.h"
39 #include "wx/filefn.h"
44 #include "wx/datetime.h"
45 #include "wx/cmdline.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 static wxString
GetTypeName(wxCmdLineParamType type
);
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // an internal representation of an option
58 struct wxCmdLineOption
60 wxCmdLineOption(wxCmdLineEntryType k
,
64 wxCmdLineParamType typ
,
67 wxASSERT_MSG( !shrt
.empty() || !lng
.empty(),
68 _T("option should have at least one name") );
82 // can't use union easily here, so just store all possible data fields, we
83 // don't waste much (might still use union later if the number of supported
84 // types increases, so always use the accessor functions and don't access
85 // the fields directly!)
87 void Check(wxCmdLineParamType
WXUNUSED_UNLESS_DEBUG(typ
)) const
89 wxASSERT_MSG( type
== typ
, _T("type mismatch in wxCmdLineOption") );
92 long GetLongVal() const
93 { Check(wxCMD_LINE_VAL_NUMBER
); return m_longVal
; }
94 const wxString
& GetStrVal() const
95 { Check(wxCMD_LINE_VAL_STRING
); return m_strVal
; }
96 const wxDateTime
& GetDateVal() const
97 { Check(wxCMD_LINE_VAL_DATE
); return m_dateVal
; }
99 void SetLongVal(long val
)
100 { Check(wxCMD_LINE_VAL_NUMBER
); m_longVal
= val
; m_hasVal
= TRUE
; }
101 void SetStrVal(const wxString
& val
)
102 { Check(wxCMD_LINE_VAL_STRING
); m_strVal
= val
; m_hasVal
= TRUE
; }
103 void SetDateVal(const wxDateTime val
)
104 { Check(wxCMD_LINE_VAL_DATE
); m_dateVal
= val
; m_hasVal
= TRUE
; }
106 void SetHasValue(bool hasValue
= TRUE
) { m_hasVal
= hasValue
; }
107 bool HasValue() const { return m_hasVal
; }
110 wxCmdLineEntryType kind
;
111 wxString shortName
, longName
, description
;
112 wxCmdLineParamType type
;
120 wxDateTime m_dateVal
;
123 struct wxCmdLineParam
125 wxCmdLineParam(const wxString
& desc
,
126 wxCmdLineParamType typ
,
134 wxString description
;
135 wxCmdLineParamType type
;
139 WX_DECLARE_OBJARRAY(wxCmdLineOption
, wxArrayOptions
);
140 WX_DECLARE_OBJARRAY(wxCmdLineParam
, wxArrayParams
);
142 #include "wx/arrimpl.cpp"
144 WX_DEFINE_OBJARRAY(wxArrayOptions
);
145 WX_DEFINE_OBJARRAY(wxArrayParams
);
147 // the parser internal state
148 struct wxCmdLineParserData
151 wxString m_switchChars
; // characters which may start an option
152 bool m_enableLongOptions
; // TRUE if long options are enabled
153 wxString m_logo
; // some extra text to show in Usage()
156 wxArrayString m_arguments
; // == argv, argc == m_arguments.GetCount()
157 wxArrayOptions m_options
; // all possible options and switchrs
158 wxArrayParams m_paramDesc
; // description of all possible params
159 wxArrayString m_parameters
; // all params found
162 wxCmdLineParserData();
163 void SetArguments(int argc
, char **argv
);
164 void SetArguments(const wxString
& cmdline
);
166 int FindOption(const wxString
& name
);
167 int FindOptionByLongName(const wxString
& name
);
170 // ============================================================================
172 // ============================================================================
174 // ----------------------------------------------------------------------------
175 // wxCmdLineParserData
176 // ----------------------------------------------------------------------------
178 wxCmdLineParserData::wxCmdLineParserData()
180 m_enableLongOptions
= TRUE
;
182 m_switchChars
= _T("-");
184 m_switchChars
= _T("/-");
188 void wxCmdLineParserData::SetArguments(int argc
, char **argv
)
192 for ( int n
= 0; n
< argc
; n
++ )
194 m_arguments
.Add(argv
[n
]);
198 void wxCmdLineParserData::SetArguments(const wxString
& cmdLine
)
202 m_arguments
.Add(wxTheApp
->GetAppName());
205 // Treat strings enclosed in double-quotes as single arguments
207 int len
= cmdLine
.Length();
211 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
216 if (cmdLine
.GetChar(i
) == wxT('"')) // We found the start of a string
220 while ((i
< len
) && (cmdLine
.GetChar(i
) != wxT('"')))
223 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
225 m_arguments
.Add(arg
);
228 i
++; // Skip past 2nd quote
230 else // Unquoted argument
233 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
236 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
238 m_arguments
.Add(arg
);
244 int wxCmdLineParserData::FindOption(const wxString
& name
)
248 size_t count
= m_options
.GetCount();
249 for ( size_t n
= 0; n
< count
; n
++ )
251 if ( m_options
[n
].shortName
== name
)
262 int wxCmdLineParserData::FindOptionByLongName(const wxString
& name
)
264 size_t count
= m_options
.GetCount();
265 for ( size_t n
= 0; n
< count
; n
++ )
267 if ( m_options
[n
].longName
== name
)
277 // ----------------------------------------------------------------------------
278 // construction and destruction
279 // ----------------------------------------------------------------------------
281 void wxCmdLineParser::Init()
283 m_data
= new wxCmdLineParserData
;
286 void wxCmdLineParser::SetCmdLine(int argc
, char **argv
)
288 m_data
->SetArguments(argc
, argv
);
291 void wxCmdLineParser::SetCmdLine(const wxString
& cmdline
)
293 m_data
->SetArguments(cmdline
);
296 wxCmdLineParser::~wxCmdLineParser()
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 void wxCmdLineParser::SetSwitchChars(const wxString
& switchChars
)
307 m_data
->m_switchChars
= switchChars
;
310 void wxCmdLineParser::EnableLongOptions(bool enable
)
312 m_data
->m_enableLongOptions
= enable
;
315 void wxCmdLineParser::SetLogo(const wxString
& logo
)
317 m_data
->m_logo
= logo
;
320 // ----------------------------------------------------------------------------
321 // command line construction
322 // ----------------------------------------------------------------------------
324 void wxCmdLineParser::SetDesc(const wxCmdLineEntryDesc
*desc
)
328 switch ( desc
->kind
)
330 case wxCMD_LINE_SWITCH
:
331 AddSwitch(desc
->shortName
, desc
->longName
, desc
->description
,
335 case wxCMD_LINE_OPTION
:
336 AddOption(desc
->shortName
, desc
->longName
, desc
->description
,
337 desc
->type
, desc
->flags
);
340 case wxCMD_LINE_PARAM
:
341 AddParam(desc
->description
, desc
->type
, desc
->flags
);
345 wxFAIL_MSG( _T("unknown command line entry type") );
346 // still fall through
348 case wxCMD_LINE_NONE
:
354 void wxCmdLineParser::AddSwitch(const wxString
& shortName
,
355 const wxString
& longName
,
356 const wxString
& desc
,
359 wxASSERT_MSG( m_data
->FindOption(shortName
) == wxNOT_FOUND
,
360 _T("duplicate switch") );
362 wxCmdLineOption
*option
= new wxCmdLineOption(wxCMD_LINE_SWITCH
,
363 shortName
, longName
, desc
,
364 wxCMD_LINE_VAL_NONE
, flags
);
366 m_data
->m_options
.Add(option
);
369 void wxCmdLineParser::AddOption(const wxString
& shortName
,
370 const wxString
& longName
,
371 const wxString
& desc
,
372 wxCmdLineParamType type
,
375 wxASSERT_MSG( m_data
->FindOption(shortName
) == wxNOT_FOUND
,
376 _T("duplicate option") );
378 wxCmdLineOption
*option
= new wxCmdLineOption(wxCMD_LINE_OPTION
,
379 shortName
, longName
, desc
,
382 m_data
->m_options
.Add(option
);
385 void wxCmdLineParser::AddParam(const wxString
& desc
,
386 wxCmdLineParamType type
,
389 // do some consistency checks: a required parameter can't follow an
390 // optional one and nothing should follow a parameter with MULTIPLE flag
392 if ( !m_data
->m_paramDesc
.IsEmpty() )
394 wxCmdLineParam
& param
= m_data
->m_paramDesc
.Last();
396 wxASSERT_MSG( !(param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
),
397 _T("all parameters after the one with wxCMD_LINE_PARAM_MULTIPLE style will be ignored") );
399 if ( !(flags
& wxCMD_LINE_PARAM_OPTIONAL
) )
401 wxASSERT_MSG( !(param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
),
402 _T("a required parameter can't follow an optional one") );
407 wxCmdLineParam
*param
= new wxCmdLineParam(desc
, type
, flags
);
409 m_data
->m_paramDesc
.Add(param
);
412 // ----------------------------------------------------------------------------
413 // access to parse command line
414 // ----------------------------------------------------------------------------
416 bool wxCmdLineParser::Found(const wxString
& name
) const
418 int i
= m_data
->FindOption(name
);
419 if ( i
== wxNOT_FOUND
)
420 i
= m_data
->FindOptionByLongName(name
);
422 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown switch") );
424 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
425 if ( !opt
.HasValue() )
431 bool wxCmdLineParser::Found(const wxString
& name
, wxString
*value
) const
433 int i
= m_data
->FindOption(name
);
434 if ( i
== wxNOT_FOUND
)
435 i
= m_data
->FindOptionByLongName(name
);
437 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
439 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
440 if ( !opt
.HasValue() )
443 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
445 *value
= opt
.GetStrVal();
450 bool wxCmdLineParser::Found(const wxString
& name
, long *value
) const
452 int i
= m_data
->FindOption(name
);
453 if ( i
== wxNOT_FOUND
)
454 i
= m_data
->FindOptionByLongName(name
);
456 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
458 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
459 if ( !opt
.HasValue() )
462 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
464 *value
= opt
.GetLongVal();
469 bool wxCmdLineParser::Found(const wxString
& name
, wxDateTime
*value
) const
471 int i
= m_data
->FindOption(name
);
472 if ( i
== wxNOT_FOUND
)
473 i
= m_data
->FindOptionByLongName(name
);
475 wxCHECK_MSG( i
!= wxNOT_FOUND
, FALSE
, _T("unknown option") );
477 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)i
];
478 if ( !opt
.HasValue() )
481 wxCHECK_MSG( value
, FALSE
, _T("NULL pointer in wxCmdLineOption::Found") );
483 *value
= opt
.GetDateVal();
488 size_t wxCmdLineParser::GetParamCount() const
490 return m_data
->m_parameters
.GetCount();
493 wxString
wxCmdLineParser::GetParam(size_t n
) const
495 return m_data
->m_parameters
[n
];
498 // Resets switches and options
499 void wxCmdLineParser::Reset()
501 for ( size_t i
= 0; i
< m_data
->m_options
.Count(); i
++ )
503 wxCmdLineOption
& opt
= m_data
->m_options
[i
];
504 opt
.SetHasValue(FALSE
);
509 // ----------------------------------------------------------------------------
510 // the real work is done here
511 // ----------------------------------------------------------------------------
513 int wxCmdLineParser::Parse()
515 bool maybeOption
= TRUE
; // can the following arg be an option?
516 bool ok
= TRUE
; // TRUE until an error is detected
517 bool helpRequested
= FALSE
; // TRUE if "-h" was given
518 bool hadRepeatableParam
= FALSE
; // TRUE if found param with MULTIPLE flag
520 size_t currentParam
= 0; // the index in m_paramDesc
522 size_t countParam
= m_data
->m_paramDesc
.GetCount();
528 size_t count
= m_data
->m_arguments
.GetCount();
529 for ( size_t n
= 1; ok
&& (n
< count
); n
++ ) // 0 is program name
531 arg
= m_data
->m_arguments
[n
];
533 // special case: "--" should be discarded and all following arguments
534 // should be considered as parameters, even if they start with '-' and
535 // not like options (this is POSIX-like)
536 if ( arg
== _T("--") )
543 // empty argument or just '-' is not an option but a parameter
544 if ( maybeOption
&& arg
.length() > 1 &&
545 wxStrchr(m_data
->m_switchChars
, arg
[0u]) )
549 int optInd
= wxNOT_FOUND
; // init to suppress warnings
551 // an option or a switch: find whether it's a long or a short one
552 if ( m_data
->m_enableLongOptions
&&
553 arg
[0u] == _T('-') && arg
[1u] == _T('-') )
558 const wxChar
*p
= arg
.c_str() + 2;
559 while ( wxIsalnum(*p
) || (*p
== _T('_')) || (*p
== _T('-')) )
564 optInd
= m_data
->FindOptionByLongName(name
);
565 if ( optInd
== wxNOT_FOUND
)
567 wxLogError(_("Unknown long option '%s'"), name
.c_str());
574 // a short one: as they can be cumulated, we try to find the
575 // longest substring which is a valid option
576 const wxChar
*p
= arg
.c_str() + 1;
577 while ( wxIsalnum(*p
) || (*p
== _T('_')) )
582 size_t len
= name
.length();
587 // we couldn't find a valid option name in the
588 // beginning of this string
589 wxLogError(_("Unknown option '%s'"), name
.c_str());
595 optInd
= m_data
->FindOption(name
.Left(len
));
597 // will try with one character less the next time
601 while ( optInd
== wxNOT_FOUND
);
603 len
++; // compensates extra len-- above
604 if ( (optInd
!= wxNOT_FOUND
) && (len
!= name
.length()) )
606 // first of all, the option name is only part of this
608 name
= name
.Left(len
);
610 // our option is only part of this argument, there is
611 // something else in it - it is either the value of this
612 // option or other switches if it is a switch
613 if ( m_data
->m_options
[(size_t)optInd
].kind
614 == wxCMD_LINE_SWITCH
)
616 // pretend that all the rest of the argument is the
617 // next argument, in fact
618 wxString arg2
= arg
[0u];
619 arg2
+= arg
.Mid(len
+ 1); // +1 for leading '-'
621 m_data
->m_arguments
.Insert(arg2
, n
+ 1);
624 //else: it's our value, we'll deal with it below
628 if ( optInd
== wxNOT_FOUND
)
632 continue; // will break, in fact
635 wxCmdLineOption
& opt
= m_data
->m_options
[(size_t)optInd
];
636 if ( opt
.kind
== wxCMD_LINE_SWITCH
)
638 // nothing more to do
641 if ( opt
.flags
& wxCMD_LINE_OPTION_HELP
)
643 helpRequested
= TRUE
;
645 // it's not an error, but we still stop here
653 // +1 for leading '-'
654 const wxChar
*p
= arg
.c_str() + 1 + name
.length();
657 p
++; // for another leading '-'
659 if ( *p
++ != _T('=') )
661 wxLogError(_("Option '%s' requires a value, '=' expected."), name
.c_str());
677 // the value is in the next argument
680 // ... but there is none
681 wxLogError(_("Option '%s' requires a value."),
688 // ... take it from there
689 p
= m_data
->m_arguments
[n
].c_str();
694 // the value is right here: this may be legal or
695 // not depending on the option style
696 if ( opt
.flags
& wxCMD_LINE_NEEDS_SEPARATOR
)
698 wxLogError(_("Separator expected after the option '%s'."),
712 wxFAIL_MSG( _T("unknown option type") );
713 // still fall through
715 case wxCMD_LINE_VAL_STRING
:
716 opt
.SetStrVal(value
);
719 case wxCMD_LINE_VAL_NUMBER
:
722 if ( value
.ToLong(&val
) )
728 wxLogError(_("'%s' is not a correct numeric value for option '%s'."),
729 value
.c_str(), name
.c_str());
736 case wxCMD_LINE_VAL_DATE
:
739 const wxChar
*res
= dt
.ParseDate(value
);
742 wxLogError(_("Option '%s': '%s' cannot be converted to a date."),
743 name
.c_str(), value
.c_str());
760 if ( currentParam
< countParam
)
762 wxCmdLineParam
& param
= m_data
->m_paramDesc
[currentParam
];
764 // TODO check the param type
766 m_data
->m_parameters
.Add(arg
);
768 if ( !(param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
) )
774 wxASSERT_MSG( currentParam
== countParam
- 1,
775 _T("all parameters after the one with wxCMD_LINE_PARAM_MULTIPLE style are ignored") );
777 // remember that we did have this last repeatable parameter
778 hadRepeatableParam
= TRUE
;
783 wxLogError(_("Unexpected parameter '%s'"), arg
.c_str());
790 // verify that all mandatory options were given
793 size_t countOpt
= m_data
->m_options
.GetCount();
794 for ( size_t n
= 0; ok
&& (n
< countOpt
); n
++ )
796 wxCmdLineOption
& opt
= m_data
->m_options
[n
];
797 if ( (opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) && !opt
.HasValue() )
802 optName
= opt
.shortName
;
806 optName
.Printf(_("%s (or %s)"),
807 opt
.shortName
.c_str(),
808 opt
.longName
.c_str());
811 wxLogError(_("The value for the option '%s' must be specified."),
818 for ( ; ok
&& (currentParam
< countParam
); currentParam
++ )
820 wxCmdLineParam
& param
= m_data
->m_paramDesc
[currentParam
];
821 if ( (currentParam
== countParam
- 1) &&
822 (param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
) &&
825 // special case: currentParam wasn't incremented, but we did
826 // have it, so don't give error
830 if ( !(param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
) )
832 wxLogError(_("The required parameter '%s' was not specified."),
833 param
.description
.c_str());
845 return ok
? 0 : helpRequested
? -1 : 1;
848 // ----------------------------------------------------------------------------
849 // give the usage message
850 // ----------------------------------------------------------------------------
852 void wxCmdLineParser::Usage()
854 wxString appname
= wxTheApp
->GetAppName();
857 wxCHECK_RET( !m_data
->m_arguments
.IsEmpty(), _T("no program name") );
859 appname
= wxFileNameFromPath(m_data
->m_arguments
[0]);
860 wxStripExtension(appname
);
863 // we construct the brief cmd line desc on the fly, but not the detailed
864 // help message below because we want to align the options descriptions
865 // and for this we must first know the longest one of them
867 wxArrayString namesOptions
, descOptions
;
868 brief
.Printf(_("Usage: %s"), appname
.c_str());
870 // the switch char is usually '-' but this can be changed with
871 // SetSwitchChars() and then the first one of possible chars is used
872 wxChar chSwitch
= !m_data
->m_switchChars
? _T('-')
873 : m_data
->m_switchChars
[0u];
875 size_t n
, count
= m_data
->m_options
.GetCount();
876 for ( n
= 0; n
< count
; n
++ )
878 wxCmdLineOption
& opt
= m_data
->m_options
[n
];
881 if ( !(opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) )
886 brief
<< chSwitch
<< opt
.shortName
;
889 option
<< _T(" ") << chSwitch
<< opt
.shortName
;
890 if ( !!opt
.longName
)
892 option
<< _T(" --") << opt
.longName
;
895 if ( opt
.kind
!= wxCMD_LINE_SWITCH
)
898 val
<< _T('<') << GetTypeName(opt
.type
) << _T('>');
899 brief
<< _T(' ') << val
;
900 option
<< (!opt
.longName
? _T(':') : _T('=')) << val
;
903 if ( !(opt
.flags
& wxCMD_LINE_OPTION_MANDATORY
) )
908 namesOptions
.Add(option
);
909 descOptions
.Add(opt
.description
);
912 count
= m_data
->m_paramDesc
.GetCount();
913 for ( n
= 0; n
< count
; n
++ )
915 wxCmdLineParam
& param
= m_data
->m_paramDesc
[n
];
918 if ( param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
)
923 brief
<< param
.description
;
925 if ( param
.flags
& wxCMD_LINE_PARAM_MULTIPLE
)
930 if ( param
.flags
& wxCMD_LINE_PARAM_OPTIONAL
)
936 if ( !!m_data
->m_logo
)
938 wxLogMessage(m_data
->m_logo
);
943 // now construct the detailed help message
944 size_t len
, lenMax
= 0;
945 count
= namesOptions
.GetCount();
946 for ( n
= 0; n
< count
; n
++ )
948 len
= namesOptions
[n
].length();
954 for ( n
= 0; n
< count
; n
++ )
956 len
= namesOptions
[n
].length();
957 detailed
<< namesOptions
[n
]
958 << wxString(_T(' '), lenMax
- len
) << _T('\t')
963 wxLogMessage(detailed
);
966 // ----------------------------------------------------------------------------
968 // ----------------------------------------------------------------------------
970 static wxString
GetTypeName(wxCmdLineParamType type
)
976 wxFAIL_MSG( _T("unknown option type") );
977 // still fall through
979 case wxCMD_LINE_VAL_STRING
: s
= _("str"); break;
980 case wxCMD_LINE_VAL_NUMBER
: s
= _("num"); break;
981 case wxCMD_LINE_VAL_DATE
: s
= _("date"); break;
987 #endif // wxUSE_CMDLINE_PARSER