Skip to content

Commit

Permalink
fix: throw if unexpected argument (#491)
Browse files Browse the repository at this point in the history
* fix: throw if unexpected argument

* chore: formatting
  • Loading branch information
mdonnalley committed Sep 16, 2022
1 parent b6062e3 commit da6d20c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/interfaces/parser.ts
Expand Up @@ -118,7 +118,7 @@ export type FlagProps = {
/**
* Accept an environment variable as input
*/
env?: string;
env?: string;
/**
* If true, the flag will not be shown in the help.
*/
Expand All @@ -142,7 +142,7 @@ export type FlagProps = {
/**
* Define complex relationships between flags.
*/
relationships?: Relationship[];
relationships?: Relationship[];
}

export type BooleanFlagProps = FlagProps & {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parse.ts
Expand Up @@ -258,7 +258,7 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
stdinRead = true
}

if (!args[i] && 'default' in arg) {
if (!args[i] && arg?.default !== undefined) {
if (typeof arg.default === 'function') {
// eslint-disable-next-line no-await-in-loop
const f = await arg.default()
Expand Down
11 changes: 11 additions & 0 deletions test/parser/parse.test.ts
Expand Up @@ -7,6 +7,7 @@ import {Interfaces} from '../../src'
import {URL} from 'url'
import {directory, file} from '../../src/parser/flags'
import * as sinon from 'sinon'
import {CLIError} from '../../src/errors'

const stripAnsi = require('strip-ansi')

Expand Down Expand Up @@ -36,6 +37,16 @@ describe('parse', () => {
expect(out.args).to.deep.equal({foo: 'arg1', bar: 'arg2'})
})

it('should throw if unexpected argument is provided', async () => {
try {
await parse(['arg1'], {})
expect.fail('should have thrown')
} catch (error) {
const err = error as CLIError
expect(err.message).to.include('Unexpected argument: arg1')
}
})

describe('output: array', () => {
it('--bool', async () => {
const out = await parse(['--bool'], {
Expand Down

0 comments on commit da6d20c

Please sign in to comment.