1

I want to accept optional parameters when creating a function to handle a command in the JQuery Terminal plug-in.

eg.

Foo -> returns a list of Foo's

Foo 1234 -> returns details of Foo 1234 only

Foo Active -> returns a list of Active Foo's

However, if I make my function header like this:

Foo: function() { ... }

...and then issue the command...

Foo 1234

...JQuery Terminal complains "[Arity] Wrong number of arguments. Function 'Foo' expects 0 got 1!".

The same goes for...

Foo: function(p1) { ... }

The command...

Foo

...causes JQuery Terminal to respond with "[Arity] Wrong number of arguments. Function 'Foo' expects 1 got 0!".

Terry Wray
  • 21
  • 4
  • Set the function to accept an argument. Check the type of the argument. If it was null, return the list. If it was an int type, look up the Foo by that id, if it was a string check the value if it was 'active' and then get the list of Active Foo. If you need more specific help implementing this, please add a more complete example of your code to the question – Rory McCrossan Apr 01 '22 at 16:36
  • Thank you for your response, Rory. I’m not being clear. JavaScript will allow me to have a parameter in a function but call it without specifying a parameter. However, JQuery Terminal seems to enforce parameter symmetry. – Terry Wray Apr 02 '22 at 21:19
  • That's true, I assumed it was a restriction put in place by the library itself - but it is a bit odd. I've not used this specific library to offer any further help though – Rory McCrossan Apr 02 '22 at 21:28
  • You need to use the option `checkArity: false`. By default, jQuery Terminal checks the number of arguments. I probably need to make this clear on a Wiki. https://terminal.jcubic.pl/api_reference.php#checkarity – jcubic Apr 03 '22 at 17:18

2 Answers2

1

Here's how I implemented jcubic's response:

<body>
<script>
$('body').terminal({
    dostuff: function(...rawArgs) {
        if (typeof rawArgs[0] === "undefined") {
            //no arguments supplied. do the default action.
            this.echo('Thanks for running the dostuff command!');
        } 
        else {
            //at least one argument supplied
            //change all args to lowercase
            const lowerArgs = rawArgs.map(element => {
                return element.toLowerCase();
            });

        lowerArgs.forEach(myCustomArgProcessor);

        //..more custom command handling goes here
        }
    }
}, 
{checkArity:false, greetings:'Welcome message goes here\n'}
    //checkArity:false 
        //This directs JQuery Terminal to allow a different number of arguments than what the handler function expects.
        //The purpose of this is to enable function overloading, enabling commands to be used naked or with multiple parameters.
    //greetings:<text>
        //This is the greeting that shows when JQuery Terminal starts up.
);
</script>
</body>
Terry Wray
  • 21
  • 4
0

A good workaround is to use a have a parameter but inside the function use an if-else statement to execute code when no parameter is passed, with checkArity: false in options.

Take this for example:

help: name => {
            if (!name) main.echo("For more information on a specific command, type help 'command-name'.");
            else switch (name) {
                    default:
                        main.error("Unknown flag or command: " + name);
                        break;
                    case "broadcast":
                        main.echo("Broadcasts the message to all users.");
                        break;
                    case "connect":
                        main.echo("Connects the terminal to server for chatting.");
                        break;
                    case "disconnect":
                        main.echo("Disconnects the terminal from server.");
                        break;
                    case "help":
                        main.echo("Provides full information for terminal commands.");
                        break;
            }
        }
Akshat
  • 1
  • 3