Introduction to CLIUI
CLIUI is a popular utility for building command-line interface (CLI) user experiences. It provides a rich set of APIs to create functional, user-friendly CLI tools. This guide covers a wide array of CLIUI APIs with code snippets and includes an application example.
Key APIs and Examples
UI
Creates a new user interface instance.
const cliui = require('cliui'); let ui = cliui();
div
Adds a new row to the UI.
ui.div('Welcome to CLIUI!'); ui.div({ text: 'Command options:', padding: [1, 0, 1, 2] });
span
Add inline elements to the UI.
ui.span( {text: 'Option 1:', width: 20}, {text: 'Description for option 1.'} );
resetOutput
Resets the UI output.
ui.resetOutput();
toString
Renders the UI to a string that can be displayed in the terminal.
console.log(ui.toString());
Full Application Example
Below is a full example that showcases several CLIUI APIs in a mini application:
const cliui = require('cliui'); let appUI = cliui(); appUI.div('CLIUI Application'); appUI.div({ text: 'Usage:', padding: [1, 0, 1, 2] }); appUI.span( { text: '--help', width: 20 }, { text: 'Display this help message' } ); appUI.span( { text: '--version', width: 20 }, { text: 'Display application version' } ); appUI.div(); console.log(appUI.toString());
In the example above, we created a small help utility for a CLI application using various CLIUI APIs such as div
and span
to build a structured and user-friendly output.
By mastering the CLIUI library, you can significantly enhance the usability of your command-line tools.
Hash: 99ee5f65016d7e895f202ce4ffcc79b8acc14c9c86d98e74f8f4252a45e76bd0