// Created: 05.01.00
// RCS-ID: $Id$
// Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
-// Licence: wxWindows license
+// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
// ============================================================================
GetLongOptionName(lng).Len() == lng.Len(),
wxT("Long option contains invalid characters")
);
+
kind = k;
{ Check(wxCMD_LINE_VAL_NUMBER); return m_longVal; }
const wxString& GetStrVal() const
{ Check(wxCMD_LINE_VAL_STRING); return m_strVal; }
+#if wxUSE_DATETIME
const wxDateTime& GetDateVal() const
{ Check(wxCMD_LINE_VAL_DATE); return m_dateVal; }
+#endif // wxUSE_DATETIME
void SetLongVal(long val)
{ Check(wxCMD_LINE_VAL_NUMBER); m_longVal = val; m_hasVal = TRUE; }
void SetStrVal(const wxString& val)
{ Check(wxCMD_LINE_VAL_STRING); m_strVal = val; m_hasVal = TRUE; }
+#if wxUSE_DATETIME
void SetDateVal(const wxDateTime val)
{ Check(wxCMD_LINE_VAL_DATE); m_dateVal = val; m_hasVal = TRUE; }
+#endif // wxUSE_DATETIME
void SetHasValue(bool hasValue = TRUE) { m_hasVal = hasValue; }
bool HasValue() const { return m_hasVal; }
long m_longVal;
wxString m_strVal;
+#if wxUSE_DATETIME
wxDateTime m_dateVal;
+#endif // wxUSE_DATETIME
};
struct wxCmdLineParam
return TRUE;
}
+#if wxUSE_DATETIME
bool wxCmdLineParser::Found(const wxString& name, wxDateTime *value) const
{
int i = m_data->FindOption(name);
return TRUE;
}
+#endif // wxUSE_DATETIME
size_t wxCmdLineParser::GetParamCount() const
{
optInd = m_data->FindOptionByLongName(name);
if ( optInd == wxNOT_FOUND )
{
- errorMsg << wxString::Format(_("Unknown long option '%s'"), name.c_str()) << "\n";
+ errorMsg << wxString::Format(_("Unknown long option '%s'"), name.c_str()) << wxT("\n");
}
}
else
// Print the argument including leading "--"
name.Prepend( wxT("--") );
- errorMsg << wxString::Format(_("Unknown option '%s'"), name.c_str()) << "\n";
+ errorMsg << wxString::Format(_("Unknown option '%s'"), name.c_str()) << wxT("\n");
}
}
{
// we couldn't find a valid option name in the
// beginning of this string
- errorMsg << wxString::Format(_("Unknown option '%s'"), name.c_str()) << "\n";
+ errorMsg << wxString::Format(_("Unknown option '%s'"), name.c_str()) << wxT("\n");
break;
}
if ( *p++ != _T('=') )
{
- errorMsg << wxString::Format(_("Option '%s' requires a value, '=' expected."), name.c_str()) << "\n";
+ errorMsg << wxString::Format(_("Option '%s' requires a value, '=' expected."), name.c_str()) << wxT("\n");
ok = FALSE;
}
{
// ... but there is none
errorMsg << wxString::Format(_("Option '%s' requires a value."),
- name.c_str()) << "\n";
+ name.c_str()) << wxT("\n");
ok = FALSE;
}
if ( opt.flags & wxCMD_LINE_NEEDS_SEPARATOR )
{
errorMsg << wxString::Format(_("Separator expected after the option '%s'."),
- name.c_str()) << "\n";
+ name.c_str()) << wxT("\n");
ok = FALSE;
}
else
{
errorMsg << wxString::Format(_("'%s' is not a correct numeric value for option '%s'."),
- value.c_str(), name.c_str()) << "\n";
+ value.c_str(), name.c_str()) << wxT("\n");
ok = FALSE;
}
}
break;
+#if wxUSE_DATETIME
case wxCMD_LINE_VAL_DATE:
{
wxDateTime dt;
if ( !res || *res )
{
errorMsg << wxString::Format(_("Option '%s': '%s' cannot be converted to a date."),
- name.c_str(), value.c_str()) << "\n";
+ name.c_str(), value.c_str()) << wxT("\n");
ok = FALSE;
}
}
}
break;
+#endif // wxUSE_DATETIME
}
}
}
}
else
{
- errorMsg << wxString::Format(_("Unexpected parameter '%s'"), arg.c_str()) << "\n";
+ errorMsg << wxString::Format(_("Unexpected parameter '%s'"), arg.c_str()) << wxT("\n");
ok = FALSE;
}
}
else
{
- optName.Printf(_("%s (or %s)"),
- opt.shortName.c_str(),
- opt.longName.c_str());
+ if ( AreLongOptionsEnabled() )
+ {
+ optName.Printf( _("%s (or %s)"),
+ opt.shortName.c_str(),
+ opt.longName.c_str() );
+ }
+ else
+ {
+ optName.Printf( wxT("%s"),
+ opt.shortName.c_str() );
+ }
}
errorMsg << wxString::Format(_("The value for the option '%s' must be specified."),
- optName.c_str()) << "\n";
+ optName.c_str()) << wxT("\n");
ok = FALSE;
}
if ( !(param.flags & wxCMD_LINE_PARAM_OPTIONAL) )
{
errorMsg << wxString::Format(_("The required parameter '%s' was not specified."),
- param.description.c_str()) << "\n";
+ param.description.c_str()) << wxT("\n");
ok = FALSE;
}
}
}
- if ( !ok && errorMsg.length() != 0 )
+ // if there was an error during parsing the command line, show this error
+ // and also the usage message if it had been requested
+ if ( !ok && (!errorMsg.empty() || (helpRequested && showUsage)) )
{
- wxString usage;
wxMessageOutput* msgOut = wxMessageOutput::Get();
- if ( showUsage ) usage = GetUsageString();
if ( msgOut )
+ {
+ wxString usage;
+ if ( showUsage )
+ usage = GetUsageString();
+
msgOut->Printf( wxT("%s%s"), usage.c_str(), errorMsg.c_str() );
+ }
+ else
+ {
+ wxFAIL_MSG( _T("no wxMessageOutput object?") );
+ }
}
return ok ? 0 : helpRequested ? -1 : 1;
void wxCmdLineParser::Usage()
{
- wxString usage = GetUsageString();
wxMessageOutput* msgOut = wxMessageOutput::Get();
if ( msgOut )
- msgOut->Printf( wxT("%s"), usage.c_str() );
+ {
+ msgOut->Printf( wxT("%s"), GetUsageString().c_str() );
+ }
+ else
+ {
+ wxFAIL_MSG( _T("no wxMessageOutput object?") );
+ }
}
wxString wxCmdLineParser::GetUsageString()
wxString usage;
wxArrayString namesOptions, descOptions;
- if ( !!m_data->m_logo )
+ if ( !m_data->m_logo.empty() )
{
usage << m_data->m_logo << _T('\n');
}
wxFAIL_MSG( _T("unknown option type") );
// still fall through
- case wxCMD_LINE_VAL_STRING: s = _("str"); break;
- case wxCMD_LINE_VAL_NUMBER: s = _("num"); break;
- case wxCMD_LINE_VAL_DATE: s = _("date"); break;
+ case wxCMD_LINE_VAL_STRING:
+ s = _("str");
+ break;
+
+ case wxCMD_LINE_VAL_NUMBER:
+ s = _("num");
+ break;
+
+ case wxCMD_LINE_VAL_DATE:
+ s = _("date");
+ break;
}
return s;
In particular, to pass a single argument containing a space to the program
it should be quoted:
-
+
myprog.exe foo bar -> argc = 3, argv[1] = "foo", argv[2] = "bar"
myprog.exe "foo bar" -> argc = 2, argv[1] = "foo bar"