Git Product home page Git Product logo

cliffold's Introduction

Cliffold - Node CLI scaffolding

A little known fact about CLIs is that they are a pain in the ass to write.

Cliffold provides some utilities that are useful when writing CLIs. It uses posix-argv-parser for POSIX compliant ARGV parsing and provides utilities for managing loggers (to either STDOUT or a file), pid files and more.

var cliffold = require("cliffold");
var cli = new cliffold.Cli(process.stdout);

cli.opt("-f", "--file", {
    env: "FILE",
    description: "A file to write stuff to"
});

cli.opd("rootPath", {
    signature: "Root directory",
    description: "The root directory to find files in"
});

// Adding a help option will cause cliffold to automatically present help output
// when -h or --help is encountered.
cli.helpOpt("-h", "--help");

// Adding a log option will cause cliffold to create a logger. It will default
// to STDOUT.
cli.logOpt("-l", "--log-file");

// Adding a pid file option will cause cliffold to maintain a pid file for the
// duration of the process. cliffold sets up an exit listener on the process
// that deletes the file.
cli.pidFileOpt("-p", "--pid-file", process);

// To make sure the pid file is also cleared when the process is forcefully
// killed, add these listeners as well
process.on("SIGINT", function () { process.exit(1); });
process.on("SIGTERM", function () { process.exit(1); });

cli.exec(process.argv.slice(2), process.env, function (error, cliProcess) {
    cliProcess.options; // The posix-argv-parser parse result

    cliProcess.get("-f"); // Gets the value

    cliProcess.log.info("Log some stuff");

    cliProcess.isHandled; // True if help was printed
});

API

new cliffold.Cli(stdout)

Creates a cli object. Pass it STDOUT, typically process.stdout.

cli.opt(short, long[, options])

Creates an option. In addition to the various options supported by posix-argv-parser, the options object supports:

  • description -- Used to print help
  • env -- The (optional) name of the environment variable to prefer over the command line option (if set)

See posix-argv-parser for further details about options.

cli.opd(name[, options])

Creates an operand. In addition to the various options supported by posix-argv-parser, the options object supports:

  • description -- Used to print help
  • env -- The (optional) name of the environment variable to prefer over the command line operand (if set)

See posix-argv-parser for further details about operands.

cli.helpOpt(short, long[, options])

Add a help option. If cliffold encounters the short or long option for help, it will print help and the object passed to the exec callback will have isHandled set to true. The options object may optionally specify name, which is the name of the program and synopsis, which will both be printed in the help output.

See cli.opt for what you can do with the options object.

cli.logOpt(short, long[, options])

Add an option to specify a log file. Creates a logger that is exposed through cliProcess.log, which defaults to logging to STDOUT.

See cli.opt for what you can do with the options object.

cli.pidFileOpt(short, long, process)

If this argument is specified when running the CLI, Write process.pid to the specified file. Attempt to remove the file when the process shuts down. You will want to help out by adding these two listeners yourself:

process.on("SIGINT", function () { process.exit(1); });
process.on("SIGTERM", function () { process.exit(1); });

cli.exec(args, environment, function (error, cp) {});

Parse arguments, and run your program. If any error is encountered, it will be available in the error object. Typical usage:

cli.exec(process.argv.slice(2), process.env, function (err, cp) {
    console.log("File is", cp.arg("--file"));
});

cli.formatHelp()

Formats help and returns it as a string. Useful e.g. when the cli fails for some reason.

The CLI process API

cp.isHandled

Boolean. true if cliffold printed help.

cp.arg(arg)

Get command line argument arg, or it's associated environment variable if provided.

cp.log

The logger object. An instance of Log.

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.