LogoCyanPrint

Set Default Values

How to set default values for questions

How to Set Default Values

Default values improve user experience by providing sensible starting points.

Shorthand vs Object Form

Shorthand forms don't support defaults. Use the object form for default values:

// Shorthand - no default support
const name = await i.text('Name?', 'project.name', 'Enter name');
// Object form - supports default // !callout
const name = await i.text({
type: QuestionType.Text,
id: 'project.name',
message: 'Name?',
desc: 'Enter project name',
default: 'my-project' // Default value // !mark
});
# Shorthand - no default support
name = await i.text('Name?', 'project.name', 'Enter name')
# Object form - supports default // !callout
name = await i.text(
'Name?',
'project.name',
TextQ(desc='Enter project name', default='my-project') # Default value // !mark
)
// Shorthand - no default support
var name = await i.Text("Name?", "project.name", "Enter name");
// Object form - supports default // !callout
var name = await i.Text("Name?", "project.name", new TextQ
{
Desc = "Enter project name",
Default = "my-project" // Default value // !mark
});

Default by Question Type

Text

The text() question type supports two ways to set initial values:

  • default: A fallback value used when no user input is provided
  • initial: A pre-filled value that appears in the input field (user can edit)
const name = await i.text({
type: QuestionType.Text,
id: 'project.name',
message: 'Project name?',
desc: 'Enter name',
default: 'my-project' // Fallback if user provides no input // !mark
});

Using initial for pre-filled input:

const name = await i.text({
type: QuestionType.Text,
id: 'project.name',
message: 'Project name?',
desc: 'Enter name',
initial: 'my-project' // Pre-filled in input field, user can edit // !mark
});
name = await i.text(
'Project name?',
'project.name',
TextQ(desc='Enter name', default='my-project') # Fallback if user provides no input // !mark
)

Using initial for pre-filled input:

name = await i.text(
'Project name?',
'project.name',
TextQ(desc='Enter name', initial='my-project') # Pre-filled in input field, user can edit // !mark
)
var name = await i.Text("Project name?", "project.name", new TextQ
{
Desc = "Enter name",
Default = "my-project" // Fallback if user provides no input // !mark
});

Using Initial for pre-filled input:

var name = await i.Text("Project name?", "project.name", new TextQ
{
Desc = "Enter name",
Initial = "my-project" // Pre-filled in input field, user can edit // !mark
});

Confirm

const typescript = await i.confirm({
type: QuestionType.Confirm,
id: 'project.typescript',
message: 'Use TypeScript?',
desc: 'Add TypeScript config',
default: true // true or false // !mark
});
typescript = await i.confirm(
'Use TypeScript?',
'project.typescript',
ConfirmQ(desc='Add TypeScript config', default=True) # True or False // !mark
)
var typescript = await i.Confirm("Use TypeScript?", "project.typescript", new ConfirmQ
{
Desc = "Add TypeScript config",
Default = true // true or false // !mark
});

Date

The dateSelect() method accepts a Date object for the default property, but returns the selected date as a string (ISO format):

const startDate = await i.dateSelect({
type: QuestionType.DateSelect,
id: 'project.start',
message: 'Start date?',
desc: 'Project start',
default: new Date() // Date object as input // !mark
});
// startDate is a string, e.g., "2024-01-15" // !callout
from datetime import datetime
start_date = await i.date_select(
'Start date?',
'project.start',
DateSelectQ(desc='Project start', default=datetime.now()) # datetime object as input // !mark
)
# start_date is a string, e.g., "2024-01-15" // !callout
var startDate = await i.DateSelect("Start date?", "project.start", new DateSelectQ
{
Desc = "Project start",
Default = DateTime.Now // DateTime object as input // !mark
});
// startDate is a string, e.g., "2024-01-15" // !callout

select() and checkbox() question types do not support the default property. Users must make an active selection for these question types.

Computed Defaults

Calculate defaults dynamically:

const projectName = await i.text({
type: QuestionType.Text,
id: 'project.name',
message: 'Project name?',
desc: 'Enter name',
default: process.cwd().split('/').pop() || 'my-project' // Current directory name // !mark
});
import os
project_name = await i.text(
'Project name?',
'project.name',
TextQ(
desc='Enter name',
default=os.path.basename(os.getcwd()) or 'my-project' # Current directory name // !mark
)
)
using System.IO;
var projectName = await i.Text("Project name?", "project.name", new TextQ
{
Desc = "Enter name",
Default = Path.GetFileName(Directory.GetCurrentDirectory()) ?? "my-project" // Current directory name // !mark
});