Socket
Socket
Sign inDemoInstall

command-line-usage

Package Overview
Dependencies
5
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

command-line-usage


Version published
Maintainers
1
Created

Package description

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

Readme

Source

view on npm npm module downloads Build Status Dependency Status js-standard-style

command-line-usage

A simple module for creating a usage guide.

Synopis

A usage guide is built from an arbitrary number of sections, e.g. a description section, synopsis, option list, examples, footer etc. Each section has a bold, underlined header and some content (a paragraph, table, option list, banner etc.)

The commandLineUsage() function takes one or more section objects (content or optionList) as input.

Inline ansi formatting can be used anywhere within section content using the formatting syntax described here.

This script:

const getUsage = require('command-line-usage')

const sections = [
  {
    header: 'A typical app',
    content: 'Generates something [italic]{very} important.'
  },
  {
    header: 'Options',
    optionList: [
      {
        name: 'input',
        typeLabel: '[underline]{file}',
        description: 'The input to process.'
      },
      {
        name: 'help',
        description: 'Print this usage guide.'
      }
    ]
  }
]
const usage = getUsage(sections)
console.log(usage)

Outputs this guide:

usage

Examples

Simple

A fairly typical usage guide with three sections - description, option list and footer. Code.

usage

Option List groups

Demonstrates breaking the option list up into groups. Code.

usage

Banners

A banner is created by adding the raw: true property to your content. This flag disables any formatting on the content, displaying it raw as supplied.

Header

Demonstrates a banner at the top. This example also adds a synopsis section. Code.

usage

Demonstrates a footer banner. Code.

usage

Examples section (table layout)

An examples section is added. To achieve this table layout, supply the content as an array of objects. The property names of each object are not important, so long as they are consistent throughout the array. Code.

usage

Command list

Useful if your app is command-driven, like git or npm. Code.

usage

Description section (table layout)

Demonstrates use of table layout in the description. In this case the second column (containing the hammer and sickle) has nowrap enabled, as the input is already formatted as desired. Code.

usage

API Reference

commandLineUsage(sections) ⇒ string

Generates a usage guide suitable for a command-line app.

Kind: Exported function

ParamTypeDescription
sectionsSection | Array.<Section>

One of more section objects (content or optionList).

commandLineUsage~content

A Content section comprises a header and one or more lines of content.

Kind: inner typedef of commandLineUsage
Properties

NameTypeDescription
headerstring

The section header, always bold and underlined.

contentstring | Array.<string> | Array.<object>

One or more lines of text. For table layout, supply the content as an array of objects. The property names of each object are not important, so long as they are consistent throughout the array.

rawboolean

Set to true to avoid indentation and wrapping. Useful for banners.

Example
Simple string of content. The syntax for ansi formatting is documented here.

{
  header: 'A typical app',
  content: 'Generates something [italic]{very} important.'
}

An array of strings is interpreted as lines, to be joined by the system newline character.

{
  header: 'A typical app',
  content: [
    'First line.',
    'Second line.'
  ]
}

An array of recordset-style objects are rendered in table layout.

{
  header: 'A typical app',
  content: [
    { colA: 'First row, first column.', colB: 'First row, second column.'},
    { colA: 'Second row, first column.', colB: 'Second row, second column.'}
  ]
}

commandLineUsage~optionList

A OptionList section adds a table displaying details of the available options.

Kind: inner typedef of commandLineUsage
Properties

NameTypeDescription
headerstring

The section header, always bold and underlined.

optionListArray.<OptionDefinition>

an array of option definition objects. In addition to the regular definition properties, command-line-usage will look for:

  • description - a string describing the option.
  • typeLabel - a string to replace the default type string (e.g. <string>). It's often more useful to set a more descriptive type label, like <ms>, <files>, <command> etc.
groupstring | Array.<string>

If specified, only options from this particular group will be printed. Example.

hidestring | Array.<string>

The names of one of more option definitions to hide from the option list. Example.

Example

{
  header: 'Options',
  optionList: [
    {
      name: 'help', alias: 'h', description: 'Display this usage guide.'
    },
    {
      name: 'src', description: 'The input files to process',
      multiple: true, defaultOption: true, typeLabel: '[underline]{file} ...'
    },
    {
      name: 'timeout', description: 'Timeout value in ms. This description is needlessly long unless you count testing of the description column maxWidth useful.',
      alias: 't', typeLabel: '[underline]{ms}'
    }
  ]
}

© 2015-16 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.

Keywords

FAQs

Last updated on 26 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc