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/dmd.js
looks like this
module.exports = {
options: {
title: "dmd",
header: "Generate markdown API documentation",
forms: [
"$ cat jsdoc-parse-output.json | dmd [<options>]"
],
footer: "Project home: https://github.com/jsdoc2md/dmd"
},
data: [
{ name: "template", alias: "t", type: String,
description: "A custom handlebars template file to insert documentation into. The default template is `{{>main}}`."
},
{ name: "heading-depth", type: Number, alias: "d",
description: "root heading depth, defaults to 2 (`##`)."
},
{ name: "plugin", type: Array,
description: "Use an installed package containing helper and/or partial overrides"
},
{ name: "helper", type: Array,
description: "handlebars helper files to override or extend the default set"
},
{ name: "partial", type: Array,
description: "handlebars partial files to override or extend the default set"
},
{ name: "example-lang", type: String, alias: "l",
description: "Specifies the default language used in @example blocks (for syntax-highlighting purposes). In gfm mode, each @example is wrapped in a fenced-code block. Example usage: `--example-lang js`. Use the special value `none` for no specific language. While using this option, you can override the supplied language for any @example by specifying the `@lang` subtag, e.g `@example @lang hbs`. Specifying `@example @lang off` will disable code blocks for that example."
},
{ name: "name-format", type: Boolean,
description: "Format identifier names as code"
},
{ name: "no-gfm", type: Boolean,
description: "By default, dmd generates github-flavoured markdown. Not all markdown parsers render gfm correctly. If your generated docs look incorrect on sites other than Github (e.g. npmjs.org) try enabling this option to disable Github-specific syntax. "
},
{ name: "separators", type: Boolean,
description: "Put <hr> breaks between identifiers. Improves readability on bulky docs. "
},
{ name: "module-index-format", type: String, alias: "m",
description: "none, grouped, table, dl"
},
{ name: "global-index-format", type: String, alias: "g",
description: "none, grouped, table, dl"
},
{ name: "param-list-format", type: String, alias: "p",
description: "Two options to render parameter lists: 'list' or 'table' (default). Table format works well in most cases but switch to list if things begin to look crowded / squashed. "
},
{ name: "property-list-format", type: String, alias: "r",
description: "list, table"
},
{ name: "member-index-format", type: String, alias: "c",
description: "grouped, list"
},
{ name: "group-by", type: Array,
description: "a list of fields to group member indexes by"
}
]
};
This command:
$ command-line-usage example/dmd.js
Outputs this:
dmd
Generate markdown API documentation
Usage
$ cat jsdoc-parse-output.json | dmd [<options>]
-t, --template <string> A custom handlebars template file to insert documentation into. The default template is `{{>main}}`.
-d, --heading-depth <number> root heading depth, defaults to 2 (`##`).
--plugin <array> Use an installed package containing helper and/or partial overrides
--helper <array> handlebars helper files to override or extend the default set
--partial <array> handlebars partial files to override or extend the default set
-l, --example-lang <string> Specifies the default language used in @example blocks (for syntax-highlighting purposes). In gfm
mode, each @example is wrapped in a fenced-code block. Example usage: `--example-lang js`. Use the
special value `none` for no specific language. While using this option, you can override the supplied
language for any @example by specifying the `@lang` subtag, e.g `@example @lang hbs`. Specifying
`@example @lang off` will disable code blocks for that example.
--name-format Format identifier names as code
--no-gfm By default, dmd generates github-flavoured markdown. Not all markdown parsers render gfm correctly. If
your generated docs look incorrect on sites other than Github (e.g. npmjs.org) try enabling this
option to disable Github-specific syntax.
--separators Put <hr> breaks between identifiers. Improves readability on bulky docs.
-m, --module-index-format <string> none, grouped, table, dl
-g, --global-index-format <string> none, grouped, table, dl
-p, --param-list-format <string> Two options to render parameter lists: 'list' or 'table' (default). Table format works well in most
cases but switch to list if things begin to look crowded / squashed.
-r, --property-list-format <string> list, table
-c, --member-index-format <string> grouped, list
--group-by <array> a list of fields to group member indexes by
Project home: https://github.com/jsdoc2md/dmd
© 2015 Lloyd Brookes <75pound@gmail.com>.