diff --git a/lib/command.ts b/lib/command.ts index 7055b38ac..ab0328522 100644 --- a/lib/command.ts +++ b/lib/command.ts @@ -324,7 +324,7 @@ export function command ( populatePositional(maybe, argv, positionalMap) } - argv._ = context.commands.concat(argv._) + argv._ = context.commands.concat(argv._.map(a => '' + a)) postProcessPositionals(argv, positionalMap, self.cmdToParseOptions(commandHandler.original)) diff --git a/lib/completion.ts b/lib/completion.ts index 5f428a3d5..77075abda 100644 --- a/lib/completion.ts +++ b/lib/completion.ts @@ -5,7 +5,7 @@ import { parseCommand } from './parse-command' import * as path from 'path' import { UsageInstance } from './usage' import { YargsInstance } from './yargs' -import { Arguments, DetailedArguments } from 'yargs-parser' +import { Arguments, DetailedArguments } from 'yargs-parser/build/lib/yargs-parser-types' import { assertNotStrictEqual } from './common-types' // add bash completions to your diff --git a/lib/typings/yargs-parser.d.ts b/lib/typings/yargs-parser.d.ts deleted file mode 100644 index cc4f4c08f..000000000 --- a/lib/typings/yargs-parser.d.ts +++ /dev/null @@ -1,124 +0,0 @@ -// TODO: either update @types/yargs-parser with this or convert yargs-parser to typescript -// Forked from: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/699b8159a6f571a14ef6d1d07b956cf78c8e729c/types/yargs-parser/index.d.ts - -/* eslint no-redeclare: "off" */ -declare namespace yargsParser { - interface Arguments { - /** Non-option arguments */ - _: string[]; - /** All remaining options */ - [argName: string]: any; - } - - interface DetailedArguments { - /** An object representing the parsed value of `args` */ - argv: Arguments; - /** Populated with an error object if an exception occurred during parsing. */ - error: Error | null; - /** The inferred list of aliases built by combining lists in opts.alias. */ - aliases: { [alias: string]: string[] }; - /** Any new aliases added via camel-case expansion. */ - newAliases: { [alias: string]: boolean }; - /** The configuration loaded from the yargs stanza in package.json. */ - configuration: Configuration; - } - - interface Configuration { - /** Should variables prefixed with --no be treated as negations? Default is `true` */ - 'boolean-negation': boolean; - /** Should hyphenated arguments be expanded into camel-case aliases? Default is `true` */ - 'camel-case-expansion': boolean; - /** Should arrays be combined when provided by both command line arguments and a configuration file. Default is `false` */ - 'combine-arrays': boolean; - /** Should keys that contain . be treated as objects? Default is `true` */ - 'dot-notation': boolean; - /** Should arguments be coerced into an array when duplicated. Default is `true` */ - 'duplicate-arguments-array': boolean; - /** Should array arguments be coerced into a single array when duplicated. Default is `true` */ - 'flatten-duplicate-arrays': boolean; - /** Should arrays consume more than one positional argument following their flag? Default is `true` */ - 'greedy-arrays': boolean; - /** Should parsing stop at the first text argument? This is similar to how e.g. ssh parses its command line. Default is `false` */ - 'halt-at-non-option': boolean; - /** Should nargs consume dash options as well as positional arguments? Default is `false` */ - 'nargs-eats-options': boolean; - /** The prefix to use for negated boolean variables. Default is `'no-'` */ - 'negation-prefix': string; - /** Should keys that look like numbers be treated as such? Default is `true` */ - 'parse-numbers': boolean; - /** Should unparsed flags be stored in -- or _. Default is `false` */ - 'populate--': boolean; - /** Should a placeholder be added for keys not set via the corresponding CLI argument? Default is `false` */ - 'set-placeholder-key': boolean; - /** Should a group of short-options be treated as boolean flags? Default is `true` */ - 'short-option-groups': boolean; - /** Should aliases be removed before returning results? Default is `false` */ - 'strip-aliased': boolean; - /** Should dashed keys be removed before returning results? This option has no effect if camel-case-expansion is disabled. Default is `false` */ - 'strip-dashed': boolean; - /** Should unknown options be treated like regular arguments? An unknown option is one that is not configured in opts. Default is `false` */ - 'unknown-options-as-args': boolean; - } - - type ArrayOption = string | { key: string; string?: boolean, boolean?: boolean, number?: boolean, integer?: boolean }; - - interface Options { - /** An object representing the set of aliases for a key: `{ alias: { foo: ['f']} }`. */ - alias: { [key: string]: string | string[] }; - /** - * Indicate that keys should be parsed as an array: `{ array: ['foo', 'bar'] }`. - * Indicate that keys should be parsed as an array and coerced to booleans / numbers: - * { array: [ { key: 'foo', boolean: true }, {key: 'bar', number: true} ] }`. - */ - array: ArrayOption | ArrayOption[]; - /** Arguments should be parsed as booleans: `{ boolean: ['x', 'y'] }`. */ - boolean: string | string[]; - /** Indicate a key that represents a path to a configuration file (this file will be loaded and parsed). */ - config: string | string[] | { [key: string]: boolean | ConfigCallback }; - /** configuration objects to parse, their properties will be set as arguments */ - configObjects: Array<{ [key: string]: any }>; - /** Provide configuration options to the yargs-parser. */ - configuration: Partial; - /** - * Provide a custom synchronous function that returns a coerced value from the argument provided (or throws an error), e.g. - * `{ coerce: { foo: function (arg) { return modifiedArg } } }`. - */ - coerce: { [key: string]: CoerceCallback }; - /** Indicate a key that should be used as a counter, e.g., `-vvv = {v: 3}`. */ - count: string | string[]; - /** Provide default values for keys: `{ default: { x: 33, y: 'hello world!' } }`. */ - default: { [key: string]: any }; - /** Environment variables (`process.env`) with the prefix provided should be parsed. */ - envPrefix: string; - /** Specify that a key requires n arguments: `{ narg: {x: 2} }`. */ - narg: { [key: string]: number }; - /** `path.normalize()` will be applied to values set to this key. */ - normalize: string | string[]; - /** Keys should be treated as strings (even if they resemble a number `-x 33`). */ - string: string | string[]; - /** Keys should be treated as numbers. */ - number: string | string[]; - /** i18n handler, defaults to util.format */ - __: (format: any, ...param: any[]) => string; - /** alias lookup table defaults */ - key: { [key: string]: any }; - } - - interface CoerceCallback { - (arg: any): any - } - - interface ConfigCallback { - (configPath: string): { [key: string]: any } - } - - interface Parser { - (args: string | any[], opts?: Partial): Arguments; - detailed(args: string | any[], opts?: Partial): DetailedArguments; - } -} - -declare var yargsParser: yargsParser.Parser -declare module 'yargs-parser' { - export = yargsParser; -} diff --git a/lib/usage.ts b/lib/usage.ts index a4204e49a..2d1856cf3 100644 --- a/lib/usage.ts +++ b/lib/usage.ts @@ -6,7 +6,7 @@ import * as path from 'path' import { YargsInstance } from './yargs' import { YError } from './yerror' import { Y18N } from 'y18n' -import { DetailedArguments } from 'yargs-parser' +import { DetailedArguments } from 'yargs-parser/build/lib/yargs-parser-types' import decamelize = require('decamelize') import setBlocking = require('set-blocking') import stringWidth = require('string-width') diff --git a/lib/validation.ts b/lib/validation.ts index 48f5ce841..34abde2e4 100644 --- a/lib/validation.ts +++ b/lib/validation.ts @@ -4,7 +4,7 @@ import { levenshtein as distance } from './levenshtein' import { objFilter } from './obj-filter' import { UsageInstance } from './usage' import { YargsInstance, Arguments } from './yargs' -import { DetailedArguments } from 'yargs-parser' +import { DetailedArguments } from 'yargs-parser/build/lib/yargs-parser-types' import { Y18N } from 'y18n' const specialKeys = ['$0', '--', '_'] @@ -131,8 +131,8 @@ export function validation (yargs: YargsInstance, usage: UsageInstance, y18n: Y1 if ((currentContext.commands.length > 0) || (commandKeys.length > 0) || isDefaultCommand) { argv._.slice(currentContext.commands.length).forEach((key) => { - if (commandKeys.indexOf(key) === -1) { - unknown.push(key) + if (commandKeys.indexOf('' + key) === -1) { + unknown.push('' + key) } }) } @@ -154,8 +154,8 @@ export function validation (yargs: YargsInstance, usage: UsageInstance, y18n: Y1 if ((currentContext.commands.length > 0) || (commandKeys.length > 0)) { argv._.slice(currentContext.commands.length).forEach((key) => { - if (commandKeys.indexOf(key) === -1) { - unknown.push(key) + if (commandKeys.indexOf('' + key) === -1) { + unknown.push('' + key) } }) } diff --git a/lib/yargs.ts b/lib/yargs.ts index 65dbd7c36..282df7b0b 100644 --- a/lib/yargs.ts +++ b/lib/yargs.ts @@ -7,7 +7,7 @@ import { Options as ParserOptions, ConfigCallback, CoerceCallback -} from 'yargs-parser' +} from 'yargs-parser/build/lib/yargs-parser-types' import { YError } from './yerror' import { UsageInstance, FailureFunction, usage as Usage } from './usage' import { argsert } from './argsert' @@ -1230,7 +1230,7 @@ export function Yargs (processArgs: string | string[] = [], cwd = process.cwd(), .concat(aliases[helpOpt] || []) .filter(k => k.length > 1) // check if help should trigger and strip it from _. - if (~helpCmds.indexOf(argv._[argv._.length - 1])) { + if (~helpCmds.indexOf('' + argv._[argv._.length - 1])) { argv._.pop() argv[helpOpt] = true } diff --git a/package.json b/package.json index 54453afbb..d565c939a 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "string-width": "^4.2.0", "which-module": "^2.0.0", "y18n": "^4.0.0", - "yargs-parser": "^18.1.2" + "yargs-parser": "^19.0.0-beta.1" }, "devDependencies": { "@types/chai": "^4.2.11", @@ -56,11 +56,11 @@ "yargs-test-extends": "^1.0.1" }, "scripts": { - "fix": "standardx --fix && standardx --fix **/*.ts", + "fix": "standardx --fix ./*.ts && standardx --fix **/*.ts", "posttest": "npm run check", "test": "c8 mocha --require ./test/before.js --timeout=12000 --check-leaks", "coverage": "c8 report --check-coverage", - "check": "standardx && standardx **/*.ts", + "check": "standardx ./*.ts && standardx **/*.ts", "compile": "rimraf build && tsc", "prepare": "npm run compile", "pretest": "npm run compile -- -p tsconfig.test.json" diff --git a/yargs.d.ts b/yargs.d.ts index 534cbff30..4e055e771 100644 --- a/yargs.d.ts +++ b/yargs.d.ts @@ -1,5 +1,31 @@ +/* +* MIT License +* +* Copyright (c) 2016 Martin Poelstra, Mizunashi Mana, Jeffery Grajkowski, +* Jeff Kenney, Jimi (Dimitris) Charalampidis, Steffen Viken ValvÄg, +* Emily Marigold Klassen, ExE Boss, and Aankhen. +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* SOFTWARE. +*/ + import { YargsInstance, RebaseFunction } from './build/lib/yargs' -import { Parser, DetailedArguments, Configuration } from 'yargs-parser'; +import { Parser, DetailedArguments, Configuration } from 'yargs-parser/build/lib/yargs-parser-types' declare namespace yargs { type BuilderCallback = ((args: YargsInstance) => PromiseLike>) | ((args: YargsInstance) => YargsInstance) | ((args: YargsInstance) => void); @@ -654,7 +680,7 @@ declare namespace yargs { skipValidation?: boolean; /** boolean, interpret option as a string, see `string()` */ string?: boolean; - type?: "array" | "count" | PositionalOptionsType; + type?: 'array' | 'count' | PositionalOptionsType; } interface PositionalOptions { @@ -702,7 +728,7 @@ declare namespace yargs { type InferredOptionType = O extends { default: infer D } ? D : - O extends { type: "count" } ? number : + O extends { type: 'count' } ? number : O extends { count: true } ? number : O extends { required: string | true } ? RequiredOptionType : O extends { require: string | true } ? RequiredOptionType : @@ -711,18 +737,18 @@ declare namespace yargs { RequiredOptionType | undefined; type RequiredOptionType = - O extends { type: "array", string: true } ? string[] : - O extends { type: "array", number: true } ? number[] : - O extends { type: "array", normalize: true } ? string[] : - O extends { type: "string", array: true } ? string[] : - O extends { type: "number", array: true } ? number[] : + O extends { type: 'array', string: true } ? string[] : + O extends { type: 'array', number: true } ? number[] : + O extends { type: 'array', normalize: true } ? string[] : + O extends { type: 'string', array: true } ? string[] : + O extends { type: 'number', array: true } ? number[] : O extends { string: true, array: true } ? string[] : O extends { number: true, array: true } ? number[] : O extends { normalize: true, array: true } ? string[] : - O extends { type: "array" } ? Array : - O extends { type: "boolean" } ? boolean : - O extends { type: "number" } ? number : - O extends { type: "string" } ? string : + O extends { type: 'array' } ? Array : + O extends { type: 'boolean' } ? boolean : + O extends { type: 'number' } ? number : + O extends { type: 'string' } ? string : O extends { array: true } ? Array : O extends { boolean: true } ? boolean : O extends { number: true } ? number : @@ -754,7 +780,7 @@ declare namespace yargs { type PromiseCompletionFunction = (current: string, argv: any) => Promise; type MiddlewareFunction = (args: Arguments) => void; type Choices = ReadonlyArray; - type PositionalOptionsType = "boolean" | "number" | "string"; + type PositionalOptionsType = 'boolean' | 'number' | 'string'; // Aliases for old @types/yargs names type Argv = YargsInstance;