Commit 135a4b44 authored by Volker Hilsheimer's avatar Volker Hilsheimer

Allow parameter values that start with --

A command line like

--configure "--setting value"

should not interpret --configure as a flag. Only recognizes values with
spaces, which is the most relevant usecase.
parent 390ac7da
......@@ -3,14 +3,14 @@ setlocal
setlocal enabledelayedexpansion
set PARSE_OPTS_FLAGS=flag4
set args=pos1 pos2 --param1 value1 --param2 value2 pos3 --flag1 --param3 value3 --array "a 1" --array "a 2" --flag2 --array a3 "pos 4" --flag3 --flag4 pos5 -- pass "pass through" --pass
set args=pos1 pos2 --param1 value1 --param2 value2 pos3 --flag1 --param3 value3 --array "a 1" --array "a 2" --flag2 --array a3 "pos 4" --flag3 --flag4 pos5 --param4 "--value4 123 --value5" -- pass "pass through" --pass
set /A errors=0
set debug=false
if "%1" == "--debug" (
set debug=true
shift
set args=%*
set "args=%*"
)
call ..\util\parse-opts.cmd %args%
......@@ -42,12 +42,13 @@ call :assert "!FLAG_flag3!" true
call :assert "!FLAG_flag4!" true
call :assert "!FLAG_flag5!" ""
call :assert "!PARAMS[@]!" "param1 param2 param3 array"
call :assert "!PARAMS[#]!" 4
call :assert "!PARAMS[@]!" "param1 param2 param3 array param4"
call :assert "!PARAMS[#]!" 5
call :assert "!PARAM_param1!" "value1"
call :assert "!PARAM_param2!" "value2"
call :assert "!PARAM_param3!" "value3"
call :assert "!PARAM_array!" "a 1"
call :assert "!PARAM_param4!" "--value4 123 --value5"
call :assert "!PARAM_array[@]!" "a 1 a 2 a3"
call :assert "!PARAM_array[0]!" "a 1"
......
......@@ -17,6 +17,7 @@ args=( pos1
--flag3
--flag4
pos5
--param4 "--value4 123 --value5"
-- pass "pass through" --pass
)
declare -i errors=0
......@@ -72,12 +73,13 @@ assert "$FLAG_flag3" "true"
assert "$FLAG_flag4" "true"
assert "$FLAG_flag5" ""
assert "${PARAMS[*]}" "param1 param2 param3 array"
assert "${#PARAMS[@]}" 4
assert "${PARAMS[*]}" "param1 param2 param3 array param4"
assert "${#PARAMS[@]}" 5
assert $PARAM_param1 "value1"
assert $PARAM_param2 "value2"
assert $PARAM_param3 "value3"
assert "$PARAM_array" "a 1"
assert "$PARAM_param4" "--value4 123 --value5"
assert "${PARAM_array[*]}" "a 1 a 2 a3"
assert "${#PARAM_array[@]}" 3
......
......@@ -18,11 +18,6 @@ set PASSTHROUGH[@]=
set "arg=%~1"
set "short=false"
if "!arg:~0,1!" == "-" if "!arg:~2!" == "" (
set "short=true"
)
if "!arg!" == "--" (
set "PASSTHROUGH[@]=%~2"
set "PASSTHROUGH[#]=1"
......@@ -39,7 +34,17 @@ set PASSTHROUGH[@]=
shift
goto :parseargs
)
if "!arg:~0,2!" == "--" (
set IS_NAME=
if "!arg!" == "!arg: =!" (
if "!arg:~0,2!" == "--" (
set IS_NAME=true
) else (
if "!arg:~0,1!" == "-" if "!arg:~2!" == "" (
set IS_NAME=true
)
)
)
if DEFINED IS_NAME (
if !argCount! LSS !nameCount! (
set args[!argCount!]=""
set /A argCount+=1
......@@ -47,20 +52,11 @@ set PASSTHROUGH[@]=
set names[!nameCount!]=!arg:~2!
set /A nameCount+=1
) else (
if "!short!" == "true" (
if !argCount! LSS !nameCount! (
set args[!argCount!]=""
set /A argCount+=1
)
set names[!nameCount!]=!arg:~1!
set /A nameCount+=1
if !nameCount! EQU !argCount! (
call :add_positional !arg!
) else (
if !nameCount! EQU !argCount! (
call :add_positional !arg!
) else (
set "args[!argCount!]=!arg!"
set /A argCount+=1
)
set "args[!argCount!]=!arg!"
set /A argCount+=1
)
)
shift
......@@ -86,6 +82,7 @@ for /L %%i in (0,1,%nameCount%) do (
if defined FORCEFLAG (
call :add_flag !name!
call :add_positional !args[%%i]!
set FORCEFLAG=
) else (
set "name=!name:-=_!"
set "pname=PARAM_!name!"
......@@ -127,7 +124,6 @@ set args=
set argCount=
set posCount=
set flagCount=
set short=
REM Interpret P0 and P1, set JOBDIR
set "_JOBDIR=!POSITIONAL[1]!"
......
......@@ -14,17 +14,21 @@ for arg in "${@}"; do
if [[ "$arg" == "--" ]]; then
PASSTHROUGH=( "${@:$(( index + 1 ))}" )
break
elif [[ "$arg" =~ ^--.*$ ]]; then
if [[ ${#args[@]} < ${#names[@]} ]]; then
args+=('""')
fi
is_name=""
if [[ ! "$arg" =~ " " ]]; then
if [[ "$arg" =~ ^--.*$ ]]; then
name="${arg/--/}"
is_name="true"
elif [[ "$arg" =~ ^-.$ ]]; then
name="${arg/-/}"
is_name="true"
fi
name="${arg/--/}"
names+=("$name")
elif [[ "$arg" =~ ^-.$ ]]; then
fi
if [[ ! -z $is_name ]]; then
if [[ ${#args[@]} < ${#names[@]} ]]; then
args+=('""')
fi
name=${arg/-/}
names+=("$name")
else
if [[ ${#names[@]} == ${#args[@]} ]]; then
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment