What is command-line-usage?
The command-line-usage npm package is used to generate usage guides for command-line applications. It helps in creating structured and styled help text that can be displayed when users need assistance with the command-line interface (CLI) of your application.
What are command-line-usage's main functionalities?
Defining Sections
This feature allows you to define different sections of the usage guide, such as headers and options. The code sample demonstrates how to create a usage guide with a description and options.
const commandLineUsage = require('command-line-usage');
const sections = [
{
header: 'My App',
content: 'This is a description of my app.'
},
{
header: 'Options',
optionList: [
{
name: 'help',
typeLabel: '{underline boolean}',
description: 'Display this usage guide.'
},
{
name: 'src',
typeLabel: '{underline file}',
description: 'The input file to process.'
}
]
}
];
const usage = commandLineUsage(sections);
console.log(usage);
Customizing Option Descriptions
This feature allows you to customize the descriptions of the options in your CLI application. The code sample shows how to define options with aliases and types, along with their descriptions.
const commandLineUsage = require('command-line-usage');
const sections = [
{
header: 'Options',
optionList: [
{
name: 'verbose',
alias: 'v',
type: Boolean,
description: 'Enable verbose mode.'
},
{
name: 'timeout',
alias: 't',
type: Number,
description: 'Set the timeout value in ms.'
}
]
}
];
const usage = commandLineUsage(sections);
console.log(usage);
Adding Examples
This feature allows you to add examples to your usage guide, making it easier for users to understand how to use your CLI application. The code sample demonstrates how to add concise and detailed examples.
const commandLineUsage = require('command-line-usage');
const sections = [
{
header: 'Examples',
content: [
{
desc: '1. A concise example.',
example: '$ app -v'
},
{
desc: '2. A long example.',
example: '$ app --timeout 1000'
}
]
}
];
const usage = commandLineUsage(sections);
console.log(usage);
Other packages similar to command-line-usage
yargs
Yargs is a popular library for building command-line interfaces. It provides extensive functionality for parsing arguments, generating usage information, and handling commands. Compared to command-line-usage, yargs offers more comprehensive features for argument parsing and command handling.
commander
Commander is another widely-used library for building command-line interfaces. It allows you to define commands, options, and usage information. While it provides similar functionality to command-line-usage, it is more focused on command and option parsing rather than generating usage guides.
oclif
Oclif is a framework for building command-line tools. It provides a robust set of features for creating complex CLI applications, including argument parsing, command handling, and generating help text. Oclif is more comprehensive and suited for larger CLI projects compared to command-line-usage.
command-line-usage
Generates column-layout usage information for a command-line parser (e.g. command-line-args).
getUsage(definitions, options) ⇒ string
⏏
Kind: Exported function
Example
Some example usage output:
my-app
Generates something useful
Usage
$ cat input.json | my-app [<options>]
$ my-app <files>
Main options
This group contains the most important options.
-a, --one <string> The first option
-b, --two <number> The second option
Miscellaneous
-c, --three <string> The third option
-d, --four <number> The fourth option
Project home: https://github.com/me/my-app
..and the code to create it:
var getUsage = require("command-line-usage");
var optionDefinitions = [
{ name: "help", alias: "h", type: Boolean, description: "Display this usage guide.", group: "main" },
{ name: "files", alias: "f", type: String, multiple: true, defaultOption: true, description: "The input files to process", group: "main" },
{ name: "timeout", alias: "t", type: Number, description: "Timeout value in ms", group: "main" },
{ name: "custom", type: Custom, description: "A custom class instance"}
];
var options = {
title: "%bold{a typical app}",
description: "Generates something %yellow bg-black{wild and crazy}",
forms: [
"$ cat input.json | my-app [<options>]",
"$ my-app <files>"
],
groups: {
main: {
title: "Main options",
description: "This group contains the most important options."
},
_none: "No group"
},
footer: "Project home: https://github.com/me/my-app",
hide: [ "files" ]
};
var usage = getUsage(optionDefinitions, options);
UsageOptions ⏏
The class describes all valid options for the getUsage
function. Inline formatting can be used within any text string supplied using valid ansi-escape-sequences formatting syntax.
Kind: Exported class
options.title : string
| textObject
The title line at the top of the usage, typically the name of the app. By default it is underlined but this formatting can be overridden by passing a textObject.
Kind: instance property of UsageOptions
Example
{
title: {
text: "my-app",
format: [ "bold", "underline" ]
}
}
options.description : string
| textObject
A description to go underneath the title. For example, some words about what the app is for.
Kind: instance property of UsageOptions
options.usage : object
An array of strings highlighting the main usage forms of the app.
Kind: instance property of UsageOptions
Properties
Name | Type | Default |
---|
title | string | textObject | "Usage" |
format | string | Array.<string> | |
Example
{
usage: {
title: "Synopsis",
forms: [
"$ my-app <options> <files>",
"$ my-app [-cvh]"
]
}
}
options.groups : object
Specify which groups to display in the output by supplying an object of key/value pairs, where the key is the name of the group to include and the value is a string or textObject. If the value is a string it is used as the group title. Alternatively supply an object containing a title
and description
string.
Kind: instance property of UsageOptions
Example
{
main: {
title: "Main options",
description: "This group contains the most important options."
},
misc: "Miscellaneous"
}
Displayed at the foot of the usage output.
Kind: instance property of UsageOptions
options.hide : string
| Array.<string>
If you want to hide certain options from the output, specify their names here.
Kind: instance property of UsageOptions
options~textObject
Contains text and formatting information.
Kind: inner typedef of UsageOptions
Properties
Name | Type | Description |
---|
text | string | |
format | string | Array.<string> | one or more ansi style names from this list. |
© 2015 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.