Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
Replaced acceptOnlyFlag and acceptOnlyFlags by validateFlags()
Browse files Browse the repository at this point in the history
  • Loading branch information
jblandry committed Jul 6, 2018
1 parent c03d389 commit e655a28
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 66 deletions.
77 changes: 36 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');



Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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);
}


Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
}
Expand Down
29 changes: 5 additions & 24 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,38 +279,19 @@ meow object

<br>

### `acceptOnlyFlag(meowCli, flag)`
Show task usage and quit if CLI call has flags that are not whitelisted
Return flag value

#### meowCli
*Required*<br>
Type: `object`<br>
meow object

#### flag
*Required*<br>
Type: `string`<br>
Whitelisted flag




<br>

### `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<br>
Return `object` of flags values

#### meowCli
*Required*<br>
Type: `object`<br>
meow object

#### refuseFlagsAndArguments
#### flagValidations
*Required*<br>
Type: `array` of `string`<br>
Whitelisted flags
Type: `object` of flag validators<br>
Whitelisted flags and their `ow` [predicate](https://github.com/sindresorhus/ow/#api)



Expand Down

0 comments on commit e655a28

Please sign in to comment.