Skip to content

Commit a0f015f

Browse files
committed
docs: update
1 parent 87f98ba commit a0f015f

3 files changed

Lines changed: 50 additions & 2 deletions

File tree

src/command.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,12 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject>
491491
}
492492
}
493493

494+
/**
495+
* A command that prints help for this command, or a sub-command if specified.
496+
*
497+
* This command is automatically added to the top-level command if you use `bindCommand: true` in `help()`.
498+
* You can also add it manually to any command.
499+
*/
494500
export class MassargHelpCommand<
495501
T extends { command?: string } = { command?: string },
496502
> extends MassargCommand<T> {

src/massarg.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import { ArgsObject, CommandConfig, MassargCommand } from './command'
22

3-
type MinimalCommandConfig<Args extends ArgsObject = ArgsObject> = Omit<
3+
/** A minimal command config that can be used to create a top-level command. */
4+
export type MinimalCommandConfig<Args extends ArgsObject = ArgsObject> = Omit<
45
CommandConfig<Args>,
56
'aliases' | 'run'
67
> &
78
Partial<Pick<CommandConfig<Args>, 'aliases' | 'run'>>
89

10+
/**
11+
* This class behaves similarly to {@link MassargCommand}, but it accepts only params that are relevant
12+
* to the top-level command.
13+
*
14+
* @see {@link massarg}
15+
* @see {@link MassargCommand}
16+
*/
917
export class Massarg<Args extends ArgsObject = ArgsObject> extends MassargCommand<Args> {
1018
constructor(options: MinimalCommandConfig<Args>) {
1119
// TODO consider re-using name and description for general help, and pass them to super
@@ -20,6 +28,11 @@ export class Massarg<Args extends ArgsObject = ArgsObject> extends MassargComman
2028
}
2129
}
2230

31+
/**
32+
* Creates a new top-level command.
33+
* @see {@link MassargCommand}
34+
* @see {@link Massarg}
35+
*/
2336
export function massarg<Args extends ArgsObject = ArgsObject>(
2437
options: MinimalCommandConfig<Args>,
2538
): MassargCommand<Args> {

src/option.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ export type OptionConfig<T = unknown, Args extends ArgsObject = ArgsObject> = z.
5050
ReturnType<typeof OptionConfig<T, Args>>
5151
>
5252

53+
/**
54+
* Configuration for a flag (boolean argument) that can be passed to a command.
55+
*/
5356
export const FlagConfig = OptionConfig<boolean>(z.any())
5457
.omit({ parse: true, isDefault: true })
5558
.merge(
@@ -84,6 +87,7 @@ export type Parser<Args extends ArgsObject = ArgsObject, OptionType extends any
8487
y: Args,
8588
) => OptionType
8689

90+
/** {@link OptionConfig} with a specified value type */
8791
export const TypedOptionConfig = <OptionType, Args extends ArgsObject = ArgsObject>(
8892
type: z.ZodType<OptionType>,
8993
) =>
@@ -117,7 +121,9 @@ export type ArrayOptionConfig<T = unknown> = z.infer<
117121
ReturnType<typeof ArrayOptionConfig<z.ZodType<T>>>
118122
>
119123

124+
/** The default prefixes for options */
120125
export const DEFAULT_OPT_FULL_PREFIX = '--'
126+
/** The default prefix for option aliases */
121127
export const DEFAULT_OPT_SHORT_PREFIX = '-'
122128

123129
/* Prefixes for options */
@@ -173,7 +179,12 @@ export class MassargOption<OptionType extends any = unknown, Args extends ArgsOb
173179
defaultValue?: OptionType
174180
aliases: string[]
175181
parse: Parser<Args, OptionType>
182+
/**
183+
* Whether this option can be used multiple times. Any passed values will end up in an array
184+
* instead of each usage overwriting the existing value.
185+
*/
176186
isArray: boolean
187+
/** Whether this option is required. Failing to specify this option will throw an error. */
177188
isRequired: boolean
178189
isDefault: boolean
179190
outputName?: string
@@ -191,6 +202,10 @@ export class MassargOption<OptionType extends any = unknown, Args extends ArgsOb
191202
this.outputName = options.outputName
192203
}
193204

205+
/**
206+
* Create a typed option from a configuration. Currently supports `number` options which
207+
* are automatically transformed from `string` to `number`.
208+
*/
194209
static fromTypedConfig<T = unknown, A extends ArgsObject = ArgsObject>(
195210
config: TypedOptionConfig<T, A>,
196211
): MassargOption<T> {
@@ -201,10 +216,18 @@ export class MassargOption<OptionType extends any = unknown, Args extends ArgsOb
201216
return new MassargOption(config as OptionConfig<T>)
202217
}
203218

219+
/**
220+
* Returns the key which this option outputs to in the final object.
221+
*
222+
* @default The camelCase version of this option's name.
223+
*
224+
* Can be overridden with {@link outputName}.
225+
*/
204226
getOutputName(): string {
205227
return this.outputName || toCamelCase(this.name)
206228
}
207229

230+
/** @internal */
208231
parseDetails(argv: string[], options: ArgsObject, prefixes: Prefixes): ArgvValue<OptionType> {
209232
let input = ''
210233
try {
@@ -233,6 +256,7 @@ export class MassargOption<OptionType extends any = unknown, Args extends ArgsOb
233256
}
234257
}
235258

259+
/** Get the help string for this option */
236260
helpString(): string {
237261
const aliases = this.aliases.length ? `|${this.aliases.join('|-')}` : ''
238262
return `--${this.name}${aliases} ${this.description}`
@@ -249,6 +273,7 @@ export class MassargOption<OptionType extends any = unknown, Args extends ArgsOb
249273
)
250274
}
251275

276+
/** Return the finalized names that will cause this option to match. */
252277
qualifiedNames(prefixes: Prefixes): QualifiedNames {
253278
return {
254279
name: prefixes.normalPrefix + this.name,
@@ -310,7 +335,7 @@ export class MassargNumber extends MassargOption<number> {
310335
}
311336

312337
/**
313-
* An option that can be passed to a command.
338+
* A boolean option that can be passed to a command.
314339
*
315340
* A flag is an option that is either present or not. It can be used to toggle
316341
* a boolean value, or to indicate that a command should be run in a different
@@ -332,8 +357,11 @@ export class MassargNumber extends MassargOption<number> {
332357
* ```
333358
*/
334359
export class MassargFlag extends MassargOption<boolean> {
360+
/** Whether this flag may be negated using `negationName` or `negationAliases`. */
335361
negatable: boolean
362+
/** The negation name of this flag, which can be used with the full option notation. */
336363
negationName: string
364+
/** The negation aliases of this flag, which can be used with the shorthand option notation. */
337365
negationAliases: string[]
338366

339367
constructor(options: FlagConfig) {
@@ -396,6 +424,7 @@ export class MassargFlag extends MassargOption<boolean> {
396424
}
397425
}
398426

427+
/** A flag that can be passed to a command to show the help message. */
399428
export class MassargHelpFlag extends MassargFlag {
400429
constructor(config: Partial<Omit<OptionConfig<boolean>, 'parse'>> = {}) {
401430
super({

0 commit comments

Comments
 (0)