feat: different opt output name (default camelCase)

This commit is contained in:
2023-11-27 22:25:58 +02:00
committed by Chen Asraf
parent 8bbc0c4594
commit 76e0f85e00
4 changed files with 21 additions and 19 deletions

View File

@@ -316,7 +316,7 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
// fill defaults
for (const option of this.options) {
if (option.defaultValue !== undefined && _a[option.name] === undefined) {
_args[option.name as keyof Args] = option.defaultValue as Args[keyof Args]
_args[option.getOutputName() as keyof Args] = option.defaultValue as Args[keyof Args]
}
}
@@ -336,7 +336,8 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
if (command) {
// this is dry run, just exit
if (!parseCommands) {
break
return command.getArgs(_argv, this.args, parent ?? this, false)
// break
}
// this is real run, parse command, pass unparsed args
return command.parse(_argv, this.args, parent ?? this)
@@ -385,7 +386,6 @@ export class MassargHelpCommand<T extends ArgsObject = ArgsObject> extends Massa
name: 'help',
aliases: ['h'],
description: 'Print help for this command, or a subcommand if specified',
// argsHint: "[command]",
run: (args, parent) => {
if (args.command) {
const command = parent.commands.find((c) => c.name === args.command)

View File

@@ -1,5 +1,7 @@
import { z } from 'zod'
export type ValidationErrorOptions = { path: string[]; code: string; message: string }
/** This error is thrown when a validation fails. */
export class ValidationError extends Error {
/** The path to the value that failed validation. */
@@ -9,7 +11,7 @@ export class ValidationError extends Error {
/** The error message. */
message: string
constructor({ path, code, message }: { path: string[]; code: string; message: string }) {
constructor({ path, code, message }: ValidationErrorOptions) {
const msg = `${path.join('.')}: ${message}`
super(msg)
this.path = path
@@ -19,6 +21,13 @@ export class ValidationError extends Error {
}
}
export type ParseErrorOptions = {
path: string[]
code: string
message: string
received?: unknown
}
/** This error is thrown when a parse fails on an option value. */
export class ParseError extends Error {
/** The path to the value that failed parsing. */
@@ -30,17 +39,7 @@ export class ParseError extends Error {
/** The value that failed parsing. */
received: unknown
constructor({
path,
code,
message,
received,
}: {
path: string[]
code: string
message: string
received?: unknown
}) {
constructor({ path, code, message, received }: ParseErrorOptions) {
let msg = `${path.join('.')}: ${message}`
if (received) {
msg += ` (received: ${received})`

View File

@@ -135,6 +135,10 @@ export class MassargOption<T = unknown> {
return new MassargOption(config as OptionConfig<T>)
}
getOutputName(): string {
return this.outputName || toCamelCase(this.name)
}
_parseDetails(argv: string[]): ArgvValue<T> {
// TODO: support --option=value
let input = ''
@@ -150,7 +154,7 @@ export class MassargOption<T = unknown> {
argv.shift()
input = argv.shift()!
const value = this.parse(input)
return { key: this.outputName || toCamelCase(this.name), value, argv }
return { key: this.getOutputName(), value, argv }
} catch (e) {
if (isZodError(e)) {
throw new ParseError({

View File

@@ -1,5 +1,4 @@
import { MassargCommand } from '../src/command'
import { defaultHelpConfig } from '../src/help'
import { massarg } from '../src/index'
const opts = {
@@ -55,7 +54,7 @@ describe('getArgs', () => {
massarg(opts)
.command({ name: 'test', description: 'test', run: jest.fn() })
.getArgs(['test', '--test', 'test']),
).toEqual({})
).toEqual({ extra: ['--test', 'test'] })
})
test('alias', () => {
@@ -138,7 +137,7 @@ describe('getArgs', () => {
.getArgs(['test3']),
).toEqual({})
})
test.skip('extra values', () => {
test('extra values', () => {
expect(
massarg(opts)
.command({