Skip to content

flag ​

Creators for flags and switches. See the Flags guide for usage.

flag ​

ts
const flag: {
	string<C extends string = string>(options: FlagOptions & StringOptions<C>): Flag<C | undefined, C>;
	number(options: FlagOptions & NumberOptions): Flag<number | undefined, number>;
	url(options: FlagOptions): Flag<URL | undefined, URL>;
	email(options: FlagOptions): Flag<string | undefined, string>;
	directory(options: FlagOptions & PathOptions): Flag<string | undefined, string>;
	file(options: FlagOptions & PathOptions): Flag<string | undefined, string>;
	boolean(options: FlagOptions): Switch<boolean>;
	count(options: FlagOptions): Switch<number>;
	custom<V, O>(parse: Parse<V, O>, valueName?: string): (options: FlagOptions & O) => Flag<V | undefined, V>;
	schema<S extends StandardSchemaV1>(schema: S, valueName?: string): (options: FlagOptions) => Flag<SchemaOutput<S> | undefined, SchemaOutput<S>>;
};
CreatorValueValue name in helpNotes
flag.stringstringstringStringOptions: choices narrows the type
flag.numbernumbernumberNumberOptions: min, max, integer
flag.urlURLurl
flag.emailstringemail
flag.directorystringdirectoryAbsolute path. PathOptions: exists
flag.filestringfileAbsolute path. PathOptions: exists
flag.booleanbooleanSwitch, defaults to false
flag.countnumberSwitch, defaults to 0
flag.customYour parser'svalueName (value)See Parse
flag.schemaSchema outputvalueName (value)The schema's input must accept a string

Flag ​

A flag that takes a value. T is the resolved type (e.g. string[] | undefined) and V is what one value parses to. Extends Param.

ts
class Flag<T, V extends Defined> extends Param<T, V> {
	readonly kind: 'flag';
	readonly options: FlagOptions;
	required(required?: boolean): Flag<…>;
	multiple(allow?: boolean): Flag<…>;
	default(value: DefaultValue<T, V>): Flag<…>;
}
MethodEffect
.required(on?)Must be provided. Clears any default.
.multiple(on?)Repeatable, collected into an array. Converts any existing default (v ↔ [v]).
.default(value)Used when absent. Clears required. Must be a non-empty array when multiple.

Each returns a new flag: flags are immutable.

Switch ​

A flag that never takes a value: flag.boolean and flag.count. Always resolves to a value. Extends Param.

ts
class Switch<T> extends Param<T, T> {
	readonly kind: 'switch';
	readonly options: FlagOptions;
	default(value: T): Switch<T>;
	resolveSwitch(occurrences: readonly boolean[]): T;
}

resolveSwitch turns occurrences (--x → true, --no-x → false) into the value, or the default when there are none.

FlagOptions ​

ts
interface FlagOptions {
	description: string;
	alias?: string;
	hidden?: boolean;
	env?: string;
}
OptionDescription
descriptionShown in help.
aliasAnother name, without dashes: 'v' → -v, 'dir' → --dir.
hiddenLeave out of help.
envEnvironment variable to read when the flag isn't given. See Environment variables.

FlagRecord ​

ts
type FlagRecord = Record<string, AnyFlag>;

An object of flags, as passed to flags.

AnyFlag ​

ts
type AnyFlag = Flag<any, any> | Switch<any>;

Any flag or switch, for constraints in generic code.