QCommandLineOption读取输入文件

时间:2016-05-07 17:36:23

标签: c++ qt-creator qcommandlineparser

我正在使用QCommandLineOption处理代码以读取输入文件(在本例中为jpg文件)。我试图围绕如何正确添加文件路径&为了访问数据而命名,但它不起作用。这是代码:

#include <iostream>
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QString>
#include <QImage>
#include "imageconvert.h"
#include "clanu_process.h"

//--input=/Users/fakepath/coming-soon.jpg --     mask=/Users/fakepath/coming-soon_mask.jpg --output=/Users/fakepath/coming-soon_out_IFQ1.jpg

int main(int argc, char *argv[])
 {
    // ------------------------------------------
    //Command line parameters management
    QCoreApplication app(argc, argv);
    QCoreApplication::setApplicationName("clanu-inpainting");
    QCoreApplication::setApplicationVersion("1.0");

    QCommandLineParser parser;
    parser.setApplicationDescription("Inpainting Console");
    parser.addHelpOption();
    parser.addVersionOption();

    QCommandLineOption inputFileOption(QStringList() << "i" << "input", "Fullpath and extension of the input <file>.", "file");
    parser.addOption(inputFileOption);

    QCommandLineOption maskFileOption(QStringList() << "m" << "mask", "Fullpath and extension of the mask <file>.", "file");
    parser.addOption(maskFileOption);

    QCommandLineOption outputFileOption(QStringList() << "o" << "output", "Fullpath and extension of the output <file>.", "file");
    parser.addOption(outputFileOption);

    // Process the actual command line arguments given by the user
    parser.process(app);

    QString inputFileName  = parser.value(inputFileOption);
    QString maskFileName   = parser.value(maskFileOption);
    QString outputFileName = parser.value(outputFileOption);

    std::cout << " input  " << inputFileName.toStdString()  <<  std::endl;
    std::cout << " output " << outputFileName.toStdString() <<  std::endl;
    std::cout << " mask   " << maskFileName.toStdString()   <<  std::endl;

    if(   maskFileName.isEmpty() ) { std::cout << "!! Mask is NOT SET and must be set!"   << std::endl; return -1; }
    if(  inputFileName.isEmpty() ) { std::cout << "!! Input is NOT SET and must be set!"  << std::endl; return -1; }
    if( outputFileName.isEmpty() ) { std::cout << "!! Output is NOT SET and must be set!" << std::endl; return -1; }

    std::cout << " - Input image file read : " << inputFileName.toStdString() << std::endl;
    std::cout << " - Mask image file read  : " << maskFileName.toStdString() << std::endl;
    std::cout << " - Output image file     : " << outputFileName.toStdString() << std::endl;
    // ------------------------------------------
return 0;
}

我得到&#34; !!面具未设置,必须设置!&#34;编译时,表示字符串maskFileName为空。有任何想法吗 ?

1 个答案:

答案 0 :(得分:0)

您的程序运行正常。我刚刚编译并尝试过,它可以工作。

问题在于您提供的示例参数:

--input=/Users/fakepath/coming-soon.jpg --     mask=/Users/fakepath/coming-soon_mask.jpg --output=/Users/fakepath/coming-soon_out_IFQ1.jpg

您需要删除-- mask中的空格。所以将ti改为:

--input=/Users/fakepath/coming-soon.jpg --mask=/Users/fakepath/coming-soon_mask.jpg --output=/Users/fakepath/coming-soon_out_IFQ1.jpg

另一方面,在处理文件路径时,最好将它们用双引号括起来以允许路径中的空格:

--input="/Users/fakepath/coming soon.jpg" --mask="/Users/fakepath/coming soon mask.jpg" --output="/Users/fakepath/coming soon out IFQ1.jpg"