Coder Social home page Coder Social logo

lieutenant's Introduction

Build Status

Lieutenant

I made a router for a command-line app I was making, and this is it!

I wanted to build an app that worked less like a traditional *nix app, with its series of "-t 13" and "--other-option=yesplz" key-value arguments, and more like git, in those parts where you give word commands like "stash pop" and "branch".

Usage

To make a command-line app, preheat your oven to 176.7°C and type:

const router = require('lieutenant')

router({
	double(number) {
		if (typeof number === "undefined" || isNaN(number)) {
			console.log('wat')
		} else {
			console.log(parseFloat(number) * 2)
		}
	}
})

Run your app with example.js double 13 and it will spit 26 right back at you.

But who has time to type out whole words? Lieutenant lets you type in as few characters as you want, as long as it's enough to uniquely identify a command. example.js d 13 will work just as well.

You don't have to specify the arguments, Lieutenant doesn't care, it will pass them all in so you can handle any number of values for a command:

router({
	add(...args) {
		console.log(args.reduce(
			(a, b) => parseFloat(a) + parseFloat(b))
		)
	}
})

and example.js add 3 4 5 => 12 just like you'd hope.

But maybe that's not why you're here - maybe you're holding out for some real routing, some recursion!

router({
	test: {
		even(n) {
			const even = parseInt(n) % 2 === 0
			console.log("That's " + (even ? "" : "not ") + "even!")
		},
		odd(n) {
			const even = parseInt(n) % 2 === 0
			console.log('That's ' + (even ? 'not ' : ') + 'odd!')
		}
	}
})

But hey, what if you want people to just example.js test without specifying anything else? Don't worry; there's a hack for that. Just add a default property:

router({
	test: {
		even(n) {
			const even = parseInt(n) % 2 === 0
			console.log('That's ' + (even ? ' : 'not ') + 'even!')
		},
		odd(n) {
			const even = parseInt(n) % 2 === 0
			console.log('That's ' + (even ? 'not ' : ') + 'odd!')
		},
		default() {
			console.log('I just checked, and your computer is still on.')
		}
	}
})

Default output

If the user passes in arguments that aren't valid, Lietenant doesn't give them any hints about which commands are valid. If you want your app to say something useful in those cases, you'll have to write it yourself.

You can supply your custom error-handling by passing a function as the second argument to the routing function:

router({
	add(...args) {
		console.log(args.reduce(
			(a, b) => parseFloat(a) + parseFloat(b)
		))
	}
}, function badRoute(...args) {
	console.log('Type in double or add or test or something, not those dumb', args.length, 'words you did')
})

As Relient K said in that one song they wrote back when they were still styling a catchy bassline, the rest is up to you!

License

WTFPL

lieutenant's People

Contributors

tehshrike avatar

Watchers

 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.