Giter VIP home page Giter VIP logo

minus-h's Introduction

minus-h

Add help generation to APIs created with Node's util.parseArgs() function.

Installation

npm install minus-h

API

parseArgsWithHelp() is a function that wraps util.parseArgs(), adding a "-h,--help" argument. It takes one or two arguments. The first argument is an augmented version of the options passed to util.parseArgs(), with descriptions provided for options as well as the command as a whole. The second optional argument is options for line wrapping. The wrapping width defaults to your terminal width (via process.stdout.columns).

import {parseArgsWithHelp} from 'minus-h'

parseArgsWithHelp({
  description: 'A command that does something very interesting',
  argumentName: 'files',
  argumentDescription: 'the files to be processed',
  allowPositionals: true,
  options: {
    encoding: {
      type: 'string',
      argumentName: 'encoding',
      description: 'encoding for files read or written',
      choices: ["utf8", "base64"]
    },
  },
}, { width: 80 })

node example.js --help outputs the following to stderr, before exiting with exit code 64:

Usage: example [options] [files]

A command that does something very interesting

Arguments:
  files                  the files to be processed

Options:
  --encoding <encoding>  encoding for files read or written (choices: "utf8",
                         "base64") Default: "utf8"
  -h,--help              display help for command

The usage() function takes the same two parameters. It always writes help information to output, then exits. It is useful if you've detected a higher level error condition with your input parameters, and want to re-iterate the usage information to users as if they had used --help.

Testing

Two more parameters may be added to the configuration parameter:

  • outputStream: a writable stream to write the help text to. Defaults to process.stderr. Only the write method is ever called.
  • exit(): a function that takes a number that is called when output is complete. Defaults to process.exit.

These options are useful during testing so that you can catch the help text that would have been written, and prevent your test harness from actually exiting.


Tests codecov

minus-h's People

Contributors

hildjj avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.