Skip to content

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(), .default(), .variadic(), .stdin(), .describe(), .deprecated()) to refine.

All args are required by default. Resolution order when extra sources are configured: CLI → stdin → env → default.

Signatures

ts
interface ArgFactory {}

Members

Methods

custom

ts
custom<T>(parseFn: ArgParseFn<T>): ArgBuilder<{ presence: "required"; valueType: T; variadic: false; }>;

enum

ts
enum<T extends readonly [string, string]>(values: T): ArgBuilder<{ presence: "required"; valueType: T[number]; variadic: false; }>;

number

ts
number(): ArgBuilder<{ presence: "required"; valueType: number; variadic: false; }>;

string

ts
string(): ArgBuilder<{ presence: "required"; valueType: string; variadic: false; }>;

Examples

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

Released under the MIT License.