[英]QCommandLineOption can't get option
I use QCommandLineOption
for parse my commande line option. 我使用QCommandLineOption
解析我的命令行选项。
this is my Arguments
class : 这是我的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);
}
if I use orientation
option (or colorMode) it's work fine but I can't get value of begin option : 如果我使用orientation
选项(或colorMode),它可以正常工作,但我无法获取begin选项的值:
QString val = parser.value("begin"); // return always : ""
./tool http://google.com ./ --begin =5
page begin : 0 页面开始:0
page end : 0 页末:0
./tool http://google.com ./ --begin=5
Unexpected value after '--begin'. '--begin'之后的意外值。
./tool http://google.com ./ -b 5
page begin : 0 页面开始:0
page end : 0 页末:0
Realy, I don't understand why ? 真的,我不明白为什么?
You need to tell the option that it should expect a value. 您需要告诉选项它应该期望一个值。
QCommandLineOption::QCommandLineOption
: In addition, the valueName can be set if the option expects a value. QCommandLineOption::QCommandLineOption
: 此外,如果选项需要一个值,则可以设置valueName。
QCommandLineOption::setValueName
: Options without a value assigned have a boolean-like behavior: either the user specifies --option or they don't. QCommandLineOption::setValueName
: 未分配值的选项具有类似布尔的行为:用户指定--option或不指定。 Options with a value assigned need to set a name for the expected value, for the documentation of the option in the help output. 分配了值的选项需要为期望值设置名称,以便在帮助输出中记录该选项。 An option with names o and output, and a value name of file will appear as -o, --output <file>
. 名称为o和output的选项以及文件的值名称为-o, --output <file>
。
QCommandLineOption beginOption(QStringList() << "b" << "begin", QApplication::translate("main", "number of the page where it starts to print"), "page", "0");
The documentation for the QCommandLineOption
constructor says: QCommandLineOption
构造函数的文档说:
QCommandLineOption::QCommandLineOption(const QStringList & names, const QString & description = QString(), const QString & valueName = QString(), const QString & defaultValue = QString())
[...] [...]
In addition, the valueName can be set if the option expects a value. 另外,如果选项需要一个值,则可以设置valueName 。 The default value for the option is set to defaultValue . 该选项的默认值设置为defaultValue 。
You need to pass a valueName
(the third argument to the constructor) to tell it that the option can have a value, rather than just being a boolean on/off option: 您需要传递一个valueName
(构造函数的第三个参数)来告诉它该选项可以具有一个值,而不仅仅是一个布尔型on / off选项:
QCommandLineOption beginOption(QStringList() << "b" << "begin", QApplication::translate("main", "number of the page where it starts to print"), "page", "0");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.