Skip to content

Multi-command CLI with nested command groups (git-like).

Multi-command CLI with nested command groups (git-like).

Usage

bash
npx tsx examples/multi-command.ts deploy production --force
npx tsx examples/multi-command.ts deploy production --region eu
DEPLOY_REGION=ap npx tsx examples/multi-command.ts deploy production
npx tsx examples/multi-command.ts db migrate --steps 3
npx tsx examples/multi-command.ts db seed
npx tsx examples/multi-command.ts login --token abc123
npx tsx examples/multi-command.ts --help
npx tsx examples/multi-command.ts --version

Source

ts
/**
 * Multi-command CLI with nested command groups (git-like).
 *
 * Demonstrates: top-level commands, nested groups, version metadata, and
 * env-backed flags.
 *
 * Usage:
 *   npx tsx examples/multi-command.ts deploy production --force
 *   npx tsx examples/multi-command.ts deploy production --region eu
 *   DEPLOY_REGION=ap npx tsx examples/multi-command.ts deploy production
 *   npx tsx examples/multi-command.ts db migrate --steps 3
 *   npx tsx examples/multi-command.ts db seed
 *   npx tsx examples/multi-command.ts login --token abc123
 *   npx tsx examples/multi-command.ts --help
 *   npx tsx examples/multi-command.ts --version
 */

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

// --- Top-level commands ---

const  = ('deploy')
	.('Deploy to an environment')
	.('target', .().('Deploy target (e.g. production, staging)'))
	.('force', .().('f').('Skip confirmation'))
	.('region', .(['us', 'eu', 'ap']).('DEPLOY_REGION').('Target region'))
	.(({ , ,  }) => {
		.(`Deploying ${.} to ${. ?? 'default region'}`);
		if (.) .('(forced)');
	});

const  = ('login')
	.('Authenticate with the service')
	.('token', .().('Auth token'))
	.(({ ,  }) => {
		.(`Logged in with token: ${. ?? 'interactive'}`);
	});

// --- Nested command group (myapp db migrate, myapp db seed) ---

const  = ('migrate')
	.('Run database migrations')
	.('steps', .().('s').('Number of migration steps'))
	.(({ ,  }) => {
		.(`Migrating ${. !==  ? `${.} steps` : 'all pending'}`);
	});

const  = ('seed')
	.('Seed the database')
	.('env', .(['dev', 'test']).('dev').('Seed environment'))
	.(({ ,  }) => {
		.(`Seeding ${.} database`);
	});

const  = ('db').('Database operations').().();

// --- Wire it all up ---

void ('myapp')
	.('1.0.0')
	.('Example multi-command CLI')
	.()
	.()
	.()
	.();

Released under the MIT License.