Coder Social home page Coder Social logo

cmdx's Introduction

ARCHIVED / DEPRECATED

cmdx

Construct Node.js CLI's with ease, inspired by Regular Expression Syntax.

Tutorial

  1. Create cli.js and define a basic command:
#!/usr/bin/env node

const cmdx = require("cmdx");

const args = cmdx
  .usage("<Number:a> <op> <Number:b> [--floor, --round]?")
  .arg("op", {values: ["+", "-", "/", "*"]})
  .parse(process.argv);
  
if (!args) return console.log("Unrecognized Command");  
  
let value, a = args.a, b = args.b;
switch (args.op) {
  case "+": value = a + b; break;
  case "-": value = a - b; break;
  case "*": value = a * b; break;
  case "/": value = a / b; break;
  default: value = 0;
}
if (args["--floor"]) value = Math.floor(value);
if (args["--round"]) value = Math.round(value);
console.log(`${a} ${op} ${b} = ${value}`);  
  1. Create package.json
{
  "bin": {
    "math": "./cli.js"
  }
}
  1. Install globally for use in your terminal
npm install -g
  1. Run the math command
> math 1 + notanumber
Unrecognized Command

> math 1.2 + 2.3
3.5

> math 1.2 * 2.3 --floor
2

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.