Guide
Build a greeting command
Read a name and an optional greeting from the command line.
On this page
Before you start
- Bun and a directory for a small TypeScript project.
Build a greeting command that reads a name and prints a message. Add a --greeting option so the person running it can choose what it says.
In an empty project directory, initialize a package:
bun init -ynpm init -ypnpm inityarn init -yInstall the parser and Zod in your project. Use Zod 3 for this release:
bun add @hulla/args@0.0.4 zod@3npm install @hulla/args@0.0.4 zod@3pnpm add @hulla/args@0.0.4 zod@3yarn add @hulla/args@0.0.4 zod@3Save this as src/cli.ts. Bun’s argv includes the executable and script path, so argv.slice(2) leaves only the words supplied by the user. This parser opts into case-sensitive matching, so option names must match their declared casing.
import { option, parser, positional } from '@hulla/args'
import { argv } from 'node:process'
const cli = parser({
name: 'greet',
arguments: [positional({ name: 'name' }), option({ name: 'greeting' })],
settings: { caseSensitive: true },
})
const result = cli.parse(argv.slice(2))
const name = result.arguments.name.value ?? 'World'
const greeting = result.arguments.greeting.value ?? 'Hello'
console.log(`${greeting}, ${name}!`)Run it with Bun, which executes TypeScript files directly:
$ bun src/cli.ts Alice --greeting Hi
Hi, Alice!With no arguments, the explicit fallbacks apply:
$ bun src/cli.ts
Hello, World!Try bun src/cli.ts Alice --greeting=Hi. It should still print Hi, Alice!. The parser accepts both a space and = between an option and its value.
To change the default message, edit the ?? 'Hello' fallback and run the command without --greeting.
Continue with Build a notes CLI for commands, persistence, validation, and exit codes. The argument guide explains the value, raw, index, and detected fields.