Skip to content

Instantly share code, notes, and snippets.

@ericlowry
Last active May 17, 2020 15:45
Show Gist options
  • Save ericlowry/3acd5835f46134264f0a5df2ab4fa1d1 to your computer and use it in GitHub Desktop.
Save ericlowry/3acd5835f46134264f0a5df2ab4fa1d1 to your computer and use it in GitHub Desktop.
Simple commander CLI template
#!/usr/bin/env node
//
// exmaple commander cli
//
require('dotenv').config();
const cli = require('commander').version(require('../package.json').version);
const makeInt = v => parseInt(v);
cli
.command(`hash <password>`)
.description('generate a password hash')
.option('--work <workFactor>', 'Password hash difficulty', makeInt, 8)
.action(async (password, opts) => {
console.log(`{"password": "${await hash(password, opts.work)}"}`);
});
/////////////////////////////////////////////////
// error on unknown commands
cli.on('command:*', function() {
console.error(`\nInvalid command: ${cli.args[0]}`);
process.exit(1);
});
cli.parse(process.argv);
if (!cli.args.length) {
cli.outputHelp();
console.log();
process.exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment