QCommandLineOption无法获得选项

时间:2014-08-15 12:07:40

标签: c++ qt

我使用QCommandLineOption来解析我的commande行选项。

这是我的Arguments课程:

#include "arguments.h"
#include <QDebug>

/**
 * @brief Constructor, need to know QApplication pointer
 * @param app
 */
Arguments::Arguments(QApplication *app)
{
    parser.setApplicationDescription("Convert html doc to pdf");
    parser.addHelpOption();
    parser.addVersionOption();
    parser.addPositionalArgument("source", QApplication::translate("main", "Source file to copy."));
    parser.addPositionalArgument("destination", QApplication::translate("main", "Destination file (ie /home/morgan/test.pdf)"));

    setOption();

    // Process the actual command line arguments given by the user
    parser.process(*app);
    args = parser.positionalArguments();
}

/**
 * @brief Arguments::getSource
 * @return QString the full path to file to be converted
 */
QString Arguments::getSource() { return args.isEmpty() ? QString() : args.at(0); }

/**
 * @brief Arguments::getDest
 * @return QString the path to file that will be converted
 */
QString Arguments::getDest() { return args.isEmpty() ? QString() : args.at(1); }


/**
 * @brief Arguments::getOrientation
 * @return Orientation Mode (Portrait or Landscape ?)
 */
QPrinter::Orientation Arguments::getOrientation()
{
    // init orientation options values
    QStringList orientationOptions;
    orientationOptions << "portrait" << "landscape" ;

    // get orientation value
    QString orientation = parser.value("orientation").toLower();

    switch (orientationOptions.indexOf(orientation)) {
    case 0:
        return QPrinter::Portrait;
        break;
    case 1:
        return QPrinter::Landscape;
        break;
    default:
        return QPrinter::Portrait;
        break;
    }
}

/**
 * @brief should print in color or not ?
 * @return color mode
 */
QPrinter::ColorMode Arguments::getColorMode()
{
    bool gray = parser.isSet("gray");
    if(gray == true)
        return QPrinter::GrayScale;
    else
        return QPrinter::Color;
}

int Arguments::getPageBegin() {
    QString val = parser.value("begin");
    int v = val.toInt();
    return v; }

int Arguments::getPageEnd() { return parser.value("end").toInt(); }

/**
 * @brief Arguments::isValidArgument
 * @return the app have all necesary arguments ?
 */
bool Arguments::isValidArgument()
{
    if (args.size() < 2) {
        fprintf(stderr, "%s\n", qPrintable(QApplication::translate("main", "Error: Must specify arguments.")));
        parser.showHelp(1);
        return false;
    } else {
        return true;
    }
}

/**
 * @brief Add all option can be given by argument
 */
void Arguments::setOption()
{
    // A String  orientation mode (-o, --orientation)
    QCommandLineOption orientationOption(QStringList() << "o" << "orientation", QApplication::translate("main", "set orientation to Landscape or Portrait (default is Portrait)."), QApplication::translate("main", "orientation"), "Portrait");
    parser.addOption(orientationOption);

    // print to gray mode (-g, --gray)
    QCommandLineOption grayOption(QStringList() << "g" << "gray", QApplication::translate("main", "should print in gray or not"));
    parser.addOption(grayOption);

    // int page start to print (-b --begin)
    QCommandLineOption beginOption(QStringList() << "b" << "begin", QApplication::translate("main", "number of the page where it starts to print"));
    parser.addOption(beginOption);

    QCommandLineOption endOption(QStringList() << "e" << "end", QApplication::translate("main", "number of the page where it stop to print"));
    parser.addOption(endOption);
}

如果我使用orientation选项(或colorMode),它的工作正常,但我无法获得开始选项的价值:

QString val = parser.value("begin"); // return always : ""

实例用法:

./tool http://google.com ./ --begin =5

  

页面开始:0
  页面结束:0

./tool http://google.com ./ --begin=5

  

&#39; - 开始&#39;之后的意外值。

./tool http://google.com ./ -b 5

  

页面开始:0
  页面结束:0

真的,我不明白为什么?

2 个答案:

答案 0 :(得分:15)

答案 1 :(得分:5)

QCommandLineOption构造函数的documentation说:

  

QCommandLineOption::QCommandLineOption(const QStringList & names, const QString & description = QString(), const QString & valueName = QString(), const QString & defaultValue = QString())

     

[...]

     

此外,如果选项需要值,则可以设置 valueName 。该选项的默认值设置为 defaultValue

你需要将valueName(第三个参数传递给构造函数)告诉它该选项可以有一个值,而不仅仅是一个布尔开/关选项:

QCommandLineOption beginOption(QStringList() << "b" << "begin", QApplication::translate("main", "number of the page where it starts to print"), "page", "0");