網(wǎng)站首頁 編程語言 正文
在開發(fā)過程中,由于需要,有時候要對程序中的命令行進行使用,比如最近的開發(fā)任務(wù)需要用外部exe啟動我們的軟件,啟動的時候傳入一些必要的命令行參數(shù),使我們軟件啟動的時候根據(jù)這些參數(shù)進行一些必要的操作,這個時候就可以使用Qt自帶的QCommandLineParser這個類。
Qt中是用QCommandLineOption和QCommandLineParser這兩個類配合進行應(yīng)用的解析。下面就直接貼出一個例子的簡單使用,方便大家理解,其中涉及到一些東西也很多,長,短命令,以及多參,從最基礎(chǔ)的用法入手,后面自己學(xué)習擴展即可。
封裝了一個用例類(舉例了一個軟件自啟動的例子):
頭文件:
/*
軟件命令行解析
1.自啟動 -r=auto
2.某路徑 -o=path
3.某路徑 -b=path
4.xxxxx -x=xxxx
5....... .......
*/
#pragma once
#include
class CommandLineParser{
public:
//@brief:是否有軟件自啟動命令行
static bool IsSetAutoRun();
//@brief:獲取軟件自啟動命令行數(shù)值
static QString GetAutoRunValue();
private:
BFCommandLineParser();
~BFCommandLineParser();
BFCommandLineParser(BFCommandLineParser&)=delete;
BFCommandLineParser&operator=(BFCommandLineParser&)= delete;
};
實現(xiàn)文件:
#include "BFCommandLineParser.h"
#include
#include
#include
CommandLineParser::CommandLineParser()
{
//構(gòu)造
}
CommandLineParser::~CommandLineParser()
{
//析構(gòu)
}
bool BFCommandLineParser::IsSetAutoRun() {
//解析對象
QCommandLineParser command_parser;
//解析選項對象
QCommandLineOption option("a");
//參數(shù)期望的數(shù)值,如果不設(shè)置,使用process解析會異常退出
option.setValueName("auto");
//添加解析選項
command_parser.addOption(option);
//開始解析
command_parser.parse(qApp->arguments());
qDebug() << command_parser.isSet(option);
//應(yīng)用程序的命令行是否含有該解析選項
return command_parser.isSet(option);
}
QString BFCommandLineParser::GetAutoRunValue() {
QCommandLineParser command_parser;
QCommandLineOption option("a");
option.setValueName("auto");
command_parser.addOption(option);
command_parser.parse(qApp->arguments());
QString str;
if (command_parser.isSet(option)) {
//獲取應(yīng)用程序的命令行中含有該解析選項的數(shù)值
str = command_parser.value(option);
}
return str;
}
自己遇到的問題和注意點:
1.QCommandLineOption::parser和QCommandLineOption::process效果類似,后者要是程序的命令行沒有選項程序會一場退出。
2.如果是有參數(shù)的選項,要設(shè)置期望名字QCommandLineOption::setValueName 然后在解析
3.順序不要錯誤,設(shè)置完選項之后再調(diào)用QCommandLineOption::parser或者QCommandLineOption::process解析。
4.獲取數(shù)值的時候如果調(diào)用的是values而不是value發(fā)現(xiàn)就不準確,其他選項任何包含該關(guān)鍵詞的字符串都會被輸出出來。
原文鏈接:https://blog.csdn.net/qq_36533978/article/details/124689694
相關(guān)推薦
- 2022-04-28 WPF使用Grid網(wǎng)格面板布局_實用技巧
- 2022-11-17 WPF利用DrawingContext實現(xiàn)繪制溫度計_C#教程
- 2022-06-11 FreeRTOS進階之系統(tǒng)延時完全解析_操作系統(tǒng)
- 2023-05-22 python中decimal模塊的用法_python
- 2022-11-12 C語言內(nèi)存操作函數(shù)使用示例梳理講解_C 語言
- 2022-09-14 Flutter?iOS開發(fā)OC混編Swift動態(tài)庫和靜態(tài)庫問題填坑_Swift
- 2023-05-08 Linux?C/C++?timeout命令實現(xiàn)運行具有時間限制功能_C 語言
- 2022-10-22 Python中的list.sort()方法和函數(shù)sorted(list)_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支