yargs 15.3.1 → 15.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/LICENSE +2 -3
  3. package/README.md +5 -5
  4. package/build/lib/apply-extends.d.ts +2 -0
  5. package/build/lib/apply-extends.js +65 -0
  6. package/build/lib/argsert.d.ts +2 -0
  7. package/build/lib/argsert.js +65 -0
  8. package/build/lib/command.d.ts +64 -0
  9. package/build/lib/command.js +416 -0
  10. package/build/lib/common-types.d.ts +36 -0
  11. package/build/lib/common-types.js +25 -0
  12. package/build/lib/completion-templates.d.ts +2 -0
  13. package/{lib → build/lib}/completion-templates.js +6 -5
  14. package/build/lib/completion.d.ts +21 -0
  15. package/build/lib/completion.js +135 -0
  16. package/build/lib/is-promise.d.ts +1 -0
  17. package/build/lib/is-promise.js +9 -0
  18. package/build/lib/levenshtein.d.ts +1 -0
  19. package/{lib → build/lib}/levenshtein.js +33 -33
  20. package/build/lib/middleware.d.ts +10 -0
  21. package/build/lib/middleware.js +57 -0
  22. package/build/lib/obj-filter.d.ts +1 -0
  23. package/build/lib/obj-filter.js +14 -0
  24. package/build/lib/parse-command.d.ts +11 -0
  25. package/build/lib/parse-command.js +36 -0
  26. package/build/lib/process-argv.d.ts +2 -0
  27. package/build/lib/process-argv.js +31 -0
  28. package/build/lib/usage.d.ts +49 -0
  29. package/build/lib/usage.js +540 -0
  30. package/build/lib/validation.d.ts +34 -0
  31. package/build/lib/validation.js +330 -0
  32. package/build/lib/yargs.d.ts +274 -0
  33. package/build/lib/yargs.js +1190 -0
  34. package/build/lib/yerror.d.ts +4 -0
  35. package/build/lib/yerror.js +11 -0
  36. package/index.js +1 -1
  37. package/locales/ja.json +6 -4
  38. package/package.json +26 -13
  39. package/yargs.js +2 -1291
  40. package/lib/apply-extends.js +0 -67
  41. package/lib/argsert.js +0 -68
  42. package/lib/command.js +0 -462
  43. package/lib/completion.js +0 -132
  44. package/lib/is-promise.js +0 -3
  45. package/lib/middleware.js +0 -64
  46. package/lib/obj-filter.js +0 -11
  47. package/lib/process-argv.js +0 -34
  48. package/lib/usage.js +0 -571
  49. package/lib/validation.js +0 -394
  50. package/lib/yerror.js +0 -11
@@ -0,0 +1 @@
1
+ export declare function levenshtein(a: string, b: string): number;
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  /*
2
3
  Copyright (c) 2011 Andrei Mackenzie
3
4
 
@@ -18,41 +19,40 @@ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18
19
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19
20
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21
  */
21
-
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.levenshtein = void 0;
22
24
  // levenshtein distance algorithm, pulled from Andrei Mackenzie's MIT licensed.
23
25
  // gist, which can be found here: https://gist.github.com/andrei-m/982927
24
- 'use strict'
25
26
  // Compute the edit distance between the two given strings
26
- module.exports = function levenshtein (a, b) {
27
- if (a.length === 0) return b.length
28
- if (b.length === 0) return a.length
29
-
30
- const matrix = []
31
-
32
- // increment along the first column of each row
33
- let i
34
- for (i = 0; i <= b.length; i++) {
35
- matrix[i] = [i]
36
- }
37
-
38
- // increment each column in the first row
39
- let j
40
- for (j = 0; j <= a.length; j++) {
41
- matrix[0][j] = j
42
- }
43
-
44
- // Fill in the rest of the matrix
45
- for (i = 1; i <= b.length; i++) {
46
- for (j = 1; j <= a.length; j++) {
47
- if (b.charAt(i - 1) === a.charAt(j - 1)) {
48
- matrix[i][j] = matrix[i - 1][j - 1]
49
- } else {
50
- matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
51
- Math.min(matrix[i][j - 1] + 1, // insertion
52
- matrix[i - 1][j] + 1)) // deletion
53
- }
27
+ function levenshtein(a, b) {
28
+ if (a.length === 0)
29
+ return b.length;
30
+ if (b.length === 0)
31
+ return a.length;
32
+ const matrix = [];
33
+ // increment along the first column of each row
34
+ let i;
35
+ for (i = 0; i <= b.length; i++) {
36
+ matrix[i] = [i];
54
37
  }
55
- }
56
-
57
- return matrix[b.length][a.length]
38
+ // increment each column in the first row
39
+ let j;
40
+ for (j = 0; j <= a.length; j++) {
41
+ matrix[0][j] = j;
42
+ }
43
+ // Fill in the rest of the matrix
44
+ for (i = 1; i <= b.length; i++) {
45
+ for (j = 1; j <= a.length; j++) {
46
+ if (b.charAt(i - 1) === a.charAt(j - 1)) {
47
+ matrix[i][j] = matrix[i - 1][j - 1];
48
+ }
49
+ else {
50
+ matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
51
+ Math.min(matrix[i][j - 1] + 1, // insertion
52
+ matrix[i - 1][j] + 1)); // deletion
53
+ }
54
+ }
55
+ }
56
+ return matrix[b.length][a.length];
58
57
  }
58
+ exports.levenshtein = levenshtein;
@@ -0,0 +1,10 @@
1
+ import { YargsInstance, Arguments } from './yargs';
2
+ export declare function globalMiddlewareFactory<T>(globalMiddleware: Middleware[], context: T): (callback: MiddlewareCallback | MiddlewareCallback[], applyBeforeValidation?: boolean) => T;
3
+ export declare function commandMiddlewareFactory(commandMiddleware?: MiddlewareCallback[]): Middleware[];
4
+ export declare function applyMiddleware(argv: Arguments | Promise<Arguments>, yargs: YargsInstance, middlewares: Middleware[], beforeValidation: boolean): Arguments | Promise<Arguments>;
5
+ export interface MiddlewareCallback {
6
+ (argv: Arguments, yargs: YargsInstance): Partial<Arguments> | Promise<Partial<Arguments>>;
7
+ }
8
+ export interface Middleware extends MiddlewareCallback {
9
+ applyBeforeValidation: boolean;
10
+ }
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.applyMiddleware = exports.commandMiddlewareFactory = exports.globalMiddlewareFactory = void 0;
4
+ const argsert_1 = require("./argsert");
5
+ const is_promise_1 = require("./is-promise");
6
+ function globalMiddlewareFactory(globalMiddleware, context) {
7
+ return function (callback, applyBeforeValidation = false) {
8
+ argsert_1.argsert('<array|function> [boolean]', [callback, applyBeforeValidation], arguments.length);
9
+ if (Array.isArray(callback)) {
10
+ for (let i = 0; i < callback.length; i++) {
11
+ if (typeof callback[i] !== 'function') {
12
+ throw Error('middleware must be a function');
13
+ }
14
+ callback[i].applyBeforeValidation = applyBeforeValidation;
15
+ }
16
+ Array.prototype.push.apply(globalMiddleware, callback);
17
+ }
18
+ else if (typeof callback === 'function') {
19
+ callback.applyBeforeValidation = applyBeforeValidation;
20
+ globalMiddleware.push(callback);
21
+ }
22
+ return context;
23
+ };
24
+ }
25
+ exports.globalMiddlewareFactory = globalMiddlewareFactory;
26
+ function commandMiddlewareFactory(commandMiddleware) {
27
+ if (!commandMiddleware)
28
+ return [];
29
+ return commandMiddleware.map(middleware => {
30
+ middleware.applyBeforeValidation = false;
31
+ return middleware;
32
+ });
33
+ }
34
+ exports.commandMiddlewareFactory = commandMiddlewareFactory;
35
+ function applyMiddleware(argv, yargs, middlewares, beforeValidation) {
36
+ const beforeValidationError = new Error('middleware cannot return a promise when applyBeforeValidation is true');
37
+ return middlewares
38
+ .reduce((acc, middleware) => {
39
+ if (middleware.applyBeforeValidation !== beforeValidation) {
40
+ return acc;
41
+ }
42
+ if (is_promise_1.isPromise(acc)) {
43
+ return acc
44
+ .then(initialObj => Promise.all([initialObj, middleware(initialObj, yargs)]))
45
+ .then(([initialObj, middlewareObj]) => Object.assign(initialObj, middlewareObj));
46
+ }
47
+ else {
48
+ const result = middleware(acc, yargs);
49
+ if (beforeValidation && is_promise_1.isPromise(result))
50
+ throw beforeValidationError;
51
+ return is_promise_1.isPromise(result)
52
+ ? result.then(middlewareObj => Object.assign(acc, middlewareObj))
53
+ : Object.assign(acc, result);
54
+ }
55
+ }, argv);
56
+ }
57
+ exports.applyMiddleware = applyMiddleware;
@@ -0,0 +1 @@
1
+ export declare function objFilter<T extends object>(original?: T, filter?: (k: keyof T, v: T[keyof T]) => boolean): T;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.objFilter = void 0;
4
+ const common_types_1 = require("./common-types");
5
+ function objFilter(original = {}, filter = () => true) {
6
+ const obj = {};
7
+ common_types_1.objectKeys(original).forEach((key) => {
8
+ if (filter(key, original[key])) {
9
+ obj[key] = original[key];
10
+ }
11
+ });
12
+ return obj;
13
+ }
14
+ exports.objFilter = objFilter;
@@ -0,0 +1,11 @@
1
+ import { NotEmptyArray } from './common-types';
2
+ export declare function parseCommand(cmd: string): ParsedCommand;
3
+ export interface ParsedCommand {
4
+ cmd: string;
5
+ demanded: Positional[];
6
+ optional: Positional[];
7
+ }
8
+ export interface Positional {
9
+ cmd: NotEmptyArray<string>;
10
+ variadic: boolean;
11
+ }
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseCommand = void 0;
4
+ function parseCommand(cmd) {
5
+ const extraSpacesStrippedCommand = cmd.replace(/\s{2,}/g, ' ');
6
+ const splitCommand = extraSpacesStrippedCommand.split(/\s+(?![^[]*]|[^<]*>)/);
7
+ const bregex = /\.*[\][<>]/g;
8
+ const firstCommand = splitCommand.shift();
9
+ if (!firstCommand)
10
+ throw new Error(`No command found in: ${cmd}`);
11
+ const parsedCommand = {
12
+ cmd: firstCommand.replace(bregex, ''),
13
+ demanded: [],
14
+ optional: []
15
+ };
16
+ splitCommand.forEach((cmd, i) => {
17
+ let variadic = false;
18
+ cmd = cmd.replace(/\s/g, '');
19
+ if (/\.+[\]>]/.test(cmd) && i === splitCommand.length - 1)
20
+ variadic = true;
21
+ if (/^\[/.test(cmd)) {
22
+ parsedCommand.optional.push({
23
+ cmd: cmd.replace(bregex, '').split('|'),
24
+ variadic
25
+ });
26
+ }
27
+ else {
28
+ parsedCommand.demanded.push({
29
+ cmd: cmd.replace(bregex, '').split('|'),
30
+ variadic
31
+ });
32
+ }
33
+ });
34
+ return parsedCommand;
35
+ }
36
+ exports.parseCommand = parseCommand;
@@ -0,0 +1,2 @@
1
+ export declare function getProcessArgvWithoutBin(): string[];
2
+ export declare function getProcessArgvBin(): string;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getProcessArgvBin = exports.getProcessArgvWithoutBin = void 0;
4
+ function getProcessArgvBinIndex() {
5
+ // The binary name is the first command line argument for:
6
+ // - bundled Electron apps: bin argv1 argv2 ... argvn
7
+ if (isBundledElectronApp())
8
+ return 0;
9
+ // or the second one (default) for:
10
+ // - standard node apps: node bin.js argv1 argv2 ... argvn
11
+ // - unbundled Electron apps: electron bin.js argv1 arg2 ... argvn
12
+ return 1;
13
+ }
14
+ function isBundledElectronApp() {
15
+ // process.defaultApp is either set by electron in an electron unbundled app, or undefined
16
+ // see https://github.com/electron/electron/blob/master/docs/api/process.md#processdefaultapp-readonly
17
+ return isElectronApp() && !process.defaultApp;
18
+ }
19
+ function isElectronApp() {
20
+ // process.versions.electron is either set by electron, or undefined
21
+ // see https://github.com/electron/electron/blob/master/docs/api/process.md#processversionselectron-readonly
22
+ return !!process.versions.electron;
23
+ }
24
+ function getProcessArgvWithoutBin() {
25
+ return process.argv.slice(getProcessArgvBinIndex() + 1);
26
+ }
27
+ exports.getProcessArgvWithoutBin = getProcessArgvWithoutBin;
28
+ function getProcessArgvBin() {
29
+ return process.argv[getProcessArgvBinIndex()];
30
+ }
31
+ exports.getProcessArgvBin = getProcessArgvBin;
@@ -0,0 +1,49 @@
1
+ import { Dictionary } from './common-types';
2
+ import { YargsInstance } from './yargs';
3
+ import { YError } from './yerror';
4
+ import { Y18N } from 'y18n';
5
+ export declare function usage(yargs: YargsInstance, y18n: Y18N): UsageInstance;
6
+ /** Instance of the usage module. */
7
+ export interface UsageInstance {
8
+ cacheHelpMessage(): void;
9
+ clearCachedHelpMessage(): void;
10
+ command(cmd: string, description: string | undefined, isDefault: boolean, aliases: string[], deprecated?: boolean): void;
11
+ deferY18nLookup(str: string): string;
12
+ describe(keys: string | string[] | Dictionary<string>, desc?: string): void;
13
+ epilog(msg: string): void;
14
+ example(cmd: string, description?: string): void;
15
+ fail(msg?: string | null, err?: YError | string): void;
16
+ failFn(f: FailureFunction): void;
17
+ freeze(): void;
18
+ functionDescription(fn: {
19
+ name?: string;
20
+ }): string;
21
+ getCommands(): [string, string, boolean, string[], boolean][];
22
+ getDescriptions(): Dictionary<string | undefined>;
23
+ getPositionalGroupName(): string;
24
+ getUsage(): [string, string][];
25
+ getUsageDisabled(): boolean;
26
+ help(): string;
27
+ reset(localLookup: Dictionary<boolean>): UsageInstance;
28
+ showHelp(level: 'error' | 'log' | ((message: string) => void)): void;
29
+ showHelpOnFail(enabled?: boolean | string, message?: string): UsageInstance;
30
+ showVersion(): void;
31
+ stringifiedValues(values?: any[], separator?: string): string;
32
+ unfreeze(): void;
33
+ usage(msg: string | null, description?: string | false): UsageInstance;
34
+ version(ver: any): void;
35
+ wrap(cols: number | null | undefined): void;
36
+ }
37
+ export interface FailureFunction {
38
+ (msg: string | undefined | null, err: YError | string | undefined, usage: UsageInstance): void;
39
+ }
40
+ export interface FrozenUsageInstance {
41
+ failMessage: string | undefined | null;
42
+ failureOutput: boolean;
43
+ usages: [string, string][];
44
+ usageDisabled: boolean;
45
+ epilogs: string[];
46
+ examples: [string, string][];
47
+ commands: [string, string, boolean, string[], boolean][];
48
+ descriptions: Dictionary<string | undefined>;
49
+ }