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).
Synopsis
Where example/my-app.js
looks like this
module.exports = {
options: {
title: "my-app",
description: "Generates something useful",
forms: [
"$ cat input.json | my-app [<options>]",
"$ my-app <files>"
],
groups: {
main: {
title: "Main options",
description: "This group contains the most important options."
},
misc: "Miscellaneous"
},
footer: "Project home: https://github.com/me/my-app"
},
data: [
{ name: "one", alias: "a", type: String, group: "main",
description: "The first option"
},
{ name: "two", type: Number, alias: "b", group: "main",
description: "The second option"
},
{ name: "three", alias: "c", type: String, group: "misc",
description: "The third option"
},
{ name: "four", type: Number, alias: "d", group: "misc",
description: "The fourth option"
}
]
};
This command:
$ command-line-usage example/my-app.js
Outputs this:
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
command-line-usage
usage(cliOptions, options) ⇒ string
⏏
Kind: Exported function
Param | Type | Description |
---|
cliOptions | Array.<cliOption> | the CLI options |
options | object | Options |
[options.title] | string | |
[options.description] | string | |
[options.forms] | string | Array.<string> | |
[options.groups] | object | |
[options.footer] | string | |
[options.hide] | string | Array.<string> | |
© 2015 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.