Last active
May 17, 2020 15:45
-
-
Save ericlowry/3acd5835f46134264f0a5df2ab4fa1d1 to your computer and use it in GitHub Desktop.
Simple commander CLI template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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