From e655a28f500954dcbc13c348cece6ecf120a54f1 Mon Sep 17 00:00:00 2001 From: Jb Landry Date: Fri, 6 Jul 2018 11:44:54 -0400 Subject: [PATCH] Replaced `acceptOnlyFlag` and `acceptOnlyFlags` by `validateFlags()` --- index.js | 77 ++++++++++++++++++++++++---------------------------- package.json | 3 +- readme.md | 29 ++++---------------- 3 files changed, 43 insertions(+), 66 deletions(-) diff --git a/index.js b/index.js index dd4b491..e554c33 100644 --- a/index.js +++ b/index.js @@ -3,16 +3,17 @@ //-------------------------------------------------------- 'use strict'; +const chalk = require('chalk'); +const glob = require('glob'); +const indentString = require('indent-string'); +const omelette = require('omelette'); const os = require('os'); +const ow = require('ow'); const path = require('path'); -const glob = require('glob'); const readPkgUp = require('read-pkg-up'); -const omelette = require('omelette'); -const chalk = require('chalk'); -const indentString = require('indent-string'); const stringWidth = require('string-width'); -const pad = require('@absolunet/terminal-pad'); const terminal = require('@absolunet/terminal'); +const pad = require('@absolunet/terminal-pad'); @@ -179,29 +180,33 @@ module.exports = class Cli { //-- Get task usage static getTaskUsage(task) { - const subs = !Array.isArray(STATIC.commands[task]); + if (task) { + const subs = !Array.isArray(STATIC.commands[task]); - let usage = `${chalk.underline('Usage:')}\n`; - if (subs) { + let usage = `${chalk.underline('Usage:')}\n`; + if (subs) { + + const length = (() => { + const lengths = []; + Object.values(STATIC.commands[task]).forEach((subtask) => { + const { call } = cmdDetails(`${task} ${subtask[0]}`); + lengths.push(stringWidth(call)); + }); + + return Math.max(...lengths); + })(); - const length = (() => { - const lengths = []; Object.values(STATIC.commands[task]).forEach((subtask) => { - const { call } = cmdDetails(`${task} ${subtask[0]}`); - lengths.push(stringWidth(call)); + usage += `${chalk.yellow(`${STATIC.pkg.name}`)} ${cmdUsage(`${task} ${subtask[0]}`, length, 3)}\n`; }); + } else { + usage += `${chalk.yellow(STATIC.pkg.name)} ${cmdUsage(task, 0, 2)}\n`; + } - return Math.max(...lengths); - })(); - - Object.values(STATIC.commands[task]).forEach((subtask) => { - usage += `${chalk.yellow(`${STATIC.pkg.name}`)} ${cmdUsage(`${task} ${subtask[0]}`, length, 3)}\n`; - }); - } else { - usage += `${chalk.yellow(STATIC.pkg.name)} ${cmdUsage(task, 0, 2)}\n`; + return indentString(usage, 2); } - return indentString(usage, 2); + return this.fullUsage; } @@ -245,37 +250,27 @@ module.exports = class Cli { } } - //-- Accept only this flag - static acceptOnlyFlag(meowCli, allowedFlag) { - const result = this.acceptOnlyFlags(meowCli, [allowedFlag]); - - if (typeof result === 'object') { - return result[allowedFlag] || false; - } - - return result; - } - //-- Accept only these flags - static acceptOnlyFlags(meowCli, allowedFlags) { - const inputFlags = Object.keys(meowCli.flags); + static validateFlags(meowCli, flagValidations) { + const inputFlags = Object.keys(meowCli.flags); + const allowedFlags = Object.keys(flagValidations); + + if (inputFlags.length === 0) { + return {}; - if (inputFlags.length <= allowedFlags.length) { + } else if (inputFlags.length <= allowedFlags.length) { const areFlagsValid = inputFlags.every((flag) => { - return allowedFlags.includes(flag); + return allowedFlags.includes(flag) && ow.isValid(meowCli.flags[flag], flagValidations[flag]); }); if (areFlagsValid) { return meowCli.flags; } - this.showTaskUsage(meowCli); - - } else if (Object.keys(meowCli.flags).length !== 0) { - this.showTaskUsage(meowCli); + return this.showTaskUsage(meowCli); } - return false; + return this.showTaskUsage(meowCli); } diff --git a/package.json b/package.json index 5aa6d2b..993a00a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@absolunet/cli", - "version": "0.6.0", + "version": "0.7.0", "description": "CLI utilities", "definition": "", "homepage": "https://github.com/absolunet/node-cli", @@ -32,6 +32,7 @@ "glob": "^7.1.2", "indent-string": "^3.2.0", "omelette": "^0.4.11", + "ow": "^0.6.0", "read-pkg-up": "^4.0.0", "string-width": "^2.1.1" } diff --git a/readme.md b/readme.md index 96157c1..a17bf0a 100644 --- a/readme.md +++ b/readme.md @@ -279,27 +279,8 @@ meow object
-### `acceptOnlyFlag(meowCli, flag)` -Show task usage and quit if CLI call has flags that are not whitelisted -Return flag value - -#### meowCli -*Required*
-Type: `object`
-meow object - -#### flag -*Required*
-Type: `string`
-Whitelisted flag - - - - -
- -### `acceptOnlyFlags(meowCli, flag)` -Show task usage and quit if CLI call has flags that are not whitelisted +### `validateFlags(meowCli, flag)` +Show task usage and quit if CLI call has flags that are not whitelisted and do not validate
Return `object` of flags values #### meowCli @@ -307,10 +288,10 @@ Return `object` of flags values Type: `object`
meow object -#### refuseFlagsAndArguments +#### flagValidations *Required*
-Type: `array` of `string`
-Whitelisted flags +Type: `object` of flag validators
+Whitelisted flags and their `ow` [predicate](https://github.com/sindresorhus/ow/#api)