Add default arg to info question type

This commit is contained in:
Rezart Qelibari
2022-02-13 23:14:56 +01:00
parent 302956c7e1
commit da96cc2cf0
4 changed files with 75 additions and 11 deletions

View File

@@ -34,7 +34,7 @@ The letter at the beginning is the question type:
| Question type | Description | Arguments | | Question type | Description | Arguments |
| ------------- | ----------- | --------- | | ------------- | ----------- | --------- |
| i (info) | A question where the user has no restrictions on input | - | | i (info) | A question where the user has no restrictions on input | `default`: a default answer. |
| p (password) | A question where the user input is not printed to standard output. | - | | p (password) | A question where the user input is not printed to standard output. | - |
| c (confirm) | A yes/no question where the user is allowed to answer yes or no. | - | | c (confirm) | A yes/no question where the user is allowed to answer yes or no. | - |
| s (select) | A list of choices where the user can select one using numbers. | `choose from`: a comma separated list of possible select values. | | s (select) | A list of choices where the user can select one using numbers. | `choose from`: a comma separated list of possible select values. |

View File

@@ -106,30 +106,43 @@ function populateQuestionsWithModuleRequiredInformation() {
log debug "Parsed questions are: ${(kv)questions}" log debug "Parsed questions are: ${(kv)questions}"
} }
function findQuestionArgInInstruction() {
local argNameToLookup="$1" arg name value
[ -z "${argNameToLookup}" ] && return
for arg in ${instructions[@]}; do
arg=("${(s.:.)arg}")
[ "${#arg}" -lt 2 ] && continue
name="${arg[1]}"
value="${arg[2]}"
[ "${name}" != "${argNameToLookup}" ] && continue
argValue="${value}"
return
done
return 10
}
function convertQuestionArgsToAskUserArgs() { function convertQuestionArgsToAskUserArgs() {
local arg argName argValue local argValue
local instructions=("${(s.;.)questionArgs}") local instructions=("${(s.;.)questionArgs}")
local questionType="${instructions[1]}" local questionType="${instructions[1]}"
shift instructions shift instructions
if [ "${questionType}" = 'info' ]; then if [ "${questionType}" = 'info' ]; then
args=(info) args=(info)
if findQuestionArgInInstruction 'default'; then
test -n "${argValue}" && args=('-d' "${argValue}" ${args})
fi
elif [ "${questionType}" = 'password' ]; then elif [ "${questionType}" = 'password' ]; then
args=('-p' info) args=('-p' info)
elif [ "${questionType}" = 'confirm' ]; then elif [ "${questionType}" = 'confirm' ]; then
args=(confirm) args=(confirm)
elif [ "${questionType}" = 'select' ]; then elif [ "${questionType}" = 'select' ]; then
for arg in "${instructions[@]}"; do findQuestionArgInInstruction 'choose from' || return 10
arg=("${(s.:.)arg}") choices=("${(s.,.)argValue}")
[ "${#arg}" -lt 2 ] && continue [ "${#choices}" -ge 1 ] || return 11
argName="${arg[1]}"
argValue="${arg[2]}"
[ "${argName}" != 'choose from' ] && continue
choices=("${(s.,.)argValue}")
done
[ "${#choices}" -ge 1 ] || return 10
args=(choose) args=(choose)
fi fi
return 0
} }
function askUserQuestion() { function askUserQuestion() {

View File

@@ -12,6 +12,17 @@ Describe 'convertQuestionArgsToAskUserArgs'
The status should be success The status should be success
End End
It 'converts info with default arg to info with default arg'
args=()
choices=()
questionArgs='info;default:Mario Kart'
When call convertQuestionArgsToAskUserArgs
The output should eq ''
The variable args should eq '-d Mario Kart info'
The variable choices should eq ''
The status should be success
End
It 'converts password to -p info' It 'converts password to -p info'
args=() choices=() questionArgs='password' args=() choices=() questionArgs='password'
When call convertQuestionArgsToAskUserArgs When call convertQuestionArgsToAskUserArgs

View File

@@ -0,0 +1,40 @@
Describe 'findQuestionArgInInstruction'
Include ./install.sh
instructions=('somearg:somevalue' 'default:one' 'choose from:blue,light green,red')
It 'finds nothing if no arg name given'
argValue=''
When call findQuestionArgInInstruction
The variable argValue should eq ''
The status should be success
End
It 'finds nothing if arg name does not exist'
argValue=''
When call findQuestionArgInInstruction 'arg name that does not exist'
The variable argValue should eq ''
The status should be failure
End
It 'finds nothing if instructions is empty'
instructions=()
argValue=''
When call findQuestionArgInInstruction 'some arg name'
The variable argValue should eq ''
The status should be failure
End
It 'finds arg value if instructions contains arg as first item'
argValue=''
When call findQuestionArgInInstruction 'somearg'
The variable argValue should eq 'somevalue'
The status should be success
End
It 'finds arg value if instructions contains arg among other items'
argValue=''
When call findQuestionArgInInstruction 'choose from'
The variable argValue should eq 'blue,light green,red'
The status should be success
End
End