FindCmdLineSwitch関数でアプリケーションのコマンドライン引数を解析する

FindCmdLineSwitch関数は、文字列がアプリケーションのコマンドライン引数として渡されたかどうかを判定します。

bool FindCmdLineSwitch(const System::UnicodeString Switch);
bool FindCmdLineSwitch(const System::UnicodeString Switch, bool IgnoreCase);
bool FindCmdLineSwitch(const System::UnicodeString Switch, const TSysCharSet &Chars, bool IgnoreCase);
bool FindCmdLineSwitch(const System::UnicodeString Switch, System::UnicodeString &Value, bool IgnoreCase = true, const TCmdLineSwitchTypes SwitchTypes = (TCmdLineSwitchTypes() << TCmdLineSwitchType::clstValueNextParam << TCmdLineSwitchType::clstValueAppended ));

コマンドラインの解析が簡単にできます。

最初の形式は大文字小文字を区別しません。
スイッチとそれ以外のパラメータを区別する文字は’/’と’-‘です。

例:Project1.exe /foo -bar

if (FindCmdLineSwitch(L"foo")) std::puts("foo"); //=>foo
if (FindCmdLineSwitch(L"BAR")) std::puts("BAR");  //=>BAR
if (FindCmdLineSwitch(L"hoge")) std::puts("hoge");  //=>

2番目の形式は、大文字小文字を区別するかどうかを指定できます。

例:Project1.exe /foo -bar

//大文字小文字を区別しない
if (FindCmdLineSwitch(L"FOO", true)) std::puts("foo1");  //=>foo1
//大文字小文字を区別する
if (FindCmdLineSwitch(L"FOO", false)) std::puts("foo2");  //=>

3番目の形式では、スイッチとそれ以外のパラメータを区別する文字を指定できます。

例:Project1.exe /foo -bar

//スイッチは'-'ではじまる
TSysCharSet chars = TSysCharSet() << '-';
if (FindCmdLineSwitch(L"foo", chars, true)) std::puts("/foo");
if (FindCmdLineSwitch(L"bar", chars, true)) std::puts("-bar"); //=>-bar

最後の形式では、引数の残りの文字を取得できます。

例:Project1.exe /foo:123 -bar456

UnicodeString value;
if (FindCmdLineSwitch(L"foo", value))
  std::wcout << value.c_str() << std::endl; //=>123
if (FindCmdLineSwitch(L"bar", value))
  std::wcout << value.c_str() << std::endl; //=>456

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください