Skip to content

Instantly share code, notes, and snippets.

@Johannestegner
Last active February 13, 2020 19:12
Show Gist options
  • Select an option

  • Save Johannestegner/8e82b2828b94845ca91fb49067bc2736 to your computer and use it in GitHub Desktop.

Select an option

Save Johannestegner/8e82b2828b94845ca91fb49067bc2736 to your computer and use it in GitHub Desktop.
rand.js
#!/usr/bin/env node
const { Manager, Command, Option, Argument } = require('@jitesoft/cli');
const random = require('@jitesoft/random-string').default;
const manager = new Manager('random-string', 'Generate random strings.');
manager.register(
new Command('gen', 'Generate a string.', {
args: [
new Argument('length', 'Length of the random string', true, Number)
],
options: [
new Option('spec', 'Special characters, 0 means none, number means minimum amount in generated string.', ['s'], true, Number, false),
new Option('alpha', 'Alpha characters, 0 means none, number means minimum amount in generated string.', ['a'], true, Number, false),
new Option('num', 'Numeric characters, 0 means none, number means minimum amount in generated string.', ['n'], true, Number, false),
]
}, (_, input, args, options) => {
const len = parseInt(args[0].value);
const alpha = options.has('alpha') ? parseInt(options.get('alpha').value) : len / 3;
const num = options.has('num') ? parseInt(options.get('num').value) : len / 3;
const special = options.has('spec') ? parseInt(options.get('spec').value) : len / 3;
return Promise.resolve(random(len, {
alpha: alpha !== 0,
minAlpha: alpha,
numbers: num !== 0,
minNumbers: num,
special: special !== 0,
minSpecial: special
}));
})
);
Promise.resolve(manager).then((mng) => {
return mng.invoke();
}).then((result) => {
console.log(result);
}).catch((error) => {
console.error(error.message);
}).finally(() => {
process.exit(1);
});
{
"name": "random-string",
"version": "1.0.0",
"bin": "index.js",
"author": "",
"license": "ISC",
"dependencies": {
"@jitesoft/cli": "^2.5.0",
"@jitesoft/random-string": "^1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment