Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
command-router
Advanced tools
Writing simple, composable modules makes it easy to have your applications functionality available in multiple contexts. command-router
aims to help you expose that functionality by providing a simple way to define a CLI with multiple actions (like git
).
command-router
provides:
If your app only has one command (like curl
for instance) you should use a [different module]]optimist instead of this one.
With npm do:
npm install command-router
In your bin or JS file:
var cli = require('command-router')
// Options are optional, and basic boolean options that default to
// false can be simply defined
cli.option('verbose')
// Define the --port option with an alias of -p and set an
// intelligent default
cli.option({ name: 'port'
, alias: 'p'
, default: process.env.PORT || 8080
, type: Number
})
cli.command('help', function(){
console.log('commands: server')
})
cli.command('server', function(){
if (cli.options.help) {
return console.log('Help for server the command')
}
// code for running server here...
})
// Set the whole thing in motion...
cli.parse(process.argv)
By default, if a route is not found command-router
will throw unless you add add a listener for the notfound
event (I couldn't think of a better way to handle this, if you have a better idea let me know). This is important for instances when scripting your CLI and you need proper exit codes when piping to other utilities.
cli.on('notfound', function(action){
console.error('I don\'t know how to: ' + action)
// you could even get smart and display some info about what
// should happen next
process.exit(1)
})
var cli = require('command-router')
Define route matchers for your application using a string or regex for route
, the fn
function will be triggered if the route
is matched when cli.parse()
is called. The fn
is called with params
and options
arguments which are also available directly on the cli object.
This routing style is heavily influenced by sinatra and relies on the wonderful routes module, for more details on how to define routes check it's documentation.
// simple stringed route
cli.command('speak dog', function(){
console.log('woof.')
})
// name params
cli.command('speak :animal', function(){
console.log(cli.params.animal)
})
// splats!
cli.command('speak *', function(){
console.log(cli.params.splat)
})
// regex, don't cut your self...
cli.command(/speak (.*)$/, function(){
console.log(cli.params.splat)
})
Define available options, their aliases, and default values.
name
: The name of the option that will map to a command linealias
: Allows a shorthand to be defined.type
: Define the type for this flag. Defaults to Boolean
default
: The default value for the option being defined. Defaults to false
nopt is used for parsing the options internally. params.type
will need to be one of the types defined in nopt.typeDefs
:
Date
is one of the options,
then it will return a Date object, not a string.true
or false
. If an option is a boolean,
then it does not need a value, and its presence will imply true
as
the value. To negate boolean flags, do --no-whatever
or --whatever false
outfd
and logfd
config options.)Array
is specified as one of the types, then the value
will be parsed as a list of options. This means that multiple values
can be specified, and that the value will always be an array.See the nopt documentation for more details.
If the params
object is missing keys for type
and default
the option will be defined as a Boolean
type defaulting to false
without an alias:
cli.option({ name: 'verbose' })
// Is the equivalent to:
cli.option({ name: 'verbose'
, type: Boolean
, default: false
})
// And can be simplifed to
cli.option('verbose')
To set an option for something like a path
to a config file:
var path = require('path')
cli.option({ name: 'config'
, alias: 'c'
, default: '.haiku/config.js'
, type: path
})
Parses the argv
array and triggers the appropriate route.
This project is really just a simple API which wraps several other excellent libraries:
MIT
FAQs
A simple CLI router for apps with sub-commands (like git)
The npm package command-router receives a total of 16 weekly downloads. As such, command-router popularity was classified as not popular.
We found that command-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.