Exemple #1
0
/*********************************************************************
 ** METHOD  :
 ** PURPOSE :
 ** INPUT   :
 ** OUTPUT  :
 ** RETURN  :
 ** REMARKS :
 *********************************************************************/
void OptionsHelpVisitor::visitOption (Option& object, size_t depth)
{
    if (!object.getName().empty() && object.isVisible())
    {
        if (object.getNbArgs() > 0)
        {
            indent(os,depth) << Stringify::format ("    %-*s (%d arg) :    %s",
                (int)nameMaxLen,
                object.getName().c_str(),
                (int)object.getNbArgs(),
                object.getHelp().c_str(),
                object.getDefaultValue().c_str()
            );

            if (object.isMandatory()==false)  {  os << Stringify::format ("  [default '%s']", object.getDefaultValue().c_str());  }

            os << endl;
        }
        else
        {
            indent(os,depth) << Stringify::format ("    %-*s (%d arg) :    %s\n",
                (int)nameMaxLen,
                object.getName().c_str(),
                (int)object.getNbArgs(),
                object.getHelp().c_str()
            );
        }
    }
}
Exemple #2
0
void CLIOptions::parse(const QStringList& cmd_line_args)
{
	qfLogFuncFrame() << cmd_line_args.join(' ');
	m_isAppBreak = false;
	m_parsedArgIndex = 0;
	m_arguments = cmd_line_args.mid(1);
	m_unusedArguments = QStringList();
	m_parseErrors = QStringList();
	m_allArgs = cmd_line_args;

	while(true) {
		QString arg = takeArg();
		if(arg.isEmpty())
			break;
		if(arg == "--help" || arg == "-h") {
			setHelp(true);
			printHelp();
			m_isAppBreak = true;
			return;
		}
		else {
			bool found = false;
			QMutableMapIterator<QString, Option> it(m_options);
			while(it.hasNext()) {
				it.next();
				Option &opt = it.value();
				QStringList names = opt.names();
				if(names.contains(arg)) {
					found = true;
					if(opt.type() != QVariant::Bool) {
						arg = takeArg();
						opt.setValueString(arg);
					}
					else {
						//LOGDEB() << "setting true";
						opt.setValue(true);
					}
					break;
				}
			}
			if(!found) {
				if(arg.startsWith("-"))
					m_unusedArguments << arg;
			}
		}
	}
	{
		QMapIterator<QString, Option> it(m_options);
		while(it.hasNext()) {
			it.next();
			Option opt = it.value();
			//LOGDEB() << "option:" << it.key() << "is mandatory:" << opt.isMandatory() << "is valid:" << opt.value().isValid();
			if(opt.isMandatory() && !opt.value().isValid()) {
				addParseError(QString("Mandatory option '%1' not set.").arg(opt.names().value(0)));
			}
		}
	}
	qf::core::Exception::setAbortOnException(isAbortOnException());
}
Exemple #3
0
void CLIOptions::printHelp(QTextStream& os) const
{
	os << applicationName() << " [OPTIONS]" << endl << endl;
	os << "OPTIONS:" << endl << endl;
	QMapIterator<QString, Option> it(m_options);
	while(it.hasNext()) {
		it.next();
		Option opt = it.value();
		os << opt.names().join(", ");
		if(opt.type() != QVariant::Bool) {
			if(opt.type() == QVariant::Int || opt.type() == QVariant::Double) os << " " << "number";
			else os << " " << "'string'";
		}
		os << ':';
		QVariant def_val = opt.defaultValue();
		if(def_val.isValid()) os << " [default(" << def_val.toString() << ")]";
		if(opt.isMandatory()) os << " [MANDATORY]";
		os << endl;
		QString oc = opt.comment();
		if(!oc.isEmpty()) os << "\t" << opt.comment() << endl;
	}
}