📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
細 文字列「__FORCETOC__」を「{{#seo: |title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki |keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 |description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This pag… |
|||
| (同じ利用者による、間の3版が非表示) | |||
| 8行目: | 8行目: | ||
== QCommandLineParserクラスの使用 == | == QCommandLineParserクラスの使用 == | ||
以下の例では、<code>QCommandLineParser</code> | 以下の例では、<code>QCommandLineParser</code>クラスを継承したサブクラスを定義して、指定されたオプションを処理している。<br> | ||
<br> | <br> | ||
<code>QCommandLineParser</code>クラスのデフォルトの動作により、例えば、--hオプションが-hオプションと同様に処理されてしまう。<br> | |||
この問題を解決するために、<code>QCommandLineParser</code>クラスの<code>process</code>メソッドをオーバーライドして、カスタムの解析ロジックを実装している。<br> | |||
<br> | |||
正規表現を使用して、有効なオプション (-v, -h, -c, --version, --help, --config) のみを受け付けるようにする。<br> | |||
上記に存在しないオプションが指定された場合は、"不明なオプション"として処理する。<br> | |||
<br> | <br> | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// CustomCommandLineParser.hファイル | |||
#include <QCoreApplication> | #include <QCoreApplication> | ||
#include <QCommandLineParser> | #include <QCommandLineParser> | ||
#include < | #include <QRegularExpression> | ||
#include <QDebug> | #include <QDebug> | ||
class CustomCommandLineParser : public QCommandLineParser | |||
{ | |||
public: | |||
void process(const QStringList &arguments) | |||
{ | |||
QStringList filteredArgs; | |||
static QRegularExpression validOptionRegex("^(-v|-h|-c|--version|--help|--config)(?:=.*)?$"); | |||
for (const QString &arg : arguments) { | |||
if (validOptionRegex.match(arg).hasMatch()) { | |||
filteredArgs << arg; | |||
} | |||
else if (arg.startsWith("-")) { | |||
qDebug() << "不明なオプション:" << arg; | |||
} | |||
else { | |||
filteredArgs << arg; | |||
} | |||
} | |||
QCommandLineParser::process(filteredArgs); | |||
} | |||
}; | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// main.cppファイル | |||
#include "CustomCommandLineParser.h" | |||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
| 27行目: | 60行目: | ||
QCoreApplication::setApplicationVersion("1.0"); | QCoreApplication::setApplicationVersion("1.0"); | ||
CustomCommandLineParser parser; | |||
parser.setApplicationDescription("Command line options example"); | parser.setApplicationDescription("Command line options example"); | ||
QCommandLineOption versionOption(QStringList() << "v" << "version", "Displays version information."); | QCommandLineOption versionOption(QStringList() << "v" << "version", "Displays version information."); | ||
parser.addOption(versionOption); | parser.addOption(versionOption); | ||
QCommandLineOption helpOption(QStringList() << "h" << "help", "Displays help information."); | QCommandLineOption helpOption(QStringList() << "h" << "help", "Displays help information."); | ||
parser.addOption(helpOption); | parser.addOption(helpOption); | ||
QCommandLineOption configOption(QStringList() << "c" << "config", "Display the configuration string.", "config"); | QCommandLineOption configOption(QStringList() << "c" << "config", "Display the configuration string.", "config"); | ||
parser.addOption(configOption); | parser.addOption(configOption); | ||
parser.process( | parser.process(QCoreApplication::arguments()); | ||
if (parser.isSet(helpOption)) { | if (parser.isSet(helpOption)) { | ||
parser.showHelp(); | parser.showHelp(); | ||
| 60行目: | 90行目: | ||
} | } | ||
if (parser.positionalArguments().isEmpty() && !parser.optionNames().isEmpty()) { | |||
qDebug() << "有効なオプションはありません。使用法については、-h または --help を使用してください。"; | |||
} | } | ||
return 0; | return 0; | ||
} | } | ||
| 89行目: | 113行目: | ||
<br> | <br> | ||
<u>※注意</u><br> | <u>※注意</u><br> | ||
<code>QCommandLineParser</code>クラスは、様々なケースに対応できるような構造になっている。<br> | <u><code>QCommandLineParser</code>クラスは、様々なケースに対応できるような構造になっている。</u><br> | ||
<br> | |||
<u><code>QCommandLineParser</code>クラスの詳細は、Qt公式ドキュメントを参照すること。</u><br> | |||
* QCommandLineParserクラス | |||
*: Qt 6 : https://doc.qt.io/qt-6/qcommandlineparser.html | |||
*: Qt 5 : https://doc.qt.io/qt-5/qcommandlineparser.html | |||
* QCommandLineOptionクラス | |||
*: https://doc.qt.io/qt-6/qcommandlineoption.html | |||
*: https://doc.qt.io/qt-5/qcommandlineoption.html | |||
<br> | |||
また、他の使用方法については、<code>QCommandLineParser</code>クラスのテストケースを参照すること。<br> | |||
* https://code.qt.io/cgit/qt/qtbase.git/tree/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp | |||
<br><br> | <br><br> | ||
| 149行目: | 182行目: | ||
"hoge \" piyo" fuga | "hoge \" piyo" fuga | ||
<br><br> | <br><br> | ||
{{#seo: | |||
|title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki | |||
|keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 | |||
|description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux | |||
|image=/resources/assets/MochiuLogo_Single_Blue.png | |||
}} | |||
__FORCETOC__ | __FORCETOC__ | ||
[[カテゴリ:Qt]] | [[カテゴリ:Qt]] | ||