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

feat: add dynamicTypes option to use function when looking up types #125

Merged
merged 1 commit into from
Jun 14, 2023

Conversation

lukekarrys
Copy link
Contributor

@lukekarrys lukekarrys commented May 30, 2023

Note: this is stacked on #124 since it had some potential conflicts. That one should land first.

This PR adds an option named dynamicTypes to go along with the existing types option.

Currently, nopt looks up each potential key in the types object. If types doesn't have a property by that name, then the current fallback is to parse it as a boolean and leave the value in argv.remain.

With dynamicTypes, you can specify a function that will receive each unknown key and can either return a type or undefined to keep the current behavior.

Here's an example:

const args = [
  '--known', '100',
  '--unknown', '50',
  '--other', '1',
]
nopt.nopt(args, {
  types: { known: Number },
  dynamicTypes: (k) => {
    if (k === 'other') {
      return Number
    }
  },
  typeDefs: nopt.typeDefs,
  shorthands: {},
})
/* {
  known: 100,
  unknown: true,
  other: 1,
  argv: {
    remain: ['50'],
    cooked: ['--known', '100', '--unknown', '50', '--other', '1'],
    original: ['--known', '100', '--unknown', '50', '--other', '1'],
  },
} */

@lukekarrys lukekarrys requested a review from a team as a code owner May 30, 2023 21:44
@lukekarrys lukekarrys self-assigned this Jun 6, 2023
Base automatically changed from lk/set-default-type to main June 14, 2023 18:48
@lukekarrys lukekarrys changed the title feat: add fallback option to use function when looking up types feat: add dyanmicTypes option to use function when looking up types Jun 14, 2023
@lukekarrys lukekarrys changed the title feat: add dyanmicTypes option to use function when looking up types feat: add dynamicTypes option to use function when looking up types Jun 14, 2023
@lukekarrys lukekarrys merged commit 008c672 into main Jun 14, 2023
@lukekarrys lukekarrys deleted the lk/fallback-types branch June 14, 2023 19:36
@github-actions github-actions bot mentioned this pull request Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants