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

Commit

Permalink
Added acceptOnlyFlags()
Browse files Browse the repository at this point in the history
  • Loading branch information
jblandry committed Jul 5, 2018
1 parent 95a95cc commit c03d389
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 24 deletions.
18 changes: 10 additions & 8 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
# http://editorconfig.org
# https://editorconfig.org

root = true

[*.js]
[*.{css,less,scss,js,html,jshtml,md}]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
indent_style = tab
[*.{json,rb,yaml,yml}]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
trim_trailing_whitespace = true
insert_final_newline = true

[{.editorconfig,.eslintrc.yaml,.gitignore,.npmignore,.sublimelinterrc,.travis.yml,package.json}]
indent_style = space
indent_size = 2
[{.editorconfig,.gitignore,.npmignore}]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
27 changes: 24 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,30 @@ module.exports = class Cli {
}

//-- Accept only this flag
static acceptOnlyFlag(meowCli, flag) {
if (Object.keys(meowCli.flags).length === 1 && meowCli.flags[flag]) {
return meowCli.flags[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);

if (inputFlags.length <= allowedFlags.length) {
const areFlagsValid = inputFlags.every((flag) => {
return allowedFlags.includes(flag);
});

if (areFlagsValid) {
return meowCli.flags;
}

this.showTaskUsage(meowCli);

} else if (Object.keys(meowCli.flags).length !== 0) {
this.showTaskUsage(meowCli);
Expand Down
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2011-2018 Absolunet, http://absolunet.com
Copyright (c) 2011-2018 Absolunet, https://absolunet.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@absolunet/cli",
"version": "0.5.1",
"version": "0.6.0",
"description": "CLI utilities",
"definition": "",
"homepage": "https://github.com/absolunet/node-cli",
"author": {
"name": "Absolunet",
"url": "http://absolunet.com/"
"url": "https://absolunet.com"
},
"license": "MIT",
"repository": {
Expand All @@ -17,22 +17,22 @@
"url": "https://github.com/absolunet/node-cli/issues"
},
"engines": {
"node": ">= 9.8.0"
"node": ">= 10.5.0"
},
"scripts": {
"test": "ava test"
},
"devDependencies": {
"@absolunet/tester": "1.4.0"
"@absolunet/tester": "1.7.1"
},
"dependencies": {
"@absolunet/terminal": "^0.4.4",
"@absolunet/terminal": "^0.5.0",
"@absolunet/terminal-pad": "^0.0.2",
"chalk": "^2.3.2",
"chalk": "^2.4.1",
"glob": "^7.1.2",
"indent-string": "^3.2.0",
"omelette": "^0.4.5",
"read-pkg-up": "^3.0.0",
"omelette": "^0.4.11",
"read-pkg-up": "^4.0.0",
"string-width": "^2.1.1"
}
}
29 changes: 25 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# @absolunet/cli

[![NPM version](https://img.shields.io/npm/v/@absolunet/cli.svg)](https://www.npmjs.com/package/@absolunet/cli)
[![Travis build](https://api.travis-ci.org/absolunet/node-cli.svg?branch=master)](https://travis-ci.org/absolunet/node-cli/builds)
[![Dependencies](https://david-dm.org/absolunet/node-cli/status.svg)](https://david-dm.org/absolunet/node-cli)
[![Dev dependencies](https://david-dm.org/absolunet/node-cli/dev-status.svg)](https://david-dm.org/absolunet/node-cli?type=dev)
[![npm](https://img.shields.io/npm/v/@absolunet/cli.svg)](https://www.npmjs.com/package/@absolunet/cli)
[![npm dependencies](https://david-dm.org/absolunet/node-cli/status.svg)](https://david-dm.org/absolunet/node-cli)
[![npms](https://badges.npms.io/%40absolunet%2Fcli.svg)](https://npms.io/search?q=%40absolunet%2Fcli)
[![Travis CI](https://api.travis-ci.org/absolunet/node-cli.svg?branch=master)](https://travis-ci.org/absolunet/node-cli/builds)
[![Code style ESLint](https://img.shields.io/badge/code_style-@absolunet/node-659d32.svg)](https://github.com/absolunet/eslint-config-node)

> CLI utilities
Expand Down Expand Up @@ -280,6 +281,7 @@ meow object

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

#### meowCli
*Required*<br>
Expand All @@ -294,6 +296,25 @@ Whitelisted flag



<br>

### `acceptOnlyFlags(meowCli, flag)`
Show task usage and quit if CLI call has flags that are not whitelisted
Return `object` of flags values

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

#### refuseFlagsAndArguments
*Required*<br>
Type: `array` of `string`<br>
Whitelisted flags




<br>

### `isRoot()`
Expand Down
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
const tester = require('@absolunet/tester');

tester.lintJs();
tester.lintJson();
tester.lintYaml();

0 comments on commit c03d389

Please sign in to comment.