]>
Commit | Line | Data |
---|---|---|
9f83044f VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: common/cmdline.cpp | |
3 | // Purpose: wxCmdLineParser implementation | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 05.01.00 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "cmdline.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/string.h" | |
33 | #include "wx/log.h" | |
34 | #include "wx/intl.h" | |
2822ee33 | 35 | #include "wx/app.h" |
9f83044f | 36 | #include "wx/dynarray.h" |
2822ee33 | 37 | #include "wx/filefn.h" |
9f83044f VZ |
38 | #endif //WX_PRECOMP |
39 | ||
40 | #include "wx/datetime.h" | |
41 | #include "wx/cmdline.h" | |
42 | ||
43 | // ---------------------------------------------------------------------------- | |
44 | // private functions | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
47 | static wxString GetTypeName(wxCmdLineParamType type); | |
48 | ||
49 | // ---------------------------------------------------------------------------- | |
50 | // private classes | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | // an internal representation of an option | |
54 | struct wxCmdLineOption | |
55 | { | |
56 | wxCmdLineOption(wxCmdLineEntryType k, | |
57 | const wxString& shrt, | |
58 | const wxString& lng, | |
59 | const wxString& desc, | |
60 | wxCmdLineParamType typ, | |
61 | int fl) | |
62 | { | |
63 | kind = k; | |
64 | ||
65 | shortName = shrt; | |
66 | longName = lng; | |
67 | description = desc; | |
68 | ||
69 | type = typ; | |
70 | flags = fl; | |
71 | ||
72 | m_hasVal = FALSE; | |
73 | } | |
74 | ||
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!) | |
79 | ||
6f2a55e3 | 80 | void Check(wxCmdLineParamType WXUNUSED_UNLESS_DEBUG(typ)) const |
9f83044f VZ |
81 | { |
82 | wxASSERT_MSG( type == typ, _T("type mismatch in wxCmdLineOption") ); | |
83 | } | |
84 | ||
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; } | |
91 | ||
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; } | |
98 | ||
99 | void SetHasValue() { m_hasVal = TRUE; } | |
100 | bool HasValue() const { return m_hasVal; } | |
101 | ||
102 | public: | |
103 | wxCmdLineEntryType kind; | |
104 | wxString shortName, longName, description; | |
105 | wxCmdLineParamType type; | |
106 | int flags; | |
107 | ||
108 | private: | |
109 | bool m_hasVal; | |
110 | ||
111 | long m_longVal; | |
112 | wxString m_strVal; | |
113 | wxDateTime m_dateVal; | |
114 | }; | |
115 | ||
116 | struct wxCmdLineParam | |
117 | { | |
118 | wxCmdLineParam(const wxString& desc, | |
119 | wxCmdLineParamType typ, | |
120 | int fl) | |
121 | : description(desc) | |
122 | { | |
123 | type = typ; | |
124 | flags = fl; | |
125 | } | |
126 | ||
127 | wxString description; | |
128 | wxCmdLineParamType type; | |
129 | int flags; | |
130 | }; | |
131 | ||
132 | WX_DECLARE_OBJARRAY(wxCmdLineOption, wxArrayOptions); | |
133 | WX_DECLARE_OBJARRAY(wxCmdLineParam, wxArrayParams); | |
134 | ||
135 | #include "wx/arrimpl.cpp" | |
136 | ||
137 | WX_DEFINE_OBJARRAY(wxArrayOptions); | |
138 | WX_DEFINE_OBJARRAY(wxArrayParams); | |
139 | ||
140 | // the parser internal state | |
141 | struct wxCmdLineParserData | |
142 | { | |
143 | // options | |
144 | wxString m_switchChars; // characters which may start an option | |
9f83044f | 145 | bool m_enableLongOptions; // TRUE if long options are enabled |
e612f101 | 146 | wxString m_logo; // some extra text to show in Usage() |
9f83044f VZ |
147 | |
148 | // cmd line data | |
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 | |
153 | ||
154 | // methods | |
155 | wxCmdLineParserData(); | |
156 | void SetArguments(int argc, char **argv); | |
157 | void SetArguments(const wxString& cmdline); | |
158 | ||
159 | int FindOption(const wxString& name); | |
160 | int FindOptionByLongName(const wxString& name); | |
161 | }; | |
162 | ||
163 | // ============================================================================ | |
164 | // implementation | |
165 | // ============================================================================ | |
166 | ||
167 | // ---------------------------------------------------------------------------- | |
168 | // wxCmdLineParserData | |
169 | // ---------------------------------------------------------------------------- | |
170 | ||
171 | wxCmdLineParserData::wxCmdLineParserData() | |
172 | { | |
173 | m_enableLongOptions = TRUE; | |
174 | #ifdef __UNIX_LIKE__ | |
175 | m_switchChars = _T("-"); | |
176 | #else // !Unix | |
177 | m_switchChars = _T("/-"); | |
178 | #endif | |
179 | } | |
180 | ||
181 | void wxCmdLineParserData::SetArguments(int argc, char **argv) | |
182 | { | |
183 | m_arguments.Empty(); | |
184 | ||
185 | for ( int n = 0; n < argc; n++ ) | |
186 | { | |
187 | m_arguments.Add(argv[n]); | |
188 | } | |
189 | } | |
190 | ||
6f2a55e3 | 191 | void wxCmdLineParserData::SetArguments(const wxString& WXUNUSED(cmdline)) |
9f83044f VZ |
192 | { |
193 | // either use wxMSW wxApp::ConvertToStandardCommandArgs() or move its logic | |
194 | // here and use this method from it - but don't duplicate the code | |
195 | ||
196 | wxFAIL_MSG(_T("TODO")); | |
197 | } | |
198 | ||
199 | int wxCmdLineParserData::FindOption(const wxString& name) | |
200 | { | |
201 | size_t count = m_options.GetCount(); | |
202 | for ( size_t n = 0; n < count; n++ ) | |
203 | { | |
204 | if ( m_options[n].shortName == name ) | |
205 | { | |
206 | // found | |
207 | return n; | |
208 | } | |
209 | } | |
210 | ||
211 | return wxNOT_FOUND; | |
212 | } | |
213 | ||
214 | int wxCmdLineParserData::FindOptionByLongName(const wxString& name) | |
215 | { | |
216 | size_t count = m_options.GetCount(); | |
217 | for ( size_t n = 0; n < count; n++ ) | |
218 | { | |
219 | if ( m_options[n].longName == name ) | |
220 | { | |
221 | // found | |
222 | return n; | |
223 | } | |
224 | } | |
225 | ||
226 | return wxNOT_FOUND; | |
227 | } | |
228 | ||
229 | // ---------------------------------------------------------------------------- | |
230 | // construction and destruction | |
231 | // ---------------------------------------------------------------------------- | |
232 | ||
233 | void wxCmdLineParser::Init() | |
234 | { | |
235 | m_data = new wxCmdLineParserData; | |
236 | } | |
237 | ||
238 | void wxCmdLineParser::SetCmdLine(int argc, char **argv) | |
239 | { | |
240 | m_data->SetArguments(argc, argv); | |
241 | } | |
242 | ||
243 | void wxCmdLineParser::SetCmdLine(const wxString& cmdline) | |
244 | { | |
245 | m_data->SetArguments(cmdline); | |
246 | } | |
247 | ||
248 | wxCmdLineParser::~wxCmdLineParser() | |
249 | { | |
250 | delete m_data; | |
251 | } | |
252 | ||
253 | // ---------------------------------------------------------------------------- | |
254 | // options | |
255 | // ---------------------------------------------------------------------------- | |
256 | ||
257 | void wxCmdLineParser::SetSwitchChars(const wxString& switchChars) | |
258 | { | |
259 | m_data->m_switchChars = switchChars; | |
260 | } | |
261 | ||
262 | void wxCmdLineParser::EnableLongOptions(bool enable) | |
263 | { | |
264 | m_data->m_enableLongOptions = enable; | |
265 | } | |
266 | ||
e612f101 VZ |
267 | void wxCmdLineParser::SetLogo(const wxString& logo) |
268 | { | |
269 | m_data->m_logo = logo; | |
270 | } | |
271 | ||
9f83044f VZ |
272 | // ---------------------------------------------------------------------------- |
273 | // command line construction | |
274 | // ---------------------------------------------------------------------------- | |
275 | ||
276 | void wxCmdLineParser::SetDesc(const wxCmdLineEntryDesc *desc) | |
277 | { | |
278 | for ( ;; desc++ ) | |
279 | { | |
280 | switch ( desc->kind ) | |
281 | { | |
282 | case wxCMD_LINE_SWITCH: | |
e612f101 VZ |
283 | AddSwitch(desc->shortName, desc->longName, desc->description, |
284 | desc->flags); | |
9f83044f VZ |
285 | break; |
286 | ||
287 | case wxCMD_LINE_OPTION: | |
288 | AddOption(desc->shortName, desc->longName, desc->description, | |
289 | desc->type, desc->flags); | |
290 | break; | |
291 | ||
292 | case wxCMD_LINE_PARAM: | |
293 | AddParam(desc->description, desc->type, desc->flags); | |
294 | break; | |
295 | ||
296 | default: | |
297 | wxFAIL_MSG( _T("unknown command line entry type") ); | |
298 | // still fall through | |
299 | ||
300 | case wxCMD_LINE_NONE: | |
301 | return; | |
302 | } | |
303 | } | |
304 | } | |
305 | ||
306 | void wxCmdLineParser::AddSwitch(const wxString& shortName, | |
307 | const wxString& longName, | |
308 | const wxString& desc, | |
309 | int flags) | |
310 | { | |
311 | wxASSERT_MSG( m_data->FindOption(shortName) == wxNOT_FOUND, | |
312 | _T("duplicate switch") ); | |
313 | ||
314 | wxCmdLineOption *option = new wxCmdLineOption(wxCMD_LINE_SWITCH, | |
315 | shortName, longName, desc, | |
316 | wxCMD_LINE_VAL_NONE, flags); | |
317 | ||
318 | m_data->m_options.Add(option); | |
319 | } | |
320 | ||
321 | void wxCmdLineParser::AddOption(const wxString& shortName, | |
322 | const wxString& longName, | |
323 | const wxString& desc, | |
324 | wxCmdLineParamType type, | |
325 | int flags) | |
326 | { | |
327 | wxASSERT_MSG( m_data->FindOption(shortName) == wxNOT_FOUND, | |
328 | _T("duplicate option") ); | |
329 | ||
330 | wxCmdLineOption *option = new wxCmdLineOption(wxCMD_LINE_OPTION, | |
331 | shortName, longName, desc, | |
332 | type, flags); | |
333 | ||
334 | m_data->m_options.Add(option); | |
335 | } | |
336 | ||
337 | void wxCmdLineParser::AddParam(const wxString& desc, | |
338 | wxCmdLineParamType type, | |
339 | int flags) | |
340 | { | |
341 | // do some consistency checks: a required parameter can't follow an | |
342 | // optional one and nothing should follow a parameter with MULTIPLE flag | |
343 | #ifdef __WXDEBUG__ | |
344 | if ( !m_data->m_paramDesc.IsEmpty() ) | |
345 | { | |
346 | wxCmdLineParam& param = m_data->m_paramDesc.Last(); | |
347 | ||
348 | wxASSERT_MSG( !(param.flags & wxCMD_LINE_PARAM_MULTIPLE), | |
349 | _T("all parameters after the one with " | |
350 | "wxCMD_LINE_PARAM_MULTIPLE style will be ignored") ); | |
351 | ||
352 | if ( !(flags & wxCMD_LINE_PARAM_OPTIONAL) ) | |
353 | { | |
354 | wxASSERT_MSG( !(param.flags & wxCMD_LINE_PARAM_OPTIONAL), | |
355 | _T("a required parameter can't follow an " | |
356 | "optional one") ); | |
357 | } | |
358 | } | |
359 | #endif // Debug | |
360 | ||
361 | wxCmdLineParam *param = new wxCmdLineParam(desc, type, flags); | |
362 | ||
363 | m_data->m_paramDesc.Add(param); | |
364 | } | |
365 | ||
366 | // ---------------------------------------------------------------------------- | |
367 | // access to parse command line | |
368 | // ---------------------------------------------------------------------------- | |
369 | ||
370 | bool wxCmdLineParser::Found(const wxString& name) const | |
371 | { | |
372 | int i = m_data->FindOption(name); | |
373 | wxCHECK_MSG( i != wxNOT_FOUND, FALSE, _T("unknown switch") ); | |
374 | ||
375 | wxCmdLineOption& opt = m_data->m_options[(size_t)i]; | |
376 | if ( !opt.HasValue() ) | |
377 | return FALSE; | |
378 | ||
379 | return TRUE; | |
380 | } | |
381 | ||
382 | bool wxCmdLineParser::Found(const wxString& name, wxString *value) const | |
383 | { | |
384 | int i = m_data->FindOption(name); | |
385 | wxCHECK_MSG( i != wxNOT_FOUND, FALSE, _T("unknown option") ); | |
386 | ||
387 | wxCmdLineOption& opt = m_data->m_options[(size_t)i]; | |
388 | if ( !opt.HasValue() ) | |
389 | return FALSE; | |
390 | ||
391 | wxCHECK_MSG( value, FALSE, _T("NULL pointer in wxCmdLineOption::Found") ); | |
392 | ||
393 | *value = opt.GetStrVal(); | |
394 | ||
395 | return TRUE; | |
396 | } | |
397 | ||
398 | bool wxCmdLineParser::Found(const wxString& name, long *value) const | |
399 | { | |
400 | int i = m_data->FindOption(name); | |
401 | wxCHECK_MSG( i != wxNOT_FOUND, FALSE, _T("unknown option") ); | |
402 | ||
403 | wxCmdLineOption& opt = m_data->m_options[(size_t)i]; | |
404 | if ( !opt.HasValue() ) | |
405 | return FALSE; | |
406 | ||
407 | wxCHECK_MSG( value, FALSE, _T("NULL pointer in wxCmdLineOption::Found") ); | |
408 | ||
409 | *value = opt.GetLongVal(); | |
410 | ||
411 | return TRUE; | |
412 | } | |
413 | ||
414 | bool wxCmdLineParser::Found(const wxString& name, wxDateTime *value) const | |
415 | { | |
416 | int i = m_data->FindOption(name); | |
417 | wxCHECK_MSG( i != wxNOT_FOUND, FALSE, _T("unknown option") ); | |
418 | ||
419 | wxCmdLineOption& opt = m_data->m_options[(size_t)i]; | |
420 | if ( !opt.HasValue() ) | |
421 | return FALSE; | |
422 | ||
423 | wxCHECK_MSG( value, FALSE, _T("NULL pointer in wxCmdLineOption::Found") ); | |
424 | ||
425 | *value = opt.GetDateVal(); | |
426 | ||
427 | return TRUE; | |
428 | } | |
429 | ||
430 | size_t wxCmdLineParser::GetParamCount() const | |
431 | { | |
432 | return m_data->m_parameters.GetCount(); | |
433 | } | |
434 | ||
435 | wxString wxCmdLineParser::GetParam(size_t n) const | |
436 | { | |
437 | return m_data->m_parameters[n]; | |
438 | } | |
439 | ||
440 | // ---------------------------------------------------------------------------- | |
441 | // the real work is done here | |
442 | // ---------------------------------------------------------------------------- | |
443 | ||
444 | int wxCmdLineParser::Parse() | |
445 | { | |
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 | |
450 | ||
451 | size_t currentParam = 0; // the index in m_paramDesc | |
452 | ||
453 | size_t countParam = m_data->m_paramDesc.GetCount(); | |
454 | ||
455 | // parse everything | |
456 | wxString arg; | |
457 | size_t count = m_data->m_arguments.GetCount(); | |
458 | for ( size_t n = 1; ok && (n < count); n++ ) // 0 is program name | |
459 | { | |
460 | arg = m_data->m_arguments[n]; | |
461 | ||
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("--") ) | |
466 | { | |
467 | maybeOption = FALSE; | |
468 | ||
469 | continue; | |
470 | } | |
471 | ||
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]) ) | |
475 | { | |
476 | bool isLong; | |
477 | wxString name; | |
478 | int optInd = wxNOT_FOUND; // init to suppress warnings | |
479 | ||
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('-') ) | |
483 | { | |
484 | // a long one | |
485 | isLong = TRUE; | |
486 | ||
487 | const wxChar *p = arg.c_str() + 2; | |
488 | while ( wxIsalpha(*p) || (*p == _T('-')) ) | |
489 | { | |
490 | name += *p++; | |
491 | } | |
492 | ||
493 | optInd = m_data->FindOptionByLongName(name); | |
494 | if ( optInd == wxNOT_FOUND ) | |
495 | { | |
496 | wxLogError(_("Unknown long option '%s'"), name.c_str()); | |
497 | } | |
498 | } | |
499 | else | |
500 | { | |
501 | isLong = FALSE; | |
502 | ||
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 ( wxIsalpha(*p) ) | |
507 | { | |
508 | name += *p++; | |
509 | } | |
510 | ||
511 | size_t len = name.length(); | |
512 | do | |
513 | { | |
514 | if ( len == 0 ) | |
515 | { | |
516 | // we couldn't find a valid option name in the | |
517 | // beginning of this string | |
518 | wxLogError(_("Unknown option '%s'"), name.c_str()); | |
519 | ||
520 | break; | |
521 | } | |
522 | else | |
523 | { | |
524 | optInd = m_data->FindOption(name.Left(len)); | |
525 | ||
526 | // will try with one character less the next time | |
527 | len--; | |
528 | } | |
529 | } | |
530 | while ( optInd == wxNOT_FOUND ); | |
531 | ||
2822ee33 | 532 | len++; // compensates extra len-- above |
4f40f5e3 | 533 | if ( (optInd != wxNOT_FOUND) && (len != name.length()) ) |
9f83044f | 534 | { |
2822ee33 VZ |
535 | // first of all, the option name is only part of this |
536 | // string | |
537 | name = name.Left(len); | |
538 | ||
9f83044f VZ |
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 ) | |
544 | { | |
545 | // pretend that all the rest of the argument is the | |
546 | // next argument, in fact | |
547 | wxString arg2 = arg[0u]; | |
2822ee33 | 548 | arg2 += arg.Mid(len + 1); // +1 for leading '-' |
9f83044f VZ |
549 | |
550 | m_data->m_arguments.Insert(arg2, n + 1); | |
4f40f5e3 | 551 | count++; |
9f83044f VZ |
552 | } |
553 | //else: it's our value, we'll deal with it below | |
554 | } | |
555 | } | |
556 | ||
557 | if ( optInd == wxNOT_FOUND ) | |
558 | { | |
559 | ok = FALSE; | |
560 | ||
561 | continue; // will break, in fact | |
562 | } | |
563 | ||
564 | wxCmdLineOption& opt = m_data->m_options[(size_t)optInd]; | |
565 | if ( opt.kind == wxCMD_LINE_SWITCH ) | |
566 | { | |
567 | // nothing more to do | |
568 | opt.SetHasValue(); | |
569 | ||
570 | if ( opt.flags & wxCMD_LINE_OPTION_HELP ) | |
571 | { | |
572 | helpRequested = TRUE; | |
573 | ||
574 | // it's not an error, but we still stop here | |
575 | ok = FALSE; | |
576 | } | |
577 | } | |
578 | else | |
579 | { | |
580 | // get the value | |
581 | ||
582 | // +1 for leading '-' | |
583 | const wxChar *p = arg.c_str() + 1 + name.length(); | |
584 | if ( isLong ) | |
585 | { | |
586 | p++; // for another leading '-' | |
587 | ||
588 | if ( *p++ != _T('=') ) | |
589 | { | |
590 | wxLogError(_("Option '%s' requires a value, '=' " | |
591 | "expected."), name.c_str()); | |
592 | ||
593 | ok = FALSE; | |
594 | } | |
595 | } | |
596 | else | |
597 | { | |
598 | switch ( *p ) | |
599 | { | |
600 | case _T(':'): | |
601 | // the value follows | |
602 | p++; | |
603 | break; | |
604 | ||
605 | case 0: | |
606 | // the value is in the next argument | |
607 | if ( ++n == count ) | |
608 | { | |
609 | // ... but there is none | |
610 | wxLogError(_("Option '%s' requires a value."), | |
611 | name.c_str()); | |
612 | ||
613 | ok = FALSE; | |
614 | } | |
615 | else | |
616 | { | |
617 | // ... take it from there | |
618 | p = m_data->m_arguments[n].c_str(); | |
619 | } | |
620 | break; | |
621 | ||
622 | default: | |
623 | // the value is right here | |
624 | ; | |
625 | } | |
626 | } | |
627 | ||
628 | if ( ok ) | |
629 | { | |
630 | wxString value = p; | |
631 | switch ( opt.type ) | |
632 | { | |
633 | default: | |
634 | wxFAIL_MSG( _T("unknown option type") ); | |
635 | // still fall through | |
636 | ||
637 | case wxCMD_LINE_VAL_STRING: | |
638 | opt.SetStrVal(value); | |
639 | break; | |
640 | ||
641 | case wxCMD_LINE_VAL_NUMBER: | |
642 | { | |
643 | long val; | |
644 | if ( value.ToLong(&val) ) | |
645 | { | |
646 | opt.SetLongVal(val); | |
647 | } | |
648 | else | |
649 | { | |
650 | wxLogError(_("'%s' is not a correct " | |
651 | "numeric value for option " | |
652 | "'%s'."), | |
653 | value.c_str(), name.c_str()); | |
654 | ||
655 | ok = FALSE; | |
656 | } | |
657 | } | |
658 | break; | |
659 | ||
660 | case wxCMD_LINE_VAL_DATE: | |
661 | { | |
662 | wxDateTime dt; | |
663 | const wxChar *res = dt.ParseDate(value); | |
664 | if ( !res || *res ) | |
665 | { | |
2822ee33 | 666 | wxLogError(_("Option '%s': '%s' cannot " |
9f83044f VZ |
667 | "be converted to a date."), |
668 | name.c_str(), value.c_str()); | |
669 | ||
670 | ok = FALSE; | |
671 | } | |
672 | else | |
673 | { | |
674 | opt.SetDateVal(dt); | |
675 | } | |
676 | } | |
677 | break; | |
678 | } | |
679 | } | |
680 | } | |
681 | } | |
682 | else | |
683 | { | |
684 | // a parameter | |
685 | if ( currentParam < countParam ) | |
686 | { | |
687 | wxCmdLineParam& param = m_data->m_paramDesc[currentParam]; | |
688 | ||
689 | // TODO check the param type | |
690 | ||
691 | m_data->m_parameters.Add(arg); | |
692 | ||
693 | if ( !(param.flags & wxCMD_LINE_PARAM_MULTIPLE) ) | |
694 | { | |
695 | currentParam++; | |
696 | } | |
697 | else | |
698 | { | |
699 | wxASSERT_MSG( currentParam == countParam - 1, | |
700 | _T("all parameters after the one with " | |
701 | "wxCMD_LINE_PARAM_MULTIPLE style " | |
702 | "are ignored") ); | |
703 | ||
704 | // remember that we did have this last repeatable parameter | |
705 | hadRepeatableParam = TRUE; | |
706 | } | |
707 | } | |
708 | else | |
709 | { | |
710 | wxLogError(_("Unexpected parameter '%s'"), arg.c_str()); | |
711 | ||
712 | ok = FALSE; | |
713 | } | |
714 | } | |
715 | } | |
716 | ||
717 | // verify that all mandatory options were given | |
718 | if ( ok ) | |
719 | { | |
720 | size_t countOpt = m_data->m_options.GetCount(); | |
721 | for ( size_t n = 0; ok && (n < countOpt); n++ ) | |
722 | { | |
723 | wxCmdLineOption& opt = m_data->m_options[n]; | |
724 | if ( (opt.flags & wxCMD_LINE_OPTION_MANDATORY) && !opt.HasValue() ) | |
725 | { | |
726 | wxString optName; | |
727 | if ( !opt.longName ) | |
728 | { | |
729 | optName = opt.shortName; | |
730 | } | |
731 | else | |
732 | { | |
733 | optName.Printf(_("%s (or %s)"), | |
734 | opt.shortName.c_str(), | |
735 | opt.longName.c_str()); | |
736 | } | |
737 | ||
738 | wxLogError(_("The value for the option '%s' must be specified."), | |
739 | optName.c_str()); | |
740 | ||
741 | ok = FALSE; | |
742 | } | |
743 | } | |
744 | ||
745 | for ( ; ok && (currentParam < countParam); currentParam++ ) | |
746 | { | |
747 | wxCmdLineParam& param = m_data->m_paramDesc[currentParam]; | |
748 | if ( (currentParam == countParam - 1) && | |
749 | (param.flags & wxCMD_LINE_PARAM_MULTIPLE) && | |
750 | hadRepeatableParam ) | |
751 | { | |
752 | // special case: currentParam wasn't incremented, but we did | |
753 | // have it, so don't give error | |
754 | continue; | |
755 | } | |
756 | ||
757 | if ( !(param.flags & wxCMD_LINE_PARAM_OPTIONAL) ) | |
758 | { | |
759 | wxLogError(_("The required parameter '%s' was not specified."), | |
760 | param.description.c_str()); | |
761 | ||
762 | ok = FALSE; | |
763 | } | |
764 | } | |
765 | } | |
766 | ||
767 | if ( !ok ) | |
768 | { | |
769 | Usage(); | |
770 | } | |
771 | ||
772 | return ok ? 0 : helpRequested ? -1 : 1; | |
773 | } | |
774 | ||
775 | // ---------------------------------------------------------------------------- | |
776 | // give the usage message | |
777 | // ---------------------------------------------------------------------------- | |
778 | ||
779 | void wxCmdLineParser::Usage() | |
780 | { | |
2822ee33 VZ |
781 | wxString appname = wxTheApp->GetAppName(); |
782 | if ( !appname ) | |
783 | { | |
784 | wxCHECK_RET( !m_data->m_arguments.IsEmpty(), _T("no program name") ); | |
785 | ||
786 | appname = wxFileNameFromPath(m_data->m_arguments[0]); | |
787 | wxStripExtension(appname); | |
788 | } | |
789 | ||
9f83044f | 790 | wxString brief, detailed; |
2822ee33 | 791 | brief.Printf(_("Usage: %s"), appname.c_str()); |
9f83044f VZ |
792 | |
793 | size_t n, count = m_data->m_options.GetCount(); | |
794 | for ( n = 0; n < count; n++ ) | |
795 | { | |
796 | wxCmdLineOption& opt = m_data->m_options[n]; | |
797 | ||
798 | brief << _T(' '); | |
799 | if ( !(opt.flags & wxCMD_LINE_OPTION_MANDATORY) ) | |
800 | { | |
801 | brief << _T('['); | |
802 | } | |
803 | ||
804 | brief << _T('-') << opt.shortName; | |
805 | detailed << _T(" -") << opt.shortName; | |
806 | if ( !!opt.longName ) | |
807 | { | |
808 | detailed << _T(" --") << opt.longName; | |
809 | } | |
810 | ||
811 | if ( opt.kind != wxCMD_LINE_SWITCH ) | |
812 | { | |
813 | wxString val; | |
814 | val << _T('<') << GetTypeName(opt.type) << _T('>'); | |
815 | brief << _T(' ') << val; | |
816 | detailed << (!opt.longName ? _T(':') : _T('=')) << val; | |
817 | } | |
818 | ||
819 | if ( !(opt.flags & wxCMD_LINE_OPTION_MANDATORY) ) | |
820 | { | |
821 | brief << _T(']'); | |
822 | } | |
823 | ||
824 | detailed << _T('\t') << opt.description << _T('\n'); | |
825 | } | |
826 | ||
827 | count = m_data->m_paramDesc.GetCount(); | |
828 | for ( n = 0; n < count; n++ ) | |
829 | { | |
830 | wxCmdLineParam& param = m_data->m_paramDesc[n]; | |
831 | ||
832 | brief << _T(' '); | |
833 | if ( param.flags & wxCMD_LINE_PARAM_OPTIONAL ) | |
834 | { | |
835 | brief << _T('['); | |
836 | } | |
837 | ||
838 | brief << param.description; | |
839 | ||
840 | if ( param.flags & wxCMD_LINE_PARAM_MULTIPLE ) | |
841 | { | |
842 | brief << _T("..."); | |
843 | } | |
844 | ||
845 | if ( param.flags & wxCMD_LINE_PARAM_OPTIONAL ) | |
846 | { | |
847 | brief << _T(']'); | |
848 | } | |
849 | } | |
850 | ||
e612f101 VZ |
851 | if ( !!m_data->m_logo ) |
852 | { | |
853 | wxLogMessage(m_data->m_logo); | |
854 | } | |
855 | ||
9f83044f VZ |
856 | wxLogMessage(brief); |
857 | wxLogMessage(detailed); | |
858 | } | |
859 | ||
860 | // ---------------------------------------------------------------------------- | |
861 | // global functions | |
862 | // ---------------------------------------------------------------------------- | |
863 | ||
864 | static wxString GetTypeName(wxCmdLineParamType type) | |
865 | { | |
866 | wxString s; | |
867 | switch ( type ) | |
868 | { | |
869 | default: | |
870 | wxFAIL_MSG( _T("unknown option type") ); | |
871 | // still fall through | |
872 | ||
873 | case wxCMD_LINE_VAL_STRING: s = _("str"); break; | |
874 | case wxCMD_LINE_VAL_NUMBER: s = _("num"); break; | |
875 | case wxCMD_LINE_VAL_DATE: s = _("date"); break; | |
876 | } | |
877 | ||
878 | return s; | |
879 | } |