| name | effect-cli |
| description | Build type-safe CLI applications using Effect CLI module for argument parsing, options, commands, and dependency injection. |
Effect CLI (v4)
Build type-safe command-line applications with typed arguments, flags, subcommands, and dependency injection.
Import Pattern
import { Argument, Command, Flag, Prompt } from 'effect/unstable/cli';
Platform services and runtime for the entry point:
import { NodeRuntime, NodeServices } from '@effect/platform-node';
Positional Arguments (Argument)
Positional arguments are parsed in order. Argument.boolean intentionally does not exist โ use Flag.boolean or Argument.choice("name", ["true", "false"]) instead.
Constructors
import { Argument } from 'effect/unstable/cli';
Argument.string('name');
Argument.integer('count');
Argument.float('ratio');
Argument.date('deadline');
Argument.file('input');
Argument.file('input', { mustExist: true });
Argument.directory('dir');
Argument.directory('dir', { mustExist: true });
Argument.path('target');
Argument.choice('env', ['dev', 'staging', 'prod']);
Argument.choiceWithValue('level', [
['debug', 0],
['info', 1],
['error', 3]
]);
Argument.redacted('secret');
Argument.fileText('config');
Argument.fileParse('config');
Argument.fileSchema('config', MySchema);
Combinators
import { Argument } from 'effect/unstable/cli';
Argument.string('file').pipe(Argument.withDescription('Input file'));
Argument.integer('port').pipe(Argument.withDefault(8080));
Argument.string('config').pipe(Argument.optional);
Argument.string('files').pipe(Argument.variadic);
Argument.string('files').pipe(Argument.variadic({ min: 1 }));
Argument.string('files').pipe(Argument.variadic({ min: 1, max: 5 }));
Argument.variadic(Argument.string('files'));
Argument.variadic(Argument.string('files'), { min: 1 });
Argument.string('files').pipe(Argument.atLeast(1));
Argument.string('files').pipe(Argument.atMost(5));
Argument.string('files').pipe(Argument.between(1, 5));
Argument.integer('port').pipe(Argument.map((p) => `http://localhost:${p}`));
Argument.string('input').pipe(Argument.withSchema(Schema.NonEmptyString));
Argument.string('repo').pipe(
Argument.withFallbackConfig(Config.string('REPOSITORY'))
);
Argument.string('name').pipe(
Argument.withFallbackPrompt(Prompt.text({ message: 'Name' }))
);
Argument.integer('port').pipe(Argument.withMetavar('PORT'));
Argument.integer('count').pipe(
Argument.filter(
(n) => n > 0,
(n) => `Expected positive, got ${n}`
)
);
Named Flags (Flag)
Flags are named options with --name or -alias syntax.
Constructors
import { Flag } from 'effect/unstable/cli';
Flag.boolean('verbose');
Flag.string('config');
Flag.integer('port');
Flag.float('rate');
Flag.date('since');
Flag.file('input');
Flag.file('input', { mustExist: true });
Flag.directory('output');
Flag.path('config-path');
Flag.choice('env', ['dev', 'staging', 'prod']);
Flag.choiceWithValue('log-level', [
['debug', 'Debug' as const],
['info', 'Info' as const],
['error', 'Error' as const]
]);
Flag.redacted('password');
Flag.fileText('config-file');
Flag.fileParse('config');
Flag.fileSchema('config', MySchema);
Flag.keyValuePair('env');
Combinators
import { Flag } from 'effect/unstable/cli';
Flag.boolean('verbose').pipe(Flag.withAlias('v'));
Flag.boolean('experimental-foo').pipe(Flag.withHidden);
Flag.string('config').pipe(Flag.withDescription('Path to config file'));
Flag.integer('port').pipe(Flag.withDefault(3000));
Flag.string('token').pipe(Flag.optional);
Flag.string('db-url').pipe(Flag.withMetavar('URL'));
Flag.string('tag').pipe(Flag.atLeast(1));
Flag.string('warning').pipe(Flag.atMost(3));
Flag.string('host').pipe(Flag.between(1, 3));
Flag.integer('port').pipe(Flag.map((p) => `http://localhost:${p}`));
Flag.string('email').pipe(Flag.withSchema(EmailSchema));
Flag.integer('port').pipe(
Flag.filter(
(p) => p >= 1 && p <= 65535,
(p) => `Port ${p} out of range`
)
);
Flag.boolean('verbose').pipe(
Flag.withFallbackConfig(Config.boolean('VERBOSE'))
);
Flag.string('name').pipe(
Flag.withFallbackPrompt(Prompt.text({ message: 'Name' }))
);
Hidden flags parse normally, but generated help, shell completions, and typo suggestions omit them.
Prompt Defaults
import { Prompt } from 'effect/unstable/cli';
Prompt.integer({ message: 'Count', default: 42 });
Prompt.file({ message: 'Pick file', default: '/workspace/config.json' });
Integer prompt defaults are editable and Enter submits the default if unchanged. Prompt.file resolves/selects the default as the initial path.
Commands
Creating Commands
Command.make accepts a name, optional config object, and optional handler:
import { Console, Effect } from 'effect';
import { Argument, Command, Flag } from 'effect/unstable/cli';
const version = Command.make('version');
const deploy = Command.make('deploy', {
env: Flag.string('env'),
force: Flag.boolean('force'),
files: Argument.string('files').pipe(Argument.variadic)
});
const greet = Command.make(
'greet',
{
name: Argument.string('name').pipe(
Argument.withDescription('Person to greet')
),
times: Flag.integer('times').pipe(Flag.withDefault(1))
},
Effect.fn(function* ({ name, times }) {
for (let i = 0; i < times; i++) {
yield* Console.log(`Hello, ${name}!`);
}
})
);
Handler Pattern
Handlers use Effect.fn with a generator that destructures the config:
const cmd = Command.make(
'deploy',
{
env: Flag.choice('env', ['dev', 'staging', 'prod']),
dryRun: Flag.boolean('dry-run')
},
Effect.fn(function* ({ env, dryRun }) {
if (dryRun) {
yield* Console.log(`Would deploy to ${env}`);
} else {
yield* Console.log(`Deploying to ${env}...`);
}
})
);
Alternatively, add a handler later with Command.withHandler:
const cmd = Command.make('greet', {
name: Flag.string('name')
}).pipe(Command.withHandler(({ name }) => Console.log(`Hello, ${name}!`)));
Command Metadata
Command.make('deploy', config, handler).pipe(
Command.withDescription('Deploy the application'),
Command.withShortDescription('Deploy app'),
Command.withAlias('d'),
Command.withHidden,
Command.withExamples([
{
command: 'myapp deploy --env prod',
description: 'Deploy to production'
},
{ command: 'myapp deploy --env dev --dry-run', description: 'Dry run' }
])
);
Command.withHidden keeps a subcommand invocable by exact name while omitting it from parent help output, shell completions, and "did you mean?" suggestions.
Nested Config
Config objects can be nested for organization:
const deploy = Command.make('deploy', {
environment: Flag.string('env'),
server: {
host: Flag.string('host').pipe(Flag.withDefault('localhost')),
port: Flag.integer('port').pipe(Flag.withDefault(3000))
},
files: Argument.string('files').pipe(Argument.variadic)
});
Subcommands
Basic Subcommands
const app = Command.make('app');
const init = Command.make(
'init',
{},
Effect.fn(function* () {
yield* Console.log('Initializing...');
})
);
const build = Command.make(
'build',
{
target: Flag.choice('target', ['web', 'node'])
},
Effect.fn(function* ({ target }) {
yield* Console.log(`Building for ${target}`);
})
);
app.pipe(
Command.withSubcommands([init, build]),
Command.run({ version: '1.0.0' }),
Effect.provide(NodeServices.layer),
NodeRuntime.runMain
);
Shared Parent Flags
Use Command.withSharedFlags to define flags on a parent that are available to all subcommands. Subcommands access parent config by yielding the parent command:
const tasks = Command.make('tasks').pipe(
Command.withSharedFlags({
workspace: Flag.string('workspace').pipe(
Flag.withAlias('w'),
Flag.withDefault('personal')
),
verbose: Flag.boolean('verbose').pipe(Flag.withAlias('v'))
})
);
const create = Command.make(
'create',
{
title: Argument.string('title'),
priority: Flag.choice('priority', ['low', 'normal', 'high']).pipe(
Flag.withDefault('normal')
)
},
Effect.fn(function* ({ title, priority }) {
const root = yield* tasks;
if (root.verbose) {
yield* Console.log(`workspace=${root.workspace} action=create`);
}
yield* Console.log(
`Created "${title}" in ${root.workspace} with ${priority} priority`
);
})
).pipe(
Command.withDescription('Create a task'),
Command.withExamples([
{
command: 'tasks create "Ship 4.0" --priority high',
description: 'Create a high-priority task'
}
])
);
const list = Command.make(
'list',
{
status: Flag.choice('status', ['open', 'done', 'all']).pipe(
Flag.withDefault('open')
),
json: Flag.boolean('json')
},
Effect.fn(function* ({ status, json }) {
const root = yield* tasks;
if (json) {
yield* Console.log(
JSON.stringify({ workspace: root.workspace, status }, null, 2)
);
} else {
yield* Console.log(`Listing ${status} tasks in ${root.workspace}`);
}
})
).pipe(Command.withDescription('List tasks'), Command.withAlias('ls'));
tasks.pipe(
Command.withSubcommands([create, list]),
Command.run({ version: '1.0.0' }),
Effect.provide(NodeServices.layer),
NodeRuntime.runMain
);
Grouped Subcommands
app.pipe(
Command.withSubcommands([
init,
{ group: 'Development', commands: [build, test] },
{ group: 'Deployment', commands: [deploy, rollback] }
])
);
Dependency Injection
Provide a Layer
const deploy = Command.make(
'deploy',
{
env: Flag.string('env')
},
Effect.fn(function* ({ env }) {
const fs = yield* FileSystem.FileSystem;
})
).pipe(Command.provide(FileSystemLive));
Command.provide((config) =>
config.env === 'local' ? LocalFsLayer : RemoteFsLayer
);
Provide a Service
Command.provideSync(MyService, makeMyService());
Command.provideEffect(MyService, Effect.succeed(makeMyService()));
Command.provideSync(MyService, (config) => makeMyService(config.env));
Running Commands
Command.run is a pipeable combinator that reads args from Stdio. The resulting effect requires FileSystem, Path, Terminal, Stdio, and ChildProcessSpawner; provide platform services and execute with the runtime:
import { NodeRuntime, NodeServices } from '@effect/platform-node';
import { Effect } from 'effect';
import { Command, Flag } from 'effect/unstable/cli';
const myCommand = Command.make(
'myapp',
{
name: Flag.string('name')
},
Effect.fn(function* ({ name }) {
yield* Console.log(`Hello, ${name}!`);
})
);
myCommand.pipe(
Command.run({ version: '1.0.0' }),
Effect.provide(NodeServices.layer),
NodeRuntime.runMain
);
Auto-generates --help and --version flags.
Built-in/global flags (--help, --version, --completions, --log-level, plus custom globals from Command.withGlobalFlags) are parsed for the active command path. A local flag on the selected command can intentionally reuse/override a global flag name or alias. Shared parent flags from Command.withSharedFlags remain command context and may be accepted before or after a subcommand.
Testing with Explicit Args
Use Command.runWith to pass args directly (useful in tests):
const run = Command.runWith(myCommand, { version: '1.0.0' });
Key Patterns
Argument = positional, Flag = named โ No Argument.boolean; use Flag.boolean for toggles
- Handlers use
Effect.fn โ Effect.fn(function*({ ...config }) { ... })
- Parent access via yield โ
const root = yield* parentCommand inside subcommand handlers
- Shared flags โ
Command.withSharedFlags on parent; only flags allowed (no arguments)
- Pipeable
Command.run โ command.pipe(Command.run({version}), Effect.provide(NodeServices.layer), NodeRuntime.runMain)
- Platform services required โ
Command.run requires FileSystem, Path, Terminal, Stdio, and ChildProcessSpawner; provide via NodeServices.layer / BunServices.layer
- All combinators are dual โ Work both as
pipe(Flag.withAlias("v")) and Flag.withAlias(flag, "v")