CommandLineParse類(命令行解析類)


https://blog.csdn.net/jkhere/article/details/8674019

https://sophia0130.github.io/2018/05/08/CommandLineParse%E7%B1%BB/

https://blog.csdn.net/ylf_2278880589/article/details/80811304

Java命令行選項解析之Commons-CLI & Args4J & JCommander

CommandLineParser類:命令行解析

這個類的出現主要是方便用戶在命令行使用過程中減少工作量,可以在程序文件中直接指定命令行中的參數指令,方便了調試。

1. C++ 例子:

 

#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
 
#include <iostream>
#include <ctype.h>
#include <string>
 
using namespace cv;
using namespace std;
 
const char* keys =
{
    "{ c | camera | 0 | use camera or not}"
    "{ fn | filename |xxxx.avi | movie file}"
    "{ t | test | test string | good day!}"
};
 
 
int main(int argc, const char** argv )
{
 
    CommandLineParser parser(argc, argv, keys);
    
    bool useCamera = parser.get<bool>("c");//括號里寫成“camera”也可以
    string file = parser.get<string>("fn");
    string third = parser.get<string>("t");
 
    //打印輸出
    cout<<useCamera<<endl;
    cout<<file<<endl;
    cout<<third<<endl;
    cout<<endl;
    parser.printParams();//CommandLineParser的成員函數,打印全部參數,還有其他成員函數,如:has(),getString()等
    return 0;
}
View Code

 

運行結果:

 

 

大概可以看出來用這個類的好處就是很方便,因為以前版本沒這個類時,如果要運行帶參數的.exe,必須在命令行中輸入文件路徑以及各種參數,並且輸入的參數格式要與代碼中的if語句判斷內容格式一樣,一不小心就輸錯了,很不方便。另外如果想要更改輸入格式的話在主函數文件中要相應更改很多地方。現在有了這個類,只需要改keys里面的內容就可以了,並且可以直接運行,不需要cmd命令行帶參運行。最后這個類封裝了很多函數,可以直接用,只不過這個本來就是類結構的優點。
————————————————
版權聲明:本文為CSDN博主「JKhere」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/jkhere/article/details/8674019

2.更簡單的c++例子:

 

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;

const char* keys =
{
    "{input |C:/Users/king/Pictures/Saved Pictures/1.jpg| string |input image}"
};
    //分別表示簡稱,文件來源,文件值和幫助語句    

int main(int argc, char** argv)
{
    Mat src, dst;

    const char* source_window = "Source image";
    const char* equalized_window = "Equalized Image";

    // Load image
    CommandLineParser parser(argc, argv, keys);
    src = imread(parser.get<String>("input"), IMREAD_COLOR);
    if (src.empty())
    {
        cout << "Could not open or find the image!\n" << endl;
        return -1;
    }

    cvtColor(src, src, COLOR_BGR2GRAY);
    equalizeHist(src, dst);

    namedWindow(source_window, WINDOW_AUTOSIZE);
    namedWindow(equalized_window, WINDOW_AUTOSIZE);

    imshow(source_window, src);
    imshow(equalized_window, dst);

    waitKey(0);
    return 0;

}
View Code

 

1.這個類的作用

以前沒這個類時,如果要運行帶參數的.exe,必須在命令行中輸入文件路徑以及各種參數,一不小心就輸錯了,很不方便。
現在有了這個類,只需要改keys里面的內容就可以了,並且運行時可以直接在vs下用F5,不需要cmd命令行帶參運行

2.keys

keys中間的內容分成4斷,用”|”分隔開,分別表示簡稱,文件來源,文件值和幫助語句 PS:文件的值我不是很理解,如果類型是bool,那就有值0或,如果是類型是圖像,它的值可以省略,我這邊文件的值用了string,也運行成功了。

 

3. Java構建命令行啟動模式CommandLineParser/Options

public void parseArgs(String[] args) throws ParseException {
    // Create a Parser
    CommandLineParser parser = new BasicParser( );
    Options options = new Options( );
    options.addOption("h", "help", false, "Print this usage information");
    options.addOption("c", "cfg", true, "config Absolute Path");
    options.addOption("l", "log", true, "log configuration");

    // Parse the program arguments
    CommandLine commandLine = parser.parse( options, args );
    // Set the appropriate variables based on supplied options

    if( commandLine.hasOption('h') ) {
        printHelpMessage();
        System.exit(0);
    }
    if( commandLine.hasOption('c') ) {
        cfg = new File(commandLine.getOptionValue('c'));
    } else {
        printHelpMessage();
        System.exit(0);
    }
    if( commandLine.hasOption('l') ) {
        log = new File(commandLine.getOptionValue('l'));
    } else {
        printHelpMessage();
        System.exit(0);
    }
}
public void printHelpMessage() {
    System.out.println( "Change the xml File and Log.XML Path to the right Absolute Path base on your project Location in your computor");
    System.out.println("Usage example: ");
    System.out.println( "java -cfg D:\\MyProject\\face2face\\logic\\src\\main\\resources\\logic.xml  -log D:\\MyProject\\face2face\\logic\\src\\main\\resources\\log.xml");
    System.exit(0);
}
View Code

在上述代碼中

options.addOption("c", "cfg", true, "config Absolute Path"); //第三個參數為true表示需要額外參數:
       linux 命令行約定:
       幾乎所有的GNU/Linux程序都遵循一些命令行參數定義的約定。程序希望出現的參數可以分成兩種:選項(options or flags)、其他類型的的參數。Options修飾了程序運行的方式,其他類型的參數則提供了輸入(例如,輸入文件的名稱)。
       對於options類型參數可以有兩種方式:
       1)短選項(short options):顧名思義,就是短小參數。它們通常包含一個連字號和一個字母(大寫或小寫字母)。例如:-s,-h等。
       2)長選項(long options):長選項,包含了兩個連字號和一些大小寫字母組成的單詞。例如,--size,--help等。
       *注:一個程序通常會提供包括short options和long options兩種參數形式的參數。
  以上代碼中的"c"和"cfg"就是短選項和長選項.

如定義:

String[] arg = {"-t","-c","hello"};//這里參數c在下面定義為需要額外參數,這里是"hello"
public void testOption(String[] args) throws ParseException{

Options options = new Options();
options.addOption("t",false,"display current time");
options.addOption("c",true,"country code");
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse( options, args);
if(cmd.hasOption("t")) {
System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date())+" in "+cmd.getOptionValue("c"));//這里調用cmd.getOptionValue("c")會輸出hello     
}else{
System.out.println((new SimpleDateFormat("yyyy-MM-dd")).format(new Date()));
}} 
View Code

————————————————
版權聲明:本文為CSDN博主「靜心安心」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/ylf_2278880589/article/details/80811304

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM