Skip to content

How to make my own commands?

Garrus edited this page Sep 7, 2015 · 6 revisions

What are they made of, exactly?

Commands are defined as valid JavaScript objects with their own properties and methods.

And how do I add them?

They are all part of one array. This makes them iterable and facilitates adding, modifying or simply using them!

Alright... Now how do I make one, exactly?

Let's take the example command: "help". See stxenon.js for more examples in a "live" environment.

First, we have to name it:

"name": "help"

The variable "name" is used by the command parser to find it.


Sometimes, commands have other names or ways to call them, thus we define an "alias":

"alias": "?"

So if the user types "help" or "?", the result is the same.


When all the commands are listed, a short description of what they do is also shown. This is how they're declared:

"desc": "[help] shows how to use the console and the user-made commands."

This is used by the integrated "help" command (which also happens to be our example!)


Then, we have to figure if this command will require specified parameters:

"parameterRequired": false

A simple boolean value, used by the command parser to make sure its gets executed properly


Speaking of execution, the way the parser runs the command is by calling the "execute" sub-method:

"execute": function(){
    //shows helpful stuff
}

Now, if the command has parameters, we simply refer "data":

"execute": function(data){
    //shows helpful stuff with data[0] or data[1]
}

This is an array sent by the parser.


Finally, the function named "help" is a self-contained how-to on the usage of the command:

"help": function(){
    //shows helpful stuff about the helpful stuff
}

The parser runs this if "help" is specified as a parameter. This means that "help" is a PROTECTED parameter and is managed by the parser ONLY (unless you modify its behaviour, which is fine with me)

And how should it look, in the end?

//Other commands may preceed
{
    "name": "help",

    "alias": "?",

    "desc": "[help] shows how to use the console and the user-made commands.",

    "parameterRequired": false,

    "execute": function(){
        "use strict";

        // First, let's explain what STX is
        outputToConsole(
            "Using " + consoleEdition + " is like using your favorite *NIX-based terminal.<br>"+
            "&nbsp;&nbsp;Here are the currently defined commands and what they do: "
        ,false);

        // Now, let's go through them...
        var i;
        for (i = 0; i <= commandsArray.length - 1; i++){

            // To show their description!
            outputToConsole(commandsArray[i].desc, true);
        }

        // We're adding a little addendum here
        outputToConsole(
            "For more information, type the command followed by \"help\".<br>"+
            "&nbsp;&nbsp;Doing that will output the command's own docs."
        ,false);
    },

    "help": function(){
        "use strict";
        outputToConsole("What do you think you are doing here, exactly?",false);
    }
}