Skip to content

Mixed machine-readable JSON and human-readable side-channel output.

Mixed machine-readable JSON and human-readable side-channel output.

  • Source: examples/standalone/json-mode.ts

  • Demonstrates: machine-readable out.json() stdout, human-readable stderr side channels, and --json for CLI-managed structured errors.

Usage

bash
npx tsx examples/json-mode.ts list                  # JSON stdout + plain stderr side channel
npx tsx examples/json-mode.ts list --format table   # JSON stdout + table stderr side channel
npx tsx examples/json-mode.ts list --json           # same success output; CLI-managed errors stay JSON-safe
npx tsx examples/json-mode.ts show web-api
npx tsx examples/json-mode.ts show nonexistent      # structured error
npx tsx examples/json-mode.ts show nonexistent --json  # JSON error

Source

ts
/**
 * Mixed machine-readable JSON and human-readable side-channel output.
 *
 * Demonstrates: machine-readable `out.json()` stdout, human-readable stderr
 * side channels, and `--json` for CLI-managed structured errors.
 *
 * Usage:
 *   npx tsx examples/json-mode.ts list                  # JSON stdout + plain stderr side channel
 *   npx tsx examples/json-mode.ts list --format table   # JSON stdout + table stderr side channel
 *   npx tsx examples/json-mode.ts list --json           # same success output; CLI-managed errors stay JSON-safe
 *   npx tsx examples/json-mode.ts show web-api
 *   npx tsx examples/json-mode.ts show nonexistent      # structured error
 *   npx tsx examples/json-mode.ts show nonexistent --json  # JSON error
 */

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

// --- Sample data ---

type  = {
	readonly : string;
	readonly : 'healthy' | 'degraded' | 'down';
	readonly : number;
};

const : readonly [] = [
	{ : 'web-api', : 'healthy', : 99.9 },
	{ : 'worker', : 'degraded', : 95.2 },
	{ : 'database', : 'healthy', : 99.99 },
	{ : 'cache', : 'down', : 0 },
];

// --- Commands ---

const  = ('list')
	.('List all services')
	.(
		'format',
		.(['table', 'plain']).('plain').('f').('Output format'),
	)
	.(({ ,  }) => {
		// This example always emits machine-readable JSON to stdout. `--json`
		// still matters for DreamCLI-managed output such as structured errors.
		.();

		if (. === 'table') {
			.(
				,
				[
					{ : 'name', : 'Service' },
					{ : 'status', : 'Status' },
					{ : 'uptime', : 'Uptime %' },
				],
				{ : 'text', : 'stderr' },
			);
		} else {
			for (const  of ) {
				.(`${.}: ${.} (${.}%)`);
			}
		}
	});

const  = ('show')
	.('Show details for a service')
	.('name', .().('Service name'))
	.(({ ,  }) => {
		const  = .(() => . === .);

		if (!) {
			// CLIError with details serializes cleanly in --json mode.
			throw new (`Service '${.}' not found`, {
				: 'NOT_FOUND',
				: 1,
				: `Available: ${.(() => .).(', ')}`,
				: { : ., : .(() => .) },
			});
		}

		.();
		.(`${.}: ${.} (uptime ${.}%)`);
	});

// --- CLI with --json support ---

void ('services')
	.('1.0.0')
	.('Service status dashboard')
	.()
	.()
	.();

Released under the MIT License.