Comparing version 7.0.1 to 7.1.0
@@ -28,3 +28,4 @@ import {PackageJson} from 'type-fest'; | ||
type AnyFlags = {[key: string]: StringFlag | BooleanFlag | NumberFlag}; | ||
type AnyFlag = StringFlag | BooleanFlag | NumberFlag; | ||
type AnyFlags = {[key: string]: AnyFlag}; | ||
@@ -41,3 +42,3 @@ interface Options<Flags extends AnyFlags> { | ||
- `isRequired`: Determine if the flag is required. | ||
If it's only known at runtime whether the flag is requried or not you can pass a Function instead of a boolean, which based on the given flags and other non-flag arguments should decide if the flag is required. | ||
If it's only known at runtime whether the flag is required or not you can pass a Function instead of a boolean, which based on the given flags and other non-flag arguments should decide if the flag is required. | ||
- `isMultiple`: Indicates a flag can be set multiple times. Values are turned into an array. (Default: false) | ||
@@ -200,10 +201,22 @@ | ||
type TypedFlags<Flags extends AnyFlags> = { | ||
[F in keyof Flags]: Flags[F] extends {type: 'number'} | ||
type TypedFlag<Flag extends AnyFlag> = | ||
Flag extends {type: 'number'} | ||
? number | ||
: Flags[F] extends {type: 'string'} | ||
: Flag extends {type: 'string'} | ||
? string | ||
: Flags[F] extends {type: 'boolean'} | ||
: Flag extends {type: 'boolean'} | ||
? boolean | ||
: unknown; | ||
type PossiblyOptionalFlag<Flag extends AnyFlag, FlagType> = | ||
Flag extends {isRequired: true} | ||
? FlagType | ||
: Flag extends {default: any} | ||
? FlagType | ||
: FlagType | undefined; | ||
type TypedFlags<Flags extends AnyFlags> = { | ||
[F in keyof Flags]: Flags[F] extends {isMultiple: true} | ||
? PossiblyOptionalFlag<Flags[F], Array<TypedFlag<Flags[F]>>> | ||
: PossiblyOptionalFlag<Flags[F], TypedFlag<Flags[F]>> | ||
}; | ||
@@ -210,0 +223,0 @@ |
31
index.js
'use strict'; | ||
const path = require('path'); | ||
const buildParserOptions = require('minimist-options'); | ||
const yargs = require('yargs-parser'); | ||
const camelCase = require('camelcase'); | ||
const camelcaseKeys = require('camelcase-keys'); | ||
const parseArguments = require('yargs-parser'); | ||
const camelCaseKeys = require('camelcase-keys'); | ||
const decamelizeKeys = require('decamelize-keys'); | ||
@@ -13,3 +12,2 @@ const trimNewlines = require('trim-newlines'); | ||
const normalizePackageData = require('normalize-package-data'); | ||
const arrify = require('arrify'); | ||
@@ -73,3 +71,3 @@ // Prevent caching of this module so module.parent is always accurate | ||
if (flag.isMultiple) { | ||
flag.type = 'array'; | ||
flag.type = flag.type ? `${flag.type}-array` : 'array'; | ||
delete flag.isMultiple; | ||
@@ -83,11 +81,2 @@ } | ||
/** | ||
Convert to alternative syntax for coercing values to expected type, according to https://github.com/yargs/yargs-parser#requireyargs-parserargs-opts. | ||
*/ | ||
const convertToTypedArrayOption = (arrayOption, flags) => | ||
arrify(arrayOption).map(flagKey => ({ | ||
key: flagKey, | ||
[flags[camelCase(flagKey, '-')].type || 'string']: true | ||
})); | ||
const validateFlags = (flags, options) => { | ||
@@ -148,11 +137,4 @@ for (const [flagKey, flagValue] of Object.entries(options.flags)) { | ||
if (parserOptions.array !== undefined) { | ||
// `yargs` supports 'string|number|boolean' arrays, | ||
// but `minimist-options` only support 'string' as element type. | ||
// Open issue to add support to `minimist-options`: https://github.com/vadimdemedes/minimist-options/issues/18. | ||
parserOptions.array = convertToTypedArrayOption(parserOptions.array, options.flags); | ||
} | ||
const {pkg} = options; | ||
const argv = yargs(options.argv, parserOptions); | ||
const argv = parseArguments(options.argv, parserOptions); | ||
let help = redent(trimNewlines((options.help || '').replace(/\t+\n*$/, '')), 2); | ||
@@ -194,3 +176,3 @@ | ||
const flags = camelcaseKeys(argv, {exclude: ['--', /^\w$/]}); | ||
const flags = camelCaseKeys(argv, {exclude: ['--', /^\w$/]}); | ||
const unnormalizedFlags = {...flags}; | ||
@@ -204,6 +186,3 @@ | ||
// Get a list of missing flags that are required | ||
const missingRequiredFlags = getMissingRequiredFlags(options.flags, flags, input); | ||
// Print error message for missing flags that are required | ||
if (missingRequiredFlags.length > 0) { | ||
@@ -210,0 +189,0 @@ reportMissingRequiredFlags(missingRequiredFlags); |
{ | ||
"name": "meow", | ||
"version": "7.0.1", | ||
"version": "7.1.0", | ||
"description": "CLI app helper", | ||
@@ -44,8 +44,6 @@ "license": "MIT", | ||
"@types/minimist": "^1.2.0", | ||
"arrify": "^2.0.1", | ||
"camelcase": "^6.0.0", | ||
"camelcase-keys": "^6.2.2", | ||
"decamelize-keys": "^1.1.0", | ||
"hard-rejection": "^2.1.0", | ||
"minimist-options": "^4.0.2", | ||
"minimist-options": "4.1.0", | ||
"normalize-package-data": "^2.5.0", | ||
@@ -52,0 +50,0 @@ "read-pkg-up": "^7.0.1", |
@@ -141,3 +141,3 @@ # meow [![Build Status](https://travis-ci.org/sindresorhus/meow.svg?branch=master)](https://travis-ci.org/sindresorhus/meow) | ||
- `isRequired`: Determine if the flag is required. (Default: false) | ||
- If it's only known at runtime whether the flag is requried or not, you can pass a `Function` instead of a `boolean`, which based on the given flags and other non-flag arguments, should decide if the flag is required. Two arguments are passed to the function: | ||
- If it's only known at runtime whether the flag is required or not, you can pass a `Function` instead of a `boolean`, which based on the given flags and other non-flag arguments, should decide if the flag is required. Two arguments are passed to the function: | ||
- The first argument is the **flags** object, which contains the flags converted to camel-case excluding aliases. | ||
@@ -144,0 +144,0 @@ - The second argument is the **input** string array, which contains the non-flag arguments. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11
22577
402
- Removedarrify@^2.0.1
- Removedcamelcase@^6.0.0
- Removedarrify@2.0.1(transitive)
- Removedcamelcase@6.3.0(transitive)
Updatedminimist-options@4.1.0