Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw if unexpected argument #491

Merged
merged 2 commits into from Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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