Giter Club home page Giter Club logo

sywac

So you want a CLI...

Build Status Coverage Status JavaScript Style Guide Dependabot Badge

A better CLI framework, made for the ES2015 era.

Visit https://sywac.io for detailed documentation. NOTE! The docs site is still under construction.

Feature Highlights

  • Single package install
  • Asynchronous parsing, validation, and command execution
  • Type-based argument parsing
  • Plug in your own types or override/extend the built-in ones
  • Support for simple CLIs or complex nested command trees
  • First-class support for positional arguments, with or without commands
  • Flexible auto-generated help content
  • Support for ANSI styles/colors (we recommend chalk)
  • Define styles/colors inline or decorate content with style hooks
  • Coherent API
  • Parse strings as easily as process.argv
  • Supports concurrent parsing, safe for chatbots or other server-side apps

Quick Start Guide

First install sywac from npm:

$ npm install --save sywac

Then create a cli.js file with code similar to this:

#!/usr/bin/env node

const cli = require('sywac')
  .positional('<string>', { paramsDesc: 'A required string argument' })
  .boolean('-b, --bool', { desc: 'A boolean option' })
  .number('-n, --num <number>', { desc: 'A number option' })
  .help('-h, --help')
  .version('-v, --version')
  .showHelpByDefault()
  .outputSettings({ maxWidth: 75 })

module.exports = cli

async function main () {
  const argv = await cli.parseAndExit()
  console.log(JSON.stringify(argv, null, 2))
}

if (require.main === module) main()

Make the cli.js file executable:

$ chmod +x cli.js

And set up cli.js as the "bin" field in package.json:

{
  "name": "example",
  "version": "0.1.0",
  "bin": "cli.js"
}

Tip:

You can use npm init sywac to easily set up the above and add sywac to your project.

Then test it out. Without any arguments, it will print the help text.

$ ./cli.js
Usage: cli <string> [options]

Arguments:
  <string>  A required string argument                  [required] [string]

Options:
  -b, --bool          A boolean option                            [boolean]
  -n, --num <number>  A number option                              [number]
  -h, --help          Show help                  [commands: help] [boolean]
  -v, --version       Show version number     [commands: version] [boolean]

Let's try passing some arguments:

$ ./cli.js hello -b -n 42
{
  "_": [],
  "string": "hello",
  "b": true,
  "bool": true,
  "n": 42,
  "num": 42,
  "h": false,
  "help": false,
  "v": false,
  "version": false
}

What happens if we pass flags without a string argument?

$ ./cli.js --bool
Usage: cli <string> [options]

Arguments:
  <string>  A required string argument                  [required] [string]

Options:
  -b, --bool          A boolean option                            [boolean]
  -n, --num <number>  A number option                              [number]
  -h, --help          Show help                  [commands: help] [boolean]
  -v, --version       Show version number     [commands: version] [boolean]

Missing required argument: string

Validation failed and sywac printed the help text with an error message. Let's check the exit code of that last run:

$ echo $?
1

This is a good sign that our CLI will play well with others.

API

For details on the full API, go to http://sywac.io

License

MIT

sywac's Projects

sywac icon sywac

:no_entry_sign: :mouse: Asynchronous, single package CLI framework for Node

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.