MochiuWiki : SUSE, EC, PCB
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
検索
個人用ツール
ログイン
Toggle dark mode
名前空間
ページ
議論
表示
閲覧
ソースを閲覧
履歴を表示
Qtの設定 - コマンドライン引数のソースを表示
提供: MochiuWiki : SUSE, EC, PCB
←
Qtの設定 - コマンドライン引数
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループのいずれかに属する利用者のみが実行できます:
管理者
、new-group。
このページのソースの閲覧やコピーができます。
== 概要 == Qt 5.2以降では、<code>[https://doc.qt.io/qt-5/qcommandlineparser.html QCommandLineParser]</code>クラスと<code>[https://doc.qt.io/qt-5/qcommandlineoption.html QCommandLineOption]</code>クラスが追加され、コマンドライン引数の扱いが簡潔になっている。<br> また、<code>QCoreApplication</code>クラスの<code>arguments</code>メソッドを使用することでも、コマンドライン引数を扱うことができる。<br> <br> ここでは、これらのクラスの使用方法について記載する。<br> 併せて、デバッグ時のみ使用するコマンドライン引数の設定方法も記載する。<br> <br><br> == QCommandLineParserクラスの使用 == 以下の例では、<code>QCommandLineParser</code>クラスを継承したサブクラスを定義して、指定されたオプションを処理している。<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> <syntaxhighlight lang="c++"> // CustomCommandLineParser.hファイル #include <QCoreApplication> #include <QCommandLineParser> #include <QRegularExpression> #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[]) { QCoreApplication a(argc, argv); QCoreApplication::setApplicationName("<任意のアプリケーション名>"); QCoreApplication::setApplicationVersion("1.0"); CustomCommandLineParser parser; parser.setApplicationDescription("Command line options example"); QCommandLineOption versionOption(QStringList() << "v" << "version", "Displays version information."); parser.addOption(versionOption); QCommandLineOption helpOption(QStringList() << "h" << "help", "Displays help information."); parser.addOption(helpOption); QCommandLineOption configOption(QStringList() << "c" << "config", "Display the configuration string.", "config"); parser.addOption(configOption); parser.process(QCoreApplication::arguments()); if (parser.isSet(helpOption)) { parser.showHelp(); return 0; } if (parser.isSet(versionOption)) { qDebug() << QCoreApplication::applicationName() << QCoreApplication::applicationVersion(); return 0; } if (parser.isSet(configOption)) { QString configString = parser.value(configOption); qDebug() << "Configuration: " << configString; return 0; } if (parser.positionalArguments().isEmpty() && !parser.optionNames().isEmpty()) { qDebug() << "有効なオプションはありません。使用法については、-h または --help を使用してください。"; } return 0; } </syntaxhighlight> <br> # 実行 <実行ファイル名> -h または <実行ファイル名> --help ヘルプ情報を表示する <実行ファイル名> -v または <実行ファイル名> --version バージョン情報を表示する <実行ファイル名> -c "<任意の文字列>" または <実行ファイル名> --config "<任意の文字列>" 指定された設定文字列を表示する <実行ファイル名> -x "不明なオプション: x" と表示する <br> <u>※注意</u><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> == QCoreApplicationクラスを使用する方法 == 上記とは別の方法として、<code>QCoreApplication::arguments()</code>を使用することもできる。<br> <code>QCoreApplication::arguments()</code>の型は<code>QStringList</code>であるため、簡単に取得および加工することができる。<br> <br> 以下の3つの例では、全てのコマンドライン引数を<code>QStringList</code>クラスで取得して表示している。<br> コマンドライン引数の1つ目の要素を削除している理由は、1つ目の引数にはソフトウェアのパスが渡されるからである。<br> <syntaxhighlight lang="c++"> // 方法 1 QStringList argv = QCoreApplication::arguments(); argv.removeAt(0); for(QString arg : argv) { qDebug() << "argument = " << arg; } </syntaxhighlight> <br> <syntaxhighlight lang="c++"> // 方法 2 QStringList argv = QCoreApplication::arguments(); for(int i = 1; i < arguments.count(); i++) { qDebug() << "argument = " << arguments.at(i); } </syntaxhighlight> <br> <syntaxhighlight lang="c++"> // 方法 3 QStringList argv = QCoreApplication::arguments(); argv.removeAt(0); foreach(QString const &arg, arguments) { qDebug() << arg; } </syntaxhighlight> <br><br> == デバッグ時におけるコマンドライン引数の設定 == # Qt Creatorを起動して、任意のQtプロジェクトを開く。 # Qt Creatorのメイン画面左にある[プロジェクト]アイコン - [Build & Run]セクション - [Run]項目を選択して、[実行時の設定]画面を表示する。 # [実行時の設定]画面から、[実行]セクション - [コマンドライン引数:]項目に、コマンドライン引数を記述する。 <br> コマンドライン引数の記述方法は、以下の通りである。<br> # 各引数は半角スペースで区切る hoge piyo fuga # 引数の内容に空白が入っている場合は、ダブルクォーテーション""で囲む "hoge piyo" fuga # 引数の内容にダブルクォーテーション""が入っている場合は、直前にエスケープシーケンスを記述する "hoge \" piyo" fuga <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__ [[カテゴリ:Qt]]
Qtの設定 - コマンドライン引数
に戻る。
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
Collapse