Closed issenn closed 1 year ago
Can you try this?
{{- range $_, $i := until 99 -}}
{{- $answer := "" -}}
{{- $answer = promptStringOnce . (printf "%s_%i" "answer" $i) "answer" -}}
{{- if regexMatch "^[A-Z][-' a-zA-Z]+$" $answer -}}
{{- writeToStdout (printf "✅ Answer set as '%s'\n" $answer) -}}
{{- break -}}
{{- end -}}
{{- writeToStdout (printf "❌ '%s' is invalid\n" $answer) -}}
{{- if eq $i 98 -}}
{{- writeToStdout "❌ ERROR: maximum tries exceeded\n" -}}
{{- exit 1 -}}
{{- end -}}
{{- end -}}
That said, prompt*Once
seem to me to be better used when you want to prompt for a value exactly once. They pull from the current chezmoi.data
to provide the default answer.
Can you try this?
{{- range $_, $i := until 99 -}} {{- $answer := "" -}} {{- $answer = promptStringOnce . (printf "%s_%i" "answer" $i) "answer" -}} {{- if regexMatch "^[A-Z][-' a-zA-Z]+$" $answer -}} {{- writeToStdout (printf "✅ Answer set as '%s'\n" $answer) -}} {{- break -}} {{- end -}} {{- writeToStdout (printf "❌ '%s' is invalid\n" $answer) -}} {{- if eq $i 98 -}} {{- writeToStdout "❌ ERROR: maximum tries exceeded\n" -}} {{- exit 1 -}} {{- end -}} {{- end -}}
That said,
prompt*Once
seem to me to be better used when you want to prompt for a value exactly once. They pull from the currentchezmoi.data
to provide the default answer.
I tried but still got error.
I'm getting the value as you said, but checking the value, repeating the check in an interactive mode until it meets expectations. Prompt only once if the value is correct.
chezmoi's template language is for templating. It's not a general purpose scripting language and attempts to use it for scripting are unlikely to be successful.
Instead, I would suggest writing a script with the behavior you want and using the output
template function to call that script and collect its output.
Ok, I'll try it.
What exactly are you trying to do?
Do loop in template with
prompt*Once
, but will got error like this:What have you tried so far?
This is bad.
If change
promptStringOnce
topromptString
, that is ok.Where else have you checked for solutions?
Output of any commands you've tried with
--verbose
flagOutput of
chezmoi doctor
Additional context
Add any other context about the problem here.