0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[備忘録]バッチファイルに特定の引数を指定する

0
Posted at

背景

業務で使用するコンソールアプリやデスクトップアプリが便利なのですが、バラバラに管理されておりかつバラバラに配置されているので、それを解決したいと思いました。そこで、下記のようなCLIを使ってそれらを管理しようと考えました。

その際、test 1.0.0 [value1] [value]のように引数を渡せることは知っていましたが、どうせなら下記のようなdotnet [option]コマンドのようにoptionを渡したいと思いました。

dotnet -h

optionを引数から取得する方法

@echo off
set VERSION_CMD=--version
set AILIAS_VERSION_CMD=-v
set VERSION=""

rem enabledelyaedexpansionでコードを呼び出し時ではなく、実際に実行するときに変数を評価する
setlocal enabledelayedexpansion & (
	set NEXT_ARG=""
)
for %%a in (%*) do (
	if !NEXT_ARG! == "true" (
		set VERSION=%%a
		set NEXT_ARG="" 
	)
	if %VERSION_CMD% == %%a (
		set NEXT_ARG="true"
	)
	if %AILIAS_VERSION_CMD% == %%a (
		set NEXT_ARG="true"
	)
)
endlocal & (
	set VERSION=%VERSION%
)
echo %VERSION%
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?