phabalicious icon indicating copy to clipboard operation
phabalicious copied to clipboard

How to work with multiselect choices question?

Open gi-dorio opened this issue 3 years ago • 3 comments

I am trying to scaffold a new app and I have a question for the user to select a bunch of components from a multiple choices question. Now, I've noticed that the answers to the questions can be used in the scaffold section as %yourkey.0%, %yourkey.1% etc based on how many choices you selected. My goal is to add the component only if it's selected, so my question is: is there any kind of IF statement for the scaffold or some other way to execute the command only if %yourkey.1% exist? Because right now referring to, for example, %yourkey.1% throws an error and blocks the process if it doesn't exist. Practical example. This is the question I ask:

yourkey:
  question: What fruits do you want to buy
  type: choices
  multiselect: true
  choices:
    - Apples
    - Oranges
    - Lemons
    - Papayas
    - Coconuts

I want the scaffolding section to create a directory for each of the selected options. So it would look something like this

scaffold:
  - mkdir %yourkey.0%
  - mkdir %yourkey.1%

How can I do this without knowing how many choices the user will make? Is there an IF statement or a way to check if, for example, %yourkey.1% exist without breaking the scaffold execution?

I'm sorry if it's a dumb question, but the docs are not clear at all on the use of values from questions

gi-dorio avatar Dec 29 '21 08:12 gi-dorio

Hi @GiuseppeCSI

Multiselects are more suited when scaffolding actual files, there you have the full power of twig to iterate over arrays. Arrays are currently not supported for the scaffold-section.

You might try this workaround: Scaffold the actual script in a first step and then execute the script, e.g.:

The file setup.sh

#!/bin/bash

{% for folder in yourkey %}
mkdir {{folder}}
{% endfor %}

the actual scaffold-file:

questions:
  yourkey:
    question: What fruits do you want to buy
    type: choices
    multiselect: true
    choices:
      - Apples
      - Oranges
      - Lemons
      - Papayas
      - Coconuts
assets:
  - setup.sh

scaffold:
  - copy_assets(%rootFolder%)
  - bash %rootFolder%/setup.sh
  - rm %rootFolder%/setup.sh

(This is from the top of my head, and untested, so YMMV)

stmh avatar Dec 30 '21 11:12 stmh

Hi, thanks a lot for the reply.

I've tried to work around the problem by using confirm questions and it works, but i tested your solution and it's definitely a lot more straightforward and avoids the redundancy of answering five yes/no questions. Since the workaround is so easy it's just a minor inconvenience, but is there any plan of supporting arrays in the scaffold section?

Also, speaking of questions, the documentation of the confirm question says it gives values 0 and 1 to the related key, but when I answer no it puts an empty space instead of a 0. Is this intended or a bug? Should I open a separate issue for this?

Thank you again

gi-dorio avatar Dec 30 '21 11:12 gi-dorio

but is there any plan of supporting arrays in the scaffold section?

We can keep this issue as a reminder. I need to think of a way to transform the array into sth we can reuse in bash (as that is the underlying tool which executes the script)

Also, speaking of questions, the documentation of the confirm question says it gives values 0 and 1 to the related key, but when I answer no it puts an empty space instead of a 0. Is this intended or a bug? Should I open a separate issue for this?

Yes please.

stmh avatar Jan 05 '22 09:01 stmh