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