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 2095c6a7df
commit 48b96022e4
4 changed files with 21 additions and 19 deletions

View File

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

View File

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

View File

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

View File

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