diff --git a/src/command.ts b/src/command.ts index e0f48c8..22ca181 100644 --- a/src/command.ts +++ b/src/command.ts @@ -316,7 +316,7 @@ export class MassargCommand { // 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 { 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 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) diff --git a/src/error.ts b/src/error.ts index 4601d07..a1788b5 100644 --- a/src/error.ts +++ b/src/error.ts @@ -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})` diff --git a/src/option.ts b/src/option.ts index f42eb4a..18760e6 100644 --- a/src/option.ts +++ b/src/option.ts @@ -135,6 +135,10 @@ export class MassargOption { return new MassargOption(config as OptionConfig) } + getOutputName(): string { + return this.outputName || toCamelCase(this.name) + } + _parseDetails(argv: string[]): ArgvValue { // TODO: support --option=value let input = '' @@ -150,7 +154,7 @@ export class MassargOption { 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({ diff --git a/test/command.test.ts b/test/command.test.ts index d9b6b2f..eb0c3a1 100644 --- a/test/command.test.ts +++ b/test/command.test.ts @@ -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({