0.1.1 • Published 11 days ago

vcate v0.1.1

Weekly downloads
-
License
ISC
Repository
-
Last release
11 days ago

VCate

This library is designed to simplify the handling of command line arguments.

This documentation is valid for version 0.0.9.

Start

Install:

npm install vcate

Import:

const VCate = require("vcate");

Work

After installation and import, you need to initialize the class.

const argv = process.argv;
const VCate = require("vcate");

let cat = new VCate(argv);

.add

To add a command to which the library will respond, use the ".add" command.

const argv = process.argv;
const VCate = require("vcate");

let cat = new VCate(argv);

cat.add("command", function(argv=[""], option=null){}, option={});

command - this is the command to which the library will respond.

function - this is a function that will be called if the command is found. It has two arguments, it is argv="" it is an array of arguments that was passed during the initialization of the class, option=null is an object to which the parameters will be transferred, how to set them will be explained later.

option - This is not a mandatory argument (if it is not set, the option in the function is also not needed), it is an object with functions.

.listen

To start listening to commands, there is a ".listen" command, it must be at the end after adding all commands.

const argv = process.argv;
const VCate = require("vcate");

let cat = new VCate(argv);

cat.add("command", function(argv=[""], option=null){}, option={});
cat.listen();

.notFound

There is also an option to set a command in case an unexpected command was entered for this e.t.notFound parameter.

const argv = process.argv;
const VCate = require("vcate");

let cat = new VCate(argv);

cat.notFound = ()=>console.log("Command not found.")

cat.add("command", function(argv=[""], option=null){}, option={});
cat.listen();

Example

Let's create a small program that will display the text in red when the "red" command is used, and green when the "green" command is used.

// We get the term parameters.
const argv = process.argv;
// We import Vcate
const VCate = require("vcate");
// Import the auxiliary class Console.
const Console = require("vcate/Console");
// Let's initialize the class.
let cat = new VCate(argv);
// We install the .notFound command
cat.notFound = ()=>Console.error("Command not found.")
// Add a command for red color.
cat.add("red", (argv=[""], option=null)=>{
    // We check whether there is a field we need in option.
    if(option.what){
        // Output as an error to obtain a red color.
        Console.error(option.what);
    };
}, {
    what: (argv)=>{
        // We process the "what" field in "option" and return the required value, in our case this is the text to be displayed.
        if(argv[3]){
            return argv.slice(3).join(" ");
        } else {
            return false;
        };
    }
});
// We do everything the same as for red, but output through confirm to get green color.
cat.add("green", (argv=[""], option=null)=>{
    if(option.what){
        Console.confirm(option.what);
    };
}, {
    what: (argv)=>{
        if(argv[3]){
            return argv.slice(3).join(" ");
        } else {
            return false;
        };
    }
});

cat.listen();

Now when running "node app red Hello!" will be displayed in red "Hello!" and when "node app green !olleH" is green "!elloH" and if you run "node app yellow WoW" it will be displayed in red "Command not found.".

0.1.1

11 days ago

0.1.0

22 days ago

0.0.9

4 months ago

0.0.8

4 months ago

0.0.7

5 months ago

0.0.6

7 months ago

0.0.5

7 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago