Skip to content

Commit

Permalink
Merge pull request #983 from oclif/mdonnalley/empty-string-arg
Browse files Browse the repository at this point in the history
fix: allow arg to be empty string
  • Loading branch information
shetzel authored Mar 4, 2024
2 parents e705c91 + 629d482 commit 1cfbf17
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export class Parser<
}

for (const token of tokens) {
if (args[token.arg]) continue
if (args[token.arg] !== undefined) continue
argv.push(token.input)
}

Expand Down
3 changes: 2 additions & 1 deletion src/parser/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export async function validate(parse: {input: ParserInput; output: ParserOutput}
})
}

if (arg.required && !parse.output.args[name] && parse.output.args[name] !== 0) {
// Only add if it's required and undefined. Allow falsy values like empty strings and 0.
if (arg.required && parse.output.args[name] === undefined) {
missingRequiredArgs.push(arg)
}
}
Expand Down
25 changes: 25 additions & 0 deletions test/parser/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,31 @@ describe('validate', () => {
await validate({input, output})
})

it('allows empty string as arg', async () => {
const input = {
argv: [],
flags: {},
args: {
emptyString: {required: true},
},
strict: true,
context: {},
'--': true,
}

const output = {
args: {emptyString: ''},
argv: [''],
raw: [],
metadata: {
flags: {},
},
}

// @ts-expect-error
await validate({input, output})
})

it('throws when required flag is undefined', async () => {
const input = {
argv: [],
Expand Down

0 comments on commit 1cfbf17

Please sign in to comment.