LogoCyanPrint

Ask Checkbox Questions

How to ask multi-select checkbox questions

How to Ask Checkbox Questions

Checkbox questions allow users to select multiple options. Use them for feature selection, configuration options, or any multi-choice scenario.

Shorthand Form

template.ts
const features = await i.checkbox(
'Select features',
Question message shown to user
['ESLint', 'Prettier', 'Jest', 'Docker'],
Available options
'project.features',
Unique key for answer reuse
'Choose features to include'
Helper description
);
// Returns: string[] - e.g., ['ESLint', 'Jest']
template.py
features = await i.checkbox(
'Select features',
Question message shown to user
['ESLint', 'Prettier', 'Jest', 'Docker'],
Available options
'project.features',
Unique key for answer reuse
'Choose features to include'
Helper description
)
# Returns: list[str] - e.g., ['ESLint', 'Jest']
Template.cs
var features = await i.Checkbox(
"Select features",
Question message shown to user
new[] { "ESLint", "Prettier", "Jest", "Docker" },
Available options
"project.features",
Unique key for answer reuse
"Choose features to include"
Helper description
);
// Returns: string[] - e.g., ["ESLint", "Jest"]

Object Form (with validation and defaults)

template.ts
const features = await i.checkbox({
type: QuestionType.Checkbox,
id: 'project.features',
message: 'Select features',
desc: 'Choose features to include',
options: ['ESLint', 'Prettier', 'Jest', 'Docker'],
default: ['ESLint'],
validate: (selected) => {
if (selected.length === 0) return 'Select at least one feature';
if (selected.length > 3) return 'Maximum 3 features allowed';
return null;
}
});
template.py
from cyanprintsdk import QuestionType
features = await i.checkbox(
type=QuestionType.CHECKBOX,
id='project.features',
message='Select features',
desc='Choose features to include',
options=['ESLint', 'Prettier', 'Jest', 'Docker'],
default=['ESLint'],
validate=lambda selected: (
'Select at least one feature' if len(selected) == 0
else 'Maximum 3 features allowed' if len(selected) > 3
else None
)
)
Template.cs
var features = await i.Checkbox(new CheckboxQuestion
{
Type = QuestionType.Checkbox,
Id = "project.features",
Message = "Select features",
Desc = "Choose features to include",
Options = new[] { "ESLint", "Prettier", "Jest", "Docker" },
Default = new[] { "ESLint" },
Validate = selected =>
{
if (selected.Length == 0) return "Select at least one feature";
if (selected.Length > 3) return "Maximum 3 features allowed";
return null;
}
});

Using Results

Conditional Files Based on Selection

processor.ts
const features = await i.checkbox(
'Select features',
['ESLint', 'Prettier', 'Jest'],
'project.features',
'Choose features'
);
const files = [
{ root: 'templates/base', glob: '**/*', exclude: [], type: GlobType.Template }
];
if (features.includes('ESLint')) {
files.push({ root: 'templates/eslint', glob: '**/*', exclude: [], type: GlobType.Copy });
}
if (features.includes('Jest')) {
files.push({ root: 'templates/jest', glob: '**/*', exclude: [], type: GlobType.Copy });
}
processor.py
features = await i.checkbox(
'Select features',
['ESLint', 'Prettier', 'Jest'],
'project.features',
'Choose features'
)
files = [
{'root': 'templates/base', 'glob': '**/*', 'exclude': [], 'type': GlobType.Template}
]
if 'ESLint' in features:
files.append({'root': 'templates/eslint', 'glob': '**/*', 'exclude': [], 'type': GlobType.Copy})
if 'Jest' in features:
files.append({'root': 'templates/jest', 'glob': '**/*', 'exclude': [], 'type': GlobType.Copy})
Processor.cs
var features = await i.Checkbox(
"Select features",
new[] { "ESLint", "Prettier", "Jest" },
"project.features",
"Choose features"
);
var files = new List<CyanGlob>
{
new() { Root = "templates/base", Glob = "**/*", Exclude = Array.Empty<string>(), Type = GlobType.Template }
};
if (features.Contains("ESLint"))
{
files.Add(new() { Root = "templates/eslint", Glob = "**/*", Exclude = Array.Empty<string>(), Type = GlobType.Copy });
}
if (features.Contains("Jest"))
{
files.Add(new() { Root = "templates/jest", Glob = "**/*", Exclude = Array.Empty<string>(), Type = GlobType.Copy });
}

Template Variables

config: {
vars: {
features: features.join(', '), // "ESLint, Jest"
hasEslint: features.includes('ESLint') ? 'true' : 'false',
hasJest: features.includes('Jest') ? 'true' : 'false'
}
}
'config': {
'vars': {
'features': ', '.join(features),
'has_eslint': 'true' if 'ESLint' in features else 'false',
'has_jest': 'true' if 'Jest' in features else 'false'
}
}
Config = new {
vars = new {
features = string.Join(", ", features),
hasEslint = features.Contains("ESLint") ? "true" : "false",
hasJest = features.Contains("Jest") ? "true" : "false"
}
}

The checkbox method returns an array of strings containing the selected options.