diff --git a/README.md b/README.md index caf5d4a..4f5b6b7 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ The letter at the beginning is the question type: | 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. | - | | 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. | diff --git a/install.sh b/install.sh index 3c6b980..474f010 100755 --- a/install.sh +++ b/install.sh @@ -106,30 +106,43 @@ function populateQuestionsWithModuleRequiredInformation() { 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() { - local arg argName argValue + local argValue local instructions=("${(s.;.)questionArgs}") local questionType="${instructions[1]}" shift instructions if [ "${questionType}" = 'info' ]; then args=(info) + if findQuestionArgInInstruction 'default'; then + test -n "${argValue}" && args=('-d' "${argValue}" ${args}) + fi elif [ "${questionType}" = 'password' ]; then args=('-p' info) elif [ "${questionType}" = 'confirm' ]; then args=(confirm) elif [ "${questionType}" = 'select' ]; then - for arg in "${instructions[@]}"; do - arg=("${(s.:.)arg}") - [ "${#arg}" -lt 2 ] && continue - argName="${arg[1]}" - argValue="${arg[2]}" - [ "${argName}" != 'choose from' ] && continue - choices=("${(s.,.)argValue}") - done - [ "${#choices}" -ge 1 ] || return 10 + findQuestionArgInInstruction 'choose from' || return 10 + choices=("${(s.,.)argValue}") + [ "${#choices}" -ge 1 ] || return 11 args=(choose) fi + return 0 } function askUserQuestion() { diff --git a/spec/convertQuestionArgsToAskUserArgs_spec.sh b/spec/convertQuestionArgsToAskUserArgs_spec.sh index ecf03b7..0f1a816 100644 --- a/spec/convertQuestionArgsToAskUserArgs_spec.sh +++ b/spec/convertQuestionArgsToAskUserArgs_spec.sh @@ -12,6 +12,17 @@ Describe 'convertQuestionArgsToAskUserArgs' The status should be success 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' args=() choices=() questionArgs='password' When call convertQuestionArgsToAskUserArgs diff --git a/spec/findQuestionArgInInstruction_spec.sh b/spec/findQuestionArgInInstruction_spec.sh new file mode 100644 index 0000000..328a08a --- /dev/null +++ b/spec/findQuestionArgInInstruction_spec.sh @@ -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