Tutorial May 29, 2026 12 min read

Building a Type-Safe CLI with @fotros/cli and @fotros/schema

A step-by-step guide to building a production-grade Node.js CLI that validates its config file at startup using Fotros primitives.

Modern CLIs do a lot more than just run commands. They read config files, validate user input, and provide rich error messages. This tutorial builds a full CLI from scratch using @fotros/cli for the command layer and @fotros/schema for config validation.

Project Setup

bash
1
mkdir my-cli && cd my-cli
2
npm init -y
3
npm install @fotros/cli @fotros/schema
4
npm install -D typescript @types/node

Defining the Config Schema

Before writing any command logic, define the shape of your config file. This schema becomes your single source of truth for both validation and TypeScript types.

typescript
1
import { createParser } from '@fotros/schema';
 
3
export const ConfigSchema = createParser({
4
  apiUrl: String,
5
  token: String,
6
  timeout: Number.optional().default(5000),
7
  debug: Boolean.optional().default(false),
8
});
 
10
export type Config = typeof ConfigSchema._output;
Tags
TypeScriptCLITutorialNode.js
// RELATED POSTS

You might also like