-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Filippo Conti
committed
Mar 25, 2019
0 parents
commit afc5cf7
Showing
11 changed files
with
5,680 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
language: node_js | ||
node_js: | ||
- "node" | ||
- "lts/*" | ||
after_script: | ||
- npm run report |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2019 b4dnewz <filippo@codekraft.it> | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# clito | ||
|
||
> Simple and well written command line applications helper | ||
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage percentage][coveralls-image]][coveralls-url] [![Project License][license-image]][license-url] | ||
|
||
## What's a clito? | ||
|
||
__Clito__, pronounced as in "clitoris", stands for __cli-tools__ and it's a well written command line application helper. | ||
It will become your best friend when dealing with cli applications, once you start play with it you will like it more than any other tool. :smirk: | ||
|
||
![southpark-chef](banner.jpg) | ||
|
||
Oops.. move along children, you are holding up the line, let see some features now.. | ||
|
||
## Features | ||
|
||
- Parses arguments respecting types | ||
- Boolean defaults to false | ||
- Support required options | ||
- Support multiple option arguments | ||
- Negates flags when using the `--no-` prefix | ||
- Outputs version when `--version` | ||
- Build help string from options when called with `--help` | ||
- Customizable help usage and command examples | ||
|
||
## Getting started | ||
|
||
Install the module using your favourite package manager: | ||
|
||
``` | ||
npm install clito | ||
``` | ||
|
||
Load it in your application code and set it up: | ||
|
||
```js | ||
#!/usr/bin/env node | ||
|
||
const clito = require('clito'); | ||
const cli = clito({ | ||
usage: 'askme <question>', | ||
flags: { | ||
person: { | ||
alias: 'p', | ||
default: 'chef' | ||
} | ||
} | ||
}) | ||
|
||
const {input, flags} = cli | ||
if (flags.person === 'chef') { | ||
console.log('> You gotta find the clitoris children.'); | ||
} | ||
``` | ||
|
||
Than run it with some input and options: | ||
|
||
``` | ||
$ node ./index.js "How do you make a girl love you more than other people?" | ||
> You gotta find the clitoris children. | ||
``` | ||
|
||
--- | ||
|
||
## License | ||
|
||
This package is under [MIT](LICENSE) license and its made with love by [Filippo Conti](https://b4dnewz.github.io/) | ||
|
||
|
||
[npm-image]: https://badge.fury.io/js/clito.svg | ||
|
||
[npm-url]: https://npmjs.org/package/clito | ||
|
||
[travis-image]: https://travis-ci.org/b4dnewz/clito.svg?branch=master | ||
|
||
[travis-url]: https://travis-ci.org/b4dnewz/clito | ||
|
||
[coveralls-image]: https://coveralls.io/repos/b4dnewz/clito/badge.svg | ||
|
||
[coveralls-url]: https://coveralls.io/r/b4dnewz/clito | ||
|
||
[license-image]: https://img.shields.io/badge/license-MIT-blue.svg | ||
|
||
[license-url]: https://github.com/b4dnewz/clito/blob/master/LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`clito --help support built in commands help 1`] = ` | ||
" clito v0.1.0 | ||
Simple and well written command line applications helper | ||
Usage: | ||
$ clito <input> | ||
Options: | ||
--foo, -f Prints bar | ||
--bar " | ||
`; | ||
exports[`clito --help support custom usage string 1`] = ` | ||
" clito v0.1.0 | ||
Simple and well written command line applications helper | ||
Usage: | ||
$ foo <input> | ||
Options: | ||
--foo a test option" | ||
`; | ||
exports[`clito --help support multiple custom usage examples 1`] = ` | ||
" clito v0.1.0 | ||
Simple and well written command line applications helper | ||
Usage: | ||
$ clito <input> | ||
Options: | ||
--foo a test option | ||
Examples: | ||
foo -foo | ||
foo --no-foo" | ||
`; | ||
exports[`clito --help support single custom usage example 1`] = ` | ||
" clito v0.1.0 | ||
Simple and well written command line applications helper | ||
Usage: | ||
$ clito <input> | ||
Options: | ||
--foo a test option | ||
Examples: | ||
foo -foo" | ||
`; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const yargs = require('yargs-parser'); | ||
const indent = require('indent-string'); | ||
const readPkgUp = require('read-pkg-up'); | ||
|
||
// Prevent caching of this module so module.parent is always accurate | ||
delete require.cache[__filename]; | ||
const parentDir = path.dirname(module.parent.filename); | ||
|
||
module.exports = function(options) { | ||
options = { | ||
pkg: readPkgUp.sync({ | ||
cwd: parentDir, | ||
normalize: false | ||
}).pkg || {}, | ||
argv: process.argv.slice(2), | ||
...options | ||
}; | ||
|
||
// Default option values expected from yargs-parser | ||
const parserDefaults = { | ||
alias: {}, | ||
array: [], | ||
boolean: [], | ||
default: {}, | ||
string: [], | ||
number: [], | ||
configuration: { | ||
'short-option-groups': true, | ||
'camel-case-expansion': true, | ||
'boolean-negation': true, | ||
'duplicate-arguments-array': false, | ||
'flatten-duplicate-arrays': true | ||
} | ||
}; | ||
|
||
// Prepare flags for reducing into parser options | ||
const flags = options.flags || {}; | ||
const flagsArr = Object.keys(flags).map(function(key) { | ||
return [key, flags[key]]; | ||
}); | ||
|
||
// Return application name and version | ||
const getVersion = function() { | ||
const {name, version} = options.pkg; | ||
const nameStr = (options.name || name) + ' '; | ||
const versionStr = `v${(options.version || version)}`; | ||
return nameStr + versionStr; | ||
}; | ||
|
||
// Print application name and version | ||
const showVersion = function() { | ||
// eslint-disable-next-line | ||
console.log(getVersion()); | ||
process.exit(); | ||
}; | ||
|
||
// Print command usage string and options help | ||
const showHelp = function() { | ||
const bannerStr = [ | ||
getVersion(), | ||
options.description || options.pkg.description | ||
].join('\n'); | ||
const pkgName = options.name || options.pkg.name; | ||
const usage = options.usage || `$ ${pkgName} <input>`; | ||
const usageStr = ['Usage:', indent(usage, 2)].join('\n'); | ||
const optsStr = flagsArr.map(f => { | ||
const [name, opts] = f; | ||
const {alias, description} = opts; | ||
let opt = [`--${name}`]; | ||
alias && opt.push(`-${alias}`); | ||
return opt.join(', ') + ' ' + (description || ''); | ||
}).join('\n'); | ||
const optOut = ['Options:', indent(optsStr, 2)].join('\n'); | ||
|
||
let out = [ | ||
bannerStr, | ||
usageStr, | ||
optOut | ||
]; | ||
|
||
if (options.examples) { | ||
const examplesStr = (Array.isArray(options.examples) ? | ||
options.examples : | ||
[options.examples]).map(s => indent(s, 2)).join('\n'); | ||
const exampleStr = [ | ||
'Examples:', | ||
examplesStr | ||
].join('\n'); | ||
out.push(exampleStr); | ||
} | ||
|
||
out = out.join('\n\n'); | ||
// eslint-disable-next-line | ||
console.log(indent(out, 2)); | ||
process.exit(); | ||
}; | ||
|
||
// Prepare parser options from user config | ||
const parserOpts = flagsArr.reduce(function(obj, flag) { | ||
const [flagName, flagOptions] = flag; | ||
const { | ||
type, | ||
alias, | ||
multiple, | ||
default: defaultValue, | ||
} = flagOptions; | ||
|
||
if (multiple) { | ||
obj.array.push({ | ||
key: flagName, | ||
[type]: true | ||
}); | ||
} else { | ||
obj[type].push(flagName); | ||
} | ||
|
||
if (alias) { | ||
obj.alias[flagName] = [alias]; | ||
} | ||
|
||
if (defaultValue || type === 'boolean') { | ||
obj.default[flagName] = defaultValue || false; | ||
} | ||
|
||
return obj; | ||
}, parserDefaults); | ||
|
||
// Parse and extract args | ||
const {_: input, ...args} = yargs(options.argv, parserOpts); | ||
|
||
// Print application version | ||
if (args.version) { | ||
showVersion(); | ||
} | ||
|
||
// If --help has been passed as flag | ||
if (args.help) { | ||
showHelp(); | ||
} | ||
|
||
// Check for required options | ||
flagsArr.forEach(function([flagName, flagOpts]) { | ||
if (flagOpts.required && typeof args[flagName] === 'undefined') { | ||
throw new Error(`Option "${flagName}" is required.`); | ||
} | ||
}); | ||
|
||
return { | ||
input, | ||
flags: args | ||
}; | ||
}; |
Oops, something went wrong.