cli-ux
cli IO utilities
Usage
The following assumes you have installed cli-ux
to your project with npm install cli-ux
or yarn add cli-ux
and have it required in your script (TypeScript example):
import cli from 'cli-ux'
cli.prompt('What is your name?')
JavaScript:
const {cli} = require('cli-ux')
cli.prompt('What is your name?')
cli.prompt()
Prompt for user input.
// just prompt for input
await cli.prompt('What is your name?')
// mask input after enter is pressed
await cli.prompt('What is your two-factor token?', {type: 'mask'})
// mask input on keypress (before enter is pressed)
await cli.prompt('What is your password?', {type: 'hide'})
// yes/no confirmation
await cli.confirm('Continue?')
// "press any key to continue"
await cli.anykey()
cli.url(text, uri)
Create a hyperlink (if supported in the terminal)
await cli.url('sometext', 'https://google.com')
// shows sometext as a hyperlink in supported terminals
// shows https://google.com in unsupported terminals
cli.open
Open a url in the browser
await cli.open('https://oclif.io')
cli.action
Shows a spinner
// start the spinner
cli.action.start('starting a process')
// show on stdout instead of stderr
cli.action.start('starting a process', {stdout: true})
// stop the spinner
cli.action.stop() // shows 'starting a process... done'
cli.action.stop('custom message') // shows 'starting a process... custom message'
This degrades gracefully when not connected to a TTY. It queues up any writes to stdout/stderr so they are displayed above the spinner.
cli.annotation
Shows an iterm annotation
// start the spinner
cli.annotation('sometest', 'annotated with this text')
cli.wait
Waits for 1 second or given milliseconds
await cli.wait()
await cli.wait(3000)
cli.table
Displays tabular data
cli.table(data, columns, options)
Where:
cli.table.flags
is an object containing all the flags to include in your command class.
{
columns: Flags.string({exclusive: ['additional'], description: 'only show provided columns (comma-seperated)'}),
sort: Flags.string({description: 'property to sort by (prepend '-' for descending)'}),
filter: Flags.string({description: 'filter property by partial string matching, ex: name=foo'}),
csv: Flags.boolean({exclusive: ['no-truncate'], description: 'output is csv format'}),
extra: Flags.boolean({char: 'x', description: 'show all columns'}),
'no-truncate': Flags.boolean({exclusive: ['csv'], description: 'do not truncate output to fit screen'}),
'no-header': Flags.boolean({exclusive: ['csv'], description: 'hide table header from output'}),
}
Table.Columns
defines the table columns and their display options.
const columns: Table.Columns = {
// where `.name` is a property of a data object
name: {}, // "Name" inferred as the column header
id: {
header: 'ID', // override column header
minWidth: '10', // column must display at this width or greater
extra: true, // only display this column when the --extra flag is present
get: row => `US-O1-${row.id}`, // custom getter for data row object
},
}
Table.Options
defines the table options, most of which are the parsed flags from the user for display customization, all of which are optional.
const options: Table.Options = {
printLine: myLogger, // custom logger
columns: flags.columns,
sort: flags.sort,
filter: flags.filter,
csv: flags.csv,
extra: flags.extra,
'no-truncate': flags['no-truncate]',
'no-header': flags['no-header]',
}
Example class:
import {Command} from '@oclif/command'
import {cli} from 'cli-ux'
import axios from 'axios'
export default class Users extends Command {
static flags = {
...cli.table.flags
}
async run() {
const {flags} = this.parse(Users)
const {data: users} = await axios.get('https://jsonplaceholder.typicode.com/users')
cli.table(users, {
name: {
minWidth: 7,
},
company: {
get: row => row.company && row.company.name
},
id: {
header: 'ID',
extra: true
}
}, {
printLine: this.log,
...flags, // parsed flags
})
}
}
Displays:
$ example-cli users
Name Team
Jordan Sales
Jamie Engineering
$ example-cli users --extra
Name Team ID
Jordan Sales 100
Jamie Engineering 200
$ example-cli users --columns=name
Name
Jordan
Jamie
$ example-cli users --filter="team=sales"
Name Team ID
Jordan Sales 100
$ example-cli users --sort=team
Name Team ID
Jamie Engineering 200
Jordan Sales 100
cli.tree
Generate a tree and display it
let tree = cli.tree()
tree.insert('foo')
tree.insert('bar')
let subtree = cli.tree()
subtree.insert('qux')
tree.nodes.bar.insert('baz', subtree)
tree.display()
Outputs:
├─ foo
└─ bar
└─ baz
└─ qux
Current Tags
-
6.0.9
...
latest
(3 years ago)
-
5.0.0-4
...
next
(6 years ago)
-
5.6.7
...
oclif-v1
(3 years ago)
167 Versions
-
6.0.9
[deprecated]
...
3 years ago
-
6.0.8
[deprecated]
...
3 years ago
-
6.0.7
[deprecated]
...
3 years ago
-
5.6.7
[deprecated]
...
3 years ago
-
6.0.6
[deprecated]
...
3 years ago
-
6.0.5
[deprecated]
...
3 years ago
-
5.6.6
[deprecated]
...
3 years ago
-
6.0.4
[deprecated]
...
3 years ago
-
6.0.3
[deprecated]
...
3 years ago
-
6.0.2
[deprecated]
...
3 years ago
-
5.6.5
[deprecated]
...
3 years ago
-
6.0.1
[deprecated]
...
3 years ago
-
5.6.4
[deprecated]
...
3 years ago
-
6.0.0
[deprecated]
...
3 years ago
-
5.6.3
[deprecated]
...
3 years ago
-
5.6.2
[deprecated]
...
4 years ago
-
5.5.1
[deprecated]
...
4 years ago
-
5.5.0
[deprecated]
...
4 years ago
-
5.4.10
[deprecated]
...
4 years ago
-
5.4.9
[deprecated]
...
4 years ago
-
5.4.8
[deprecated]
...
4 years ago
-
5.4.7
[deprecated]
...
4 years ago
-
5.4.6
[deprecated]
...
5 years ago
-
5.4.5
[deprecated]
...
5 years ago
-
5.4.4
[deprecated]
...
5 years ago
-
5.4.3
[deprecated]
...
5 years ago
-
5.4.2
[deprecated]
...
5 years ago
-
5.4.1
[deprecated]
...
5 years ago
-
5.4.0
[deprecated]
...
5 years ago
-
5.3.3
[deprecated]
...
5 years ago
-
5.3.2
[deprecated]
...
5 years ago
-
5.3.1
[deprecated]
...
5 years ago
-
5.3.0
[deprecated]
...
6 years ago
-
5.2.2
[deprecated]
...
6 years ago
-
5.2.1
[deprecated]
...
6 years ago
-
5.2.0
[deprecated]
...
6 years ago
-
5.1.0
[deprecated]
...
6 years ago
-
5.0.0
[deprecated]
...
6 years ago
-
5.0.0-4
[deprecated]
...
6 years ago
-
5.0.0-3
[deprecated]
...
6 years ago
-
5.0.0-2
[deprecated]
...
6 years ago
-
4.9.3
[deprecated]
...
6 years ago
-
4.9.2
[deprecated]
...
6 years ago
-
5.0.0-1
[deprecated]
...
6 years ago
-
4.9.1
[deprecated]
...
6 years ago
-
4.9.0
[deprecated]
...
6 years ago
-
5.0.0-0
[deprecated]
...
6 years ago
-
4.8.2
[deprecated]
...
6 years ago
-
4.8.1
[deprecated]
...
6 years ago
-
4.8.0
[deprecated]
...
6 years ago
-
4.7.3
[deprecated]
...
7 years ago
-
4.7.2
[deprecated]
...
7 years ago
-
4.7.1
[deprecated]
...
7 years ago
-
4.7.0
[deprecated]
...
7 years ago
-
4.6.3
[deprecated]
...
7 years ago
-
4.6.2
[deprecated]
...
7 years ago
-
4.6.1
[deprecated]
...
7 years ago
-
4.6.0
[deprecated]
...
7 years ago
-
4.5.1
[deprecated]
...
7 years ago
-
4.5.0
[deprecated]
...
7 years ago
-
4.4.1
[deprecated]
...
7 years ago
-
4.4.0
[deprecated]
...
7 years ago
-
4.3.1
[deprecated]
...
7 years ago
-
4.3.0
[deprecated]
...
7 years ago
-
4.2.3
[deprecated]
...
7 years ago
-
4.2.2
[deprecated]
...
7 years ago
-
4.2.1
[deprecated]
...
7 years ago
-
4.2.0
[deprecated]
...
7 years ago
-
4.1.0
[deprecated]
...
7 years ago
-
4.0.1
[deprecated]
...
7 years ago
-
4.0.0
[deprecated]
...
7 years ago
-
3.5.0
[deprecated]
...
7 years ago
-
3.4.1
[deprecated]
...
7 years ago
-
3.4.0
[deprecated]
...
7 years ago
-
3.3.31
[deprecated]
...
7 years ago
-
3.3.30
[deprecated]
...
7 years ago
-
3.3.29
[deprecated]
...
7 years ago
-
3.3.28
[deprecated]
...
7 years ago
-
3.3.27
[deprecated]
...
7 years ago
-
3.3.26
[deprecated]
...
7 years ago
-
3.3.25
[deprecated]
...
7 years ago
-
3.3.24
[deprecated]
...
7 years ago
-
3.3.23
[deprecated]
...
7 years ago
-
3.3.22
[deprecated]
...
7 years ago
-
3.3.21
[deprecated]
...
7 years ago
-
3.3.20
[deprecated]
...
7 years ago
-
3.3.19
[deprecated]
...
7 years ago
-
3.3.18
[deprecated]
...
7 years ago
-
3.3.17
[deprecated]
...
7 years ago
-
3.3.16
[deprecated]
...
7 years ago
-
3.3.15
[deprecated]
...
7 years ago
-
3.3.14
[deprecated]
...
7 years ago
-
3.3.13
[deprecated]
...
7 years ago
-
3.3.12
[deprecated]
...
7 years ago
-
3.3.11
[deprecated]
...
7 years ago
-
3.3.10
[deprecated]
...
7 years ago
-
3.3.9
[deprecated]
...
7 years ago
-
3.3.8
[deprecated]
...
7 years ago
-
3.3.7
[deprecated]
...
7 years ago
-
3.3.6
[deprecated]
...
7 years ago
-
3.3.5
[deprecated]
...
7 years ago
-
3.3.4
[deprecated]
...
7 years ago
-
3.3.3
[deprecated]
...
7 years ago
-
3.3.2
[deprecated]
...
7 years ago
-
3.3.1
[deprecated]
...
7 years ago
-
3.3.0
[deprecated]
...
7 years ago
-
3.2.2
[deprecated]
...
7 years ago
-
3.2.1
[deprecated]
...
7 years ago
-
3.2.0
[deprecated]
...
7 years ago
-
3.1.7
[deprecated]
...
7 years ago
-
3.1.6
[deprecated]
...
7 years ago
-
3.1.5
[deprecated]
...
7 years ago
-
3.1.4
[deprecated]
...
7 years ago
-
3.1.3
[deprecated]
...
7 years ago
-
3.1.2
[deprecated]
...
7 years ago
-
3.1.1
[deprecated]
...
7 years ago
-
3.1.0
[deprecated]
...
7 years ago
-
3.0.1
[deprecated]
...
7 years ago
-
3.0.0
[deprecated]
...
7 years ago
-
2.1.1
[deprecated]
...
7 years ago
-
2.1.0
[deprecated]
...
7 years ago
-
3.0.0-alpha.5
[deprecated]
...
7 years ago
-
3.0.0-alpha.4
[deprecated]
...
7 years ago
-
3.0.0-alpha.3
[deprecated]
...
7 years ago
-
3.0.0-alpha.2
[deprecated]
...
7 years ago
-
3.0.0-alpha.1
[deprecated]
...
7 years ago
-
3.0.0-alpha.0
[deprecated]
...
7 years ago
-
2.0.21
[deprecated]
...
7 years ago
-
2.0.20
[deprecated]
...
7 years ago
-
2.0.19
[deprecated]
...
7 years ago
-
2.0.18
[deprecated]
...
7 years ago
-
2.0.17
[deprecated]
...
7 years ago
-
2.0.16
[deprecated]
...
7 years ago
-
2.0.15
[deprecated]
...
7 years ago
-
2.0.14
[deprecated]
...
7 years ago
-
2.0.13
[deprecated]
...
7 years ago
-
2.0.12
[deprecated]
...
7 years ago
-
2.0.11
[deprecated]
...
7 years ago
-
2.0.10
[deprecated]
...
7 years ago
-
2.0.9
[deprecated]
...
7 years ago
-
2.0.8
[deprecated]
...
7 years ago
-
2.0.7
[deprecated]
...
7 years ago
-
2.0.6
[deprecated]
...
7 years ago
-
2.0.5
[deprecated]
...
7 years ago
-
2.0.4
[deprecated]
...
7 years ago
-
2.0.3
[deprecated]
...
7 years ago
-
2.0.2
[deprecated]
...
7 years ago
-
2.0.1
[deprecated]
...
7 years ago
-
2.0.0
[deprecated]
...
7 years ago
-
1.1.13
[deprecated]
...
7 years ago
-
1.1.12
[deprecated]
...
7 years ago
-
1.1.11
[deprecated]
...
7 years ago
-
1.1.10
[deprecated]
...
7 years ago
-
1.1.9
[deprecated]
...
7 years ago
-
1.1.8
[deprecated]
...
7 years ago
-
1.1.7
[deprecated]
...
7 years ago
-
1.1.6
[deprecated]
...
7 years ago
-
1.1.5
[deprecated]
...
7 years ago
-
1.1.4
[deprecated]
...
7 years ago
-
1.1.3
[deprecated]
...
7 years ago
-
1.1.2
[deprecated]
...
7 years ago
-
1.1.1
[deprecated]
...
7 years ago
-
1.1.0
[deprecated]
...
7 years ago
-
1.0.1
[deprecated]
...
7 years ago
-
1.0.0
[deprecated]
...
7 years ago
-
0.0.1
[deprecated]
...
7 years ago
-
0.0.0
[deprecated]
...
7 years ago