简体   繁体   English

QCommandLineOption读取输入文件

[英]QCommandLineOption reading input files

I'm working on a code using QCommandLineOption to read input files (jpg files in this case). 我正在使用QCommandLineOption读取输入文件(在这种情况下为jpg文件)的代码。 I'm trying to wrap my head around how to properly add the filepath & names to it to access the datas, but it doesn't work. 我试图将注意力集中在如何正确地向其添加文件路径和名称以访问数据上,但这是行不通的。 Here's the code : 这是代码:

#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;
}

I'm getting "!! Mask is NOT SET and must be set!" 我收到“ !!未设置面具,必须设置!” when compiling, which means the string maskFileName is empty. 编译时,这意味着字符串maskFileName为空。 Any ideas ? 有任何想法吗 ?

Your program works fine. 您的程序运行正常。 I just compiled it and tried, it works. 我只是编译并尝试了,它起作用了。

The problem is with the sample arguments you gave it: 问题出在您给它的示例参数上:

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

You need to remove the spaces in -- mask . 您需要删除的空间-- mask So change ti to: 因此将ti更改为:

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

On a side note, when dealing with file path it's a good idea to wrap them in double quotes to allow for spaces in your path: 附带说明一下,在处理文件路径时,最好将它们用双引号引起来,以在路径中留出空格:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM