ArgFactory
Arg factory functions — the public API for creating positional arguments.
Each method returns an ArgBuilder seeded with the correct ArgKind and initial type-level config. Chain modifiers (.optional(), .env(), .config(), .prompt(), .default(), .variadic(), .stdin(), .describe(), .deprecated()) to refine.
All args are required by default. Resolution order when extra sources are configured: CLI → stdin → env → config → prompt → default.
- Import:
@kjanat/dreamcli - Export kind: interface
- Declared in:
src/core/schema/arg.ts - Source link:
src/core/schema/arg.ts:2024
Signatures
interface ArgFactory {}Members
Methods
string
String-valued positional argument, with optional string constraints. Required by default.
Constraints are enforced at the parse and resolution boundaries, in fixed order: nonEmpty → minLength → maxLength → pattern. A .default() value is validated against them where the chain declares it. They also compose via chained methods (.nonEmpty(), .minLength(), .maxLength(), .pattern()), which override values set here.
string(constraints?: StringConstraints): ArgBuilder<{ valueType: string; presence: "required"; variadic: false; argKind: "string"; elementEligible: true; }>;number
Number-valued positional argument. Required by default.
The parser coerces the raw CLI string to a number and emits a ParseError if conversion fails. Env values are coerced the same way.
number(constraints?: NumberConstraints): ArgBuilder<{ valueType: number; presence: "required"; variadic: false; argKind: "number"; elementEligible: true; }>;boolean
Boolean positional argument. Required by default.
A positional carries no presence semantics, so the token spells the value out: true/false or 1/0 from argv, and also yes/no or an empty string from env, config, stdin, and a prompt.
boolean(): ArgBuilder<{ valueType: boolean; presence: "required"; variadic: false; argKind: "boolean"; elementEligible: true; }>;keyValue
Key-value positional argument. Required by default.
Consumes KEY=VALUE tokens and resolves to a record, split at the first =. The non-variadic form reads one token, the variadic form aggregates the whole tail. Later occurrences of the same key win, which .duplicateKeys() changes. Not promptable.
An element builder gives each entry value its own codec, constraints, and checks, so arg.keyValue(arg.path()) checks every value on disk.
keyValue<E extends ArgConfig & { elementEligible: true; }>(element?: ArgBuilder<E>): ArgBuilder<{ valueType: Record<string, E["valueType"]>; presence: "required"; variadic: false; argKind: "keyValue"; elementEligible: false; }>;enum
Enum-valued positional argument. Required by default.
Accepts only the listed string literals. The inferred type is the union of those literals (e.g. 'us' | 'eu' | 'ap'), not string. Invalid values produce a ParseError listing allowed options.
enum<T extends readonly [string, ...string[]]>(values: T): ArgBuilder<{ valueType: T[number]; presence: "required"; variadic: false; argKind: "enum"; elementEligible: true; }>;custom
Custom positional argument validated by a Standard Schema v1 validator (zod, valibot, arktype, …). The resolved value from any source is validated after resolution; the arg's value type is the validator's output type.
Sync and async validators are both supported. Validation issues surface as a CONSTRAINT_VIOLATED error naming the argument.
Custom-parsed positional argument. Required by default.
The parse function receives the raw string and must return a value of type T. Throw an Error (or ParseError) to signal invalid input. The same parse function is used for both CLI and env values.
custom<S extends StandardSchemaV1>(schema: S): ArgBuilder<{ valueType: InferStandardOutput<S>; presence: "required"; variadic: false; argKind: "custom"; elementEligible: true; }>;
custom<T>(parseFn: ArgParseFn<T>): ArgBuilder<{ valueType: T; presence: "required"; variadic: false; argKind: "custom"; elementEligible: true; }>;url
URL-valued positional argument. Parses into a URL; invalid URLs are rejected with an INVALID_VALUE error naming the argument.
url(options?: UrlFlagOptions): ArgBuilder<{ valueType: URL; presence: "required"; variadic: false; argKind: "custom"; elementEligible: true; }>;path
Path-valued positional argument. The value stays a string; optional filesystem checks run after resolution through the runtime adapter, so CLI, stdin, env, and defaulted values are all validated.
A variadic path arg checks every value it collects. Help renders a positional by its own name, so the 'path' value hint on the schema does not reach the usage line.
path(options?: PathFlagOptions): ArgBuilder<{ valueType: string; presence: "required"; variadic: false; argKind: "string"; elementEligible: true; }>;date
Date-valued positional argument. Accepts strict ISO-8601 (2026-07-10, 2026-07-10T14:30:00Z) and parses into a Date. Lenient Date.parse inputs ('0', 'March 5') and calendar-invalid dates (2026-02-31) are rejected.
Returns Date (not Temporal) because the supported runtimes do not all ship Temporal yet; use arg.custom() with Temporal.PlainDate.from where the target runtime has it.
date(options?: DateFlagOptions): ArgBuilder<{ valueType: Date; presence: "required"; variadic: false; argKind: "custom"; elementEligible: true; }>;duration
Duration positional argument. Accepts '30s', '5m', '1.5h', '250ms', '2d', compounds like '1h30m', or a bare millisecond count, and resolves to milliseconds.
duration(): ArgBuilder<{ valueType: number; presence: "required"; variadic: false; argKind: "custom"; elementEligible: true; }>;bytes
Byte-size positional argument. Accepts '512mb', '1.5gb', '64kb', '100b' or a bare byte count, and resolves to bytes. Units are binary (1kb = 1024) and case-insensitive.
bytes(): ArgBuilder<{ valueType: number; presence: "required"; variadic: false; argKind: "custom"; elementEligible: true; }>;Examples
Overview of common kinds with common modifier patterns
```ts
command('process')
// String arg with env fallback
.arg('input', arg.string()
.env('INPUT_FILE')
.describe('Input file path'))
// Number arg with env + default
.arg('concurrency', arg.number()
.env('CONCURRENCY')
.default(4)
.describe('Worker threads'))
// Custom arg — hex color parser with env
.arg('color', arg.custom((raw) => {
if (!/^#?[0-9a-f]{6}$/i.test(raw)) throw new Error('bad hex');
return raw.startsWith('#') ? raw : `#${raw}`;
})
.env('THEME_COLOR')
.optional()
.describe('Theme color (hex)'))
.action(({ args }) => {
args.input; // string (required)
args.concurrency; // number (defaulted)
args.color; // string | undefined (optional custom)
});
## See Also
- [Examples overview](/examples/)
- [Example Hover](/reference/example-hover-prototype)
- [API overview](/reference/api)
- [@kjanat/dreamcli](/reference/main)