Skip to content

Commit

Permalink
feat: different opt output name (default camelCase)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenasraf committed Dec 1, 2023
1 parent 2095c6a commit 48b9602
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 11 additions & 12 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -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. */
Expand All @@ -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
Expand All @@ -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. */
Expand All @@ -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})`
Expand Down
6 changes: 5 additions & 1 deletion src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand All @@ -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({
Expand Down
5 changes: 2 additions & 3 deletions test/command.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MassargCommand } from '../src/command'
import { defaultHelpConfig } from '../src/help'
import { massarg } from '../src/index'

const opts = {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -138,7 +137,7 @@ describe('getArgs', () => {
.getArgs(['test3']),
).toEqual({})
})
test.skip('extra values', () => {
test('extra values', () => {
expect(
massarg(opts)
.command({
Expand Down

0 comments on commit 48b9602

Please sign in to comment.