Skip to content

Commit

Permalink
docs: update
Browse files Browse the repository at this point in the history
  • Loading branch information
chenasraf committed Jan 28, 2024
1 parent 87f98ba commit a0f015f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,12 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject>
}
}

/**
* A command that prints help for this command, or a sub-command if specified.
*
* This command is automatically added to the top-level command if you use `bindCommand: true` in `help()`.
* You can also add it manually to any command.
*/
export class MassargHelpCommand<
T extends { command?: string } = { command?: string },
> extends MassargCommand<T> {
Expand Down
15 changes: 14 additions & 1 deletion src/massarg.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { ArgsObject, CommandConfig, MassargCommand } from './command'

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

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

/**
* Creates a new top-level command.
* @see {@link MassargCommand}
* @see {@link Massarg}
*/
export function massarg<Args extends ArgsObject = ArgsObject>(
options: MinimalCommandConfig<Args>,
): MassargCommand<Args> {
Expand Down
31 changes: 30 additions & 1 deletion src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export type OptionConfig<T = unknown, Args extends ArgsObject = ArgsObject> = z.
ReturnType<typeof OptionConfig<T, Args>>
>

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

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

/** The default prefixes for options */
export const DEFAULT_OPT_FULL_PREFIX = '--'
/** The default prefix for option aliases */
export const DEFAULT_OPT_SHORT_PREFIX = '-'

/* Prefixes for options */
Expand Down Expand Up @@ -173,7 +179,12 @@ export class MassargOption<OptionType extends any = unknown, Args extends ArgsOb
defaultValue?: OptionType
aliases: string[]
parse: Parser<Args, OptionType>
/**
* 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.
*/
isArray: boolean
/** Whether this option is required. Failing to specify this option will throw an error. */
isRequired: boolean
isDefault: boolean
outputName?: string
Expand All @@ -191,6 +202,10 @@ export class MassargOption<OptionType extends any = unknown, Args extends ArgsOb
this.outputName = options.outputName
}

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

/**
* Returns the key which this option outputs to in the final object.
*
* @default The camelCase version of this option's name.
*
* Can be overridden with {@link outputName}.
*/
getOutputName(): string {
return this.outputName || toCamelCase(this.name)
}

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

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

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

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

constructor(options: FlagConfig) {
Expand Down Expand Up @@ -396,6 +424,7 @@ export class MassargFlag extends MassargOption<boolean> {
}
}

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

0 comments on commit a0f015f

Please sign in to comment.