3.0.1 • Published 4 years ago
torko v3.0.1
Torko
An easier to use tool to manage command-line arguments and flags in nodejs
Installation
Install torko with npm
npm install torkoAPI Reference
Initilize
To Initilize, simply require and call torko. Passing process.argv.slice(2) as a parameter is optional.
const torko = require('torko')(); // requires and calls torko
console.log(torko);Now,the above script with node index.js start --port 8080 -w.
What are we doing?
- The first part
node index.jssays node to run the script. startis the command we pass to torko. Commands must not start with '-' and they must be passed before flags and arguments.--port 8080, hereportis the argument and8080is value.-wis a flag. Flags don't contain value.
Now, if you've understood, let's focus on the output:
Torko { commands: [ 'start' ], args: { port: '8080' }, flags: [ 'w' ] }commands: Array of commandsargs: Key:Value pair of arguments. Keys are without prefix.flags: Array of flags without prefix.
Methods
.handle().command(<command>).by(<function>): Calls<function>if<command>was passed as command.handle().arg(<arg>).by(<function>): Calls<function>with the value of as a parameter if<arg>was passed as an argument..handle().flag(<flag>).by(<function>): Calls<function>if<flag>was passed as an flag.
Usage/Examples
Logs server starting if start was passed as a command
const torko = require('torko')();
torko.handle().command('start').by(() => {
console.log('server starting');
})Gets port number
const torko = require('torko')();
torko.handle().arg('port').by(port_number => {
console.log(port);
});Checks whether server shiuld be ran in production mode
const torko = require('torko')();
torko.handle().flag('production').by(()=> {
console.log('starting server in production');
});Support
- Report Bug Your feedbacks and suggestions are appreciated. Mail me at abir.sheikh.4044@gmail.com