Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/esatterwhite/node-seeli
Object orientated, event driven CLI module
https://github.com/esatterwhite/node-seeli
ansi command-line interactive javascript terminal
Last synced: about 2 months ago
JSON representation
Object orientated, event driven CLI module
- Host: GitHub
- URL: https://github.com/esatterwhite/node-seeli
- Owner: esatterwhite
- License: mit
- Created: 2014-07-06T18:05:21.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2023-12-31T01:39:16.000Z (12 months ago)
- Last Synced: 2024-10-24T05:51:58.217Z (about 2 months ago)
- Topics: ansi, command-line, interactive, javascript, terminal
- Language: JavaScript
- Homepage: http://esatterwhite.github.io/node-seeli/
- Size: 8.71 MB
- Stars: 18
- Watchers: 3
- Forks: 3
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-cli-frameworks - node-seeli
README
[![Test + Release](https://github.com/esatterwhite/node-seeli/actions/workflows/release.yml/badge.svg)](https://github.com/esatterwhite/node-seeli/actions/workflows/release.yml)
![package dependancies](https://david-dm.org/esatterwhite/node-seeli.png)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/47a935a723c94c73bc97d749836ee489)](https://www.codacy.com/app/esatterwhite/node-seeli?utm_source=github.com&utm_medium=referral&utm_content=esatterwhite/node-seeli&utm_campaign=Badge_Grade)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)seeli ( C. L. I. )
======================Object orientated, event driven , **Interactive** CLI module. Seeli aims to give you the tools to compose
A command line interface the way you want it, and otherwise, stays out of your way.![gif](https://raw.githubusercontent.com/esatterwhite/node-seeli/master/assets/seeli.gif "interactive mode")
```js
const os = require('os')
const cli = require('seeli')cli.config({
exitOnError: true
, color: 'green'
, name: 'example'
})const Hello = new cli.Command({
description:"displays a simple hello world command"
, name: 'hello'
, ui: 'dots'
, usage:[
`${cli.bold("Usage:")} ${cli.get('name')} hello --interactive`
, `${cli.bold("Usage:")} ${cli.get('name')} hello --name=john`
, `${cli.bold("Usage:")} ${cli.get('name')} hello --name=john --name=marry --name=paul -v screaming`
], flags:{
name:{
type:[ String, Array ]
, shorthand:'n'
, description:"The name of the person to say hello to"
, required:true
}, 'nested:value' : {
type: Number
, shorthand: 'nv'
, description: 'A newsted Value'
, name: 'nested'
}, excited: {
type:Boolean
, shorthand: 'e'
, description:"Say hello in a very excited manner"
, default:false
}, volume:{
type:String
, choices:['normal', 'screaming']
, description:"Will yell at each person"
, default:'normal'
, shorthand:'v'
}
}
, onContent: (content) => {
// command success
// content is the final output from run function
// non string content is not written to stdout automatically
// you could do it hereconsole.log(content.join(os.EOL))
}, run: async function( cmd, data ){
const out = [];
this.ui.start('processing names');
var names = Array.isArray( data.name ) ? data.name : [ data.name ];
for( var x = 0; x< names.length; x++ ){
this.ui.text = (`processing ${names[x]}`)
await new Promise((resolve) => {
setTimeout(() => {
let value = "Hello, " + names[x];
if( data.excited ){
value += '!';
}out.push( data.volume === 'screaming' ? value.toUpperCase() : value );
resolve(true);
}, 1000 * x + 1);
});
}this.ui.succeed('names processed successfully');
// anything returned from run
// is emitted from the `content` event
// strings will automatically be written to stdout
return out
}
});cli.use('hello', Hello)
cli.run();
```now you will have a fully functional `hello world` command with help and an interactive walk through
```
node ./cli help world
node ./cli world --help
node ./cli world --interactive
node ./cli world --name=Mark --name=Sally --no-excited
```* [API](#api)
* [Seeli.run( )](#seelirun-)
* [Seeli.list``](#seelilistarray)
* [Seeli.use( name ``, cmd `` )](#seeliuse-name-string-cmd-command-)
* [Seeli.bold( text ``)](#seelibold-text-string)
* [Seeli.green( text ``)](#seeligreen-text-string)
* [Seeli.blue( text ``)](#seeliblue-text-string)
* [Seeli.red( text ``)](#seelired-text-string)
* [Seeli.yellow( text ``)](#seeliyellow-text-string)
* [Seeli.cyan( text ``)](#seelicyan-text-string)
* [Seeli.magenta( text ``)](#seelimagenta-text-string)
* [Seeli.redBright( text ``)](#seeliredbright-text-string)
* [Seeli.blueBright( text ``)](#seelibluebright-text-string)
* [Seeli.greenBright( text ``)](#seeligreenbright-text-string)
* [Seeli.yellowBright( text ``)](#seeliyellowbright-text-string)
* [Seeli.cyanBright( text ``)](#seelicyanbright-text-string)
* [Seeli.config( key ``, value `` )](#seeliconfig-key-string-value-object-)
* [Seeli.config( opts `` )](#seeliconfig-opts-object-)
* [Seeli.config( key `` )](#seeliconfig-key-string-)
* [Supported Confgurations](#supported-confgurations)
* [Package Configuration](#package-configuration)
* [Command( options `` )](#command-options-object-)
* [Options](#options)
* [Flag Options](#flag-options)
* [Nested Flags](#nested-flags)
* [Auto Help](#auto-help)
* [Asyncronous](#asyncronous)
* [Progress](#progress)
* [Events](#events)# API
## Seeli.run( )
Executes The command line interface
## Seeli.list``
List of all top level registered commands
## Seeli.use( name ``, cmd `` )
Registers a new command under the specified name where the name will invoke the associated command
```js
var cli = require('seeli')
var Cmd = new cli.Command();cli.use('test', Cmd )
cli.run()
```## Seeli.bold( text ``)
wraps text in the ansi code for bold
## Seeli.green( text ``)
wraps text in the ansi code for green
## Seeli.blue( text ``)
wraps text in the ansi code for blue
## Seeli.red( text ``)
wraps text in the ansi code for red## Seeli.yellow( text ``)
wraps text in the ansi code for yellow
## Seeli.cyan( text ``)
wraps text in the ansi code for cyan
## Seeli.magenta( text ``)
wraps text in the ansi code for magenta
## Seeli.redBright( text ``)
wraps text in the ansi code for redBright
## Seeli.blueBright( text ``)
wraps text in the ansi code for blueBright
## Seeli.greenBright( text ``)
wraps text in the ansi code for greenBright
## Seeli.yellowBright( text ``)
wraps text in the ansi code for yellowBright
## Seeli.cyanBright( text ``)
wraps text in the ansi code for cyanBright
## Seeli.config( key ``, value `` )
sets a configuration value.
## Seeli.config( opts `` )
set multiple configuration values using a single object
## Seeli.config( key `` )
A config value to look up. Can be a dot separated key to look up nested values
#### Supported Confgurations
* color `` - The chalk color to use when outputting help text. default `green`
* name `` - the name of the command that is used in generated help
* exitOnError `` - Seeli will forcefully exit the current process when an error is encountered. default `false`
* exitOnContent `` - Seeli will forefully exit the current process when it is passed output content from a command. default `true`
* plugins `|[]` - A list of plugins to load and execute. A plugin may be either a function, or a module id to be required.
If it is a module id, the module must export a single function which will be passed the seeli instance when called.
* help `` - a file path or module name to a custom help command. This will be passed to `require` and must export a single command instance
* `seeli.config('help', '/path/to/help/command')`##### Package Configuration
Alternatively, initial configuration may be provided via `package.json` in a top-level key - `seeli`
```json5
// package.json{
"seeli": {
"color": "blue",
"name": "whizbang",
"plugins": [
"@myscope/simple-command"
]
}
}
```## Command( options `` )
## Options
name | type | default | description
-----|:-----:|--------|-------------
**description** | `String` | `""` | Used to render help output
**strict** | `Boolean` | `false` | When true, commands will error when the receive unknown flags
**args** | `Array` | `null` | if supplied, `agrs` will be used instead of `process.argv`
**interactive** | `Boolean` | `true` | If set to false, the flag will be excluded from the interactive prompts
**usage** | `String` / `Array` | `""` | A string or array of strings used to generate help text
**flags** | `Object` | `{}` | key value pairs used to control the command where keys are the name of the flag and the values is a configuration object for the flag
**ui** | `String` | `dots` | The kind of [progress indicator](https://github.com/sindresorhus/cli-spinners/blob/master/spinners.json) your command should use
**run** | `Function` | `no-op` | An async function used as the body of the command. It will be passed a `subcommand` name if one was passed, and a `data` object containing the processed values from the command input.
**commands** | `Command[]` | `[]` | A list of additional command to utilize as sub commands.### Flag Options
name | required | type | description
-----|:--------:|:----:|------------
**type** | `true` | `string` |The type of input that is expected. Boolean types to not expect input. The present of the flag **implies** `true`. Additionally, boolean flags allow for `--no-` to enforce `false`. If you want to accept multiple **values**, you specify type as an array with the first value being the type you which to accept. For example `[String, Array ]**`** means you will accept multiple string values.|
**description** | `false` | `string` | a description of the flag in question. |
**required** | `false` | `boolean` | If set to `true` a `RequiredFieldError` will be emitted |
**shorthand** | `false` | `string` | An options short hand flag that will be expanded out to the long hand flag. |
**interacive** | `false` | `boolean` | If set to false the flag will omitted from interactive prompts
**default** | `false` | `mixed` | A value to return if the flag is omitted. |
**mask** | `false` | `boolean` | **interactive mode only** Sets the input type to masked input to hide values
**choices** | `false` | `array` | Used only during an interactive command. Restricts the users options only to the options **specified** |
**multi** | `false` | `boolean` | **interactive mode only** If choices is specified, and multi is true, this user will be presented a multi checkbox UI allowing them to pick multiple values. The return value will be an array
**skip** | `false` | `boolean` | **interactive mode only** - if set to `true` this flag will be omitted from the interactive command prompts |
**event** | `false` | `boolean` | if set to `true` the command will emit an event withe the same name as the flag with **the** value that was captured for that flag |
**when** | `false` | `function` | **interactive mode only** Receives the current user answers hash and should return true or **false** depending on whether or not this question should be asked.
**validate** | `false` | `function` | receives user input and should return true if the value is **valid**, and an error message (String) otherwise. If false is returned, a default error message is provided.
**filter** | `false` | `function` | Receives the user input and return the filtered value to be used **inside** the program. The value returned will be added to the Answers hash.#### Nested Flags
Flag names that contain a colon (`:`) will be parsed as a nested value in the data that is return to you commands. You can Set arbitrarily deep values.
You can use this to automatically construct complex object. Array values are limited to primitive types```javascript
// cli --foo:bar:foobar=hello --foo:bar:baz=world --nested:array=1 --nested:array=2{
foo: {
bar: {
foobar: "hello"
, baz: "world"
}
}
, nested: {
array: [1, 2]
}
}
```## Auto Help
Seeli will generate help from the usage string and flags. You can help as a command `seeli help ` or as a flag `seeli --help`
## Asyncronous
Your defined `run` function can be an async function, or a function that returns a `Promise`. This allows you to do complex async operations and I/O. If an error is thrown, it will be displayed.
Otherwise, the content returned from your `run` function will be output to stdout ( if it returned a `String`).## Progress
Your command's `run` function has access to an instance of [ora](https://www.npmjs.com/package/ora) allowing you to display progress indicators and helpful messages while you perform other work.
## Events
Instances of the seeli Command or Commands that inherit from it as also instances of the `EventEmitter` class. By default any flag that has its `event` option set to `true` will emit an event with the value of the flag before the run function is executed.
```js
var EventCommand = new cli.Command({
args:[ '--one', '--no-two']
, flags:{
one:{
type:Boolean
, event:true
}
, two:{
type:Boolean
, event:true
}
}
, run: async function( cmd, data ){
return data.one && data.two
}
});EventCommand.on('one', function( value ){
assert.equal( true, value );
});EventCommand.on('two', function( value ){
assert.equal( false, value )
});EventCommand.on('content', function( value ){
assert.equal( false, value );
});EventCommand.run( null );
```