Skip to main content

Class: MassargOption<OptionType, Args>

option.MassargOption

An option that can be passed to a command.

Options can be specified in two ways:

  • Using the long form, e.g. --option value
  • Using the short form, e.g. -o value

They can also have a parse function, which will be used to parse the value passed in from the original argument (string).

Example

massarg(options).option({
name: 'option',
description: 'An option',
defaultValue: 'default',
aliases: ['o'],
parse: (value) => value.toUpperCase(),
})

Type parameters

NameType
OptionTypeextends any = unknown
Argsextends ArgsObject = ArgsObject

Hierarchy

Implements

Constructors

constructor

new MassargOption<OptionType, Args>(options): MassargOption<OptionType, Args>

Type parameters

NameType
OptionTypeextends unknown = unknown
Argsextends ArgsObject = ArgsObject

Parameters

NameTypeDescription
optionsObject-
options.namestringName of the option
options.descriptionstringDescription of the option, displayed in the help output
options.aliasesstring[]Aliases for the option, which can be used with the shorthand option notation.
options.defaultValue?anyDefault value of the option
options.array?booleanWhether the option is an array. Array options can be specified multiple times, and the values will be collected into an array. Normally, specifying an option multiple times will override the previous value.
options.required?booleanWhether the option is required. If it is required, parsing will throw an error if it's not present.
options.isDefault?booleanWhether the option is the default option. The default option is the option that is used if no other option is specified, e.g. a value is passed in without an option name. Note that if commands match the same argument first, they will be used instead of the default option.
options.hidden?booleanWhether the option is hidden. Hidden options are not displayed in the help output.
options.outputName?stringSpecify a custom name for the output, which will be used when parsing the args.
options.parse?Parser<Args, OptionType>Parse the value of the option. You can return any type here, or throw an error if the value is invalid.

Returns

MassargOption<OptionType, Args>

Defined in

src/option.ts:192

Properties

name

name: string

Implementation of

OptionConfig.name

Defined in

src/option.ts:177


description

description: string

Implementation of

OptionConfig.description

Defined in

src/option.ts:178


defaultValue

Optional defaultValue: OptionType

Implementation of

OptionConfig.defaultValue

Defined in

src/option.ts:179


aliases

aliases: string[]

Implementation of

OptionConfig.aliases

Defined in

src/option.ts:180


parse

parse: Parser<Args, OptionType>

Implementation of

OptionConfig.parse

Defined in

src/option.ts:181


isArray

isArray: boolean

Whether this option can be used multiple times. Any passed values will end up in an array instead of each usage overwriting the existing value.

Defined in

src/option.ts:186


isRequired

isRequired: boolean

Whether this option is required. Failing to specify this option will throw an error.

Defined in

src/option.ts:188


isDefault

isDefault: boolean

Implementation of

OptionConfig.isDefault

Defined in

src/option.ts:189


outputName

Optional outputName: string

Implementation of

OptionConfig.outputName

Defined in

src/option.ts:190

Methods

fromTypedConfig

fromTypedConfig<T, A>(config): MassargOption<T, ArgsObject>

Create a typed option from a configuration. Currently supports number options which are automatically transformed from string to number.

Type parameters

NameType
Tunknown
Aextends ArgsObject = ArgsObject

Parameters

NameType
configObject
config.namestring
config.descriptionstring
config.aliasesstring[]
config.typeundefined | "number"
config.defaultValueany
config.arrayundefined | boolean
config.requiredundefined | boolean
config.isDefaultundefined | boolean
config.hiddenundefined | boolean
config.outputNameundefined | string
config.parseundefined | Parser<A, T>

Returns

MassargOption<T, ArgsObject>

Defined in

src/option.ts:209


getOutputName

getOutputName(): string

Returns the key which this option outputs to in the final object.

Returns

string

Default

The camelCase version of this option's name.

Can be overridden with {@link outputName}.

Defined in

src/option.ts:226


helpString

helpString(): string

Get the help string for this option

Returns

string

Defined in

src/option.ts:260


isMatch

isMatch(arg, prefixes): boolean

Returns true if the flag (including any prefixes) matches the name or aliases

Parameters

NameType
argstring
prefixesPrefixes

Returns

boolean

Defined in

src/option.ts:266


qualifiedNames

qualifiedNames(prefixes): QualifiedNames

Return the finalized names that will cause this option to match.

Parameters

NameType
prefixesPrefixes

Returns

QualifiedNames

Defined in

src/option.ts:277