[英]QCommandLineOption can't get option
我使用QCommandLineOption
解析我的命令行選項。
這是我的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),它可以正常工作,但我無法獲取begin選項的值:
QString val = parser.value("begin"); // return always : ""
./tool http://google.com ./ --begin =5
頁面開始:0
頁末:0
./tool http://google.com ./ --begin=5
'--begin'之后的意外值。
./tool http://google.com ./ -b 5
頁面開始:0
頁末:0
真的,我不明白為什么?
您需要告訴選項它應該期望一個值。
QCommandLineOption::QCommandLineOption
: 此外,如果選項需要一個值,則可以設置valueName。
QCommandLineOption::setValueName
: 未分配值的選項具有類似布爾的行為:用戶指定--option或不指定。 分配了值的選項需要為期望值設置名稱,以便在幫助輸出中記錄該選項。 名稱為o和output的選項以及文件的值名稱為-o, --output <file>
。
QCommandLineOption beginOption(QStringList() << "b" << "begin", QApplication::translate("main", "number of the page where it starts to print"), "page", "0");
QCommandLineOption
構造函數的文檔說:
QCommandLineOption::QCommandLineOption(const QStringList & names, const QString & description = QString(), const QString & valueName = QString(), const QString & defaultValue = QString())
[...]
另外,如果選項需要一個值,則可以設置valueName 。 該選項的默認值設置為defaultValue 。
您需要傳遞一個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.