Skip to content

Interactive prompts with config file fallback.

Interactive prompts with config file fallback.

  • Source: examples/standalone/interactive.ts

  • Demonstrates: per-flag .prompt(), .env(), .config(), and the full resolution chain (CLI → env → config → prompt → default).

Usage

bash
npx tsx examples/interactive.ts                    # prompts for everything
npx tsx examples/interactive.ts --region eu        # skips region prompt
DEPLOY_REGION=ap npx tsx examples/interactive.ts   # env resolves region
echo '{}' | npx tsx examples/interactive.ts        # non-interactive: uses defaults / errors

Source

ts
/**
 * Interactive prompts with config file fallback.
 *
 * Demonstrates: per-flag `.prompt()`, `.env()`, `.config()`, and the full
 * resolution chain (CLI → env → config → prompt → default).
 *
 * The resolution chain means:
 *   1. Explicit CLI flag wins:      --region eu
 *   2. Then environment variable:   DEPLOY_REGION=eu
 *   3. Then config file value:      { "deploy": { "region": "eu" } }
 *   4. Then interactive prompt:     prompts user to select
 *   5. Then default value:          "us"
 *
 * Usage:
 *   npx tsx examples/interactive.ts                    # prompts for everything
 *   npx tsx examples/interactive.ts --region eu        # skips region prompt
 *   DEPLOY_REGION=ap npx tsx examples/interactive.ts   # env resolves region
 *   echo '{}' | npx tsx examples/interactive.ts        # non-interactive: uses defaults / errors
 */

import { , , ,  } from '@kjanat/dreamcli';

const  = ('deploy')
	.('Deploy to an environment')
	.('target', .().('Deploy target').('staging'))
	.(
		'region',
		
			.(['us', 'eu', 'ap'])
			.('DEPLOY_REGION')
			.('deploy.region')
			.({ : 'select', : 'Which region?' })
			.('us')
			.('Target region'),
	)
	.(
		'confirm',
		
			.()
			.({ : 'confirm', : 'Proceed with deployment?' })
			.('Confirm deployment'),
	)
	.(
		'tag',
		
			.()
			.({ : 'input', : 'Release tag (e.g. v1.2.3):' })
			.('Release tag'),
	)
	.(({ , ,  }) => {
		// All flags are resolved by the time action runs.
		// flags.region: "us" | "eu" | "ap" — guaranteed by prompt/default
		// flags.confirm: boolean
		// flags.tag: string | undefined — only if user provided one
		if (!.) {
			.('Deployment cancelled');
			return;
		}
		.(`Deploying ${.} to ${.}`);
		if (. !== ) {
			.(`Tagged: ${.}`);
		}
	});

void ('deploy').().();

Released under the MIT License.