Skip to content

Commit

Permalink
feat(command): add support for input filter for non-interactive
Browse files Browse the repository at this point in the history
the input filter, previously supported for interactive mode via inquirer
allow transformation of the input before validation happens. This allows
for a great deal of functionality and flexibility for end users being
able to fix typos, enter short hands, etc. This is being extended into
the non-interactive commands as well
  • Loading branch information
esatterwhite committed Dec 29, 2023
1 parent c028655 commit dd854a1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/command/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ class Command extends Registry {
return this.parsed
}
const value = hasOwn(this.parsed, key) ? this.parsed[key] : flag.default
object.set(this.parsed, key, value)
object.set(
this.parsed
, key
, typeof flag.filter === 'function' ? flag.filter(value, this.parsed) : value
)
}
return this.parsed
}
Expand Down Expand Up @@ -648,7 +652,7 @@ function toQuestion(flag, opts, answers) {
// can return them
arg.when = opts.when ? opts.when.bind(null, answers) : undefined
arg.validate = opts.validate ? opts.validate.bind(null, answers) : undefined
arg.filter = opts.filter ? opts.filter.bind(null, answers) : undefined
arg.filter = opts.filter ? opts.filter.bind(null) : undefined
arg.transformer = opts.transformer ? opts.transformer : transform.bind(arg)
return arg
}
Expand Down
5 changes: 4 additions & 1 deletion test/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ test('command', async (t) => {
, 'foo:bar:baz': {
type: Number
, required: true
, filter(input) {
return input + 100
}
}
, 'nested:array': {
type: [Number, Array]
Expand All @@ -71,7 +74,7 @@ test('command', async (t) => {
t.match(data, {
foo: {
bar: {
baz: Number
baz: 112
}
}
, test: Boolean
Expand Down

0 comments on commit dd854a1

Please sign in to comment.