Well since I've been doing some Duktape stuff lately, I recalled this loose end and have come up with a solution.
There were a few issues - please let me know if you have a problem with any of them.
Firstly the Command registration map is using const char * and we can't guarantee the lifetime of the duktape strings so I've moved them over to std::string.
This applies to OvmsCommandMap itself as well as OvmsCommand - m_name, m_title and m_usage_template.
I think the intention was to have something like this for the register command.. it was missing a parameter. It now looks like this:
OvmsCommand.Register( function, parent, command, description, parameter_desc, minargs, maxargs).
Missing maxargs will mean maxargs = minargs.
Missing minargs will mean no parameters.
The calling function will be passed 2 parameters: the name of the command and an array of strings (the argv).
For sub-commands, these will be separated by '/'. So something like "me/too".
I think this way is simple but effective.
It would be good to add completion, but that can be added later.
One of the things I wanted to talk about is what security protections we need to add to prevent a script from taking over an existing function! (or future function).
When vim initially added user commands and functions it was a free-for-all but somebody soon realised there needed to be some restrictions to avoid issues..
so what they did was force user function and commands to start with a capital letter. I would like people's thoughts on whether we need to do something similar.
It might be that we would be satisfied with failing if system commands were attempted to be overridden. Should we allow adding sub-commands to system commands?
I can see that might be useful..
I have added a sample below of a working function:
--8<----
mycommand = function(c, argv){
print("Hello There: "+c+"\n");
if (argv.length == 0)
print("Simple\n");
else
print("Script: "+argv[0]+"\n")
}
OvmsCommand.Register(mycommand, "", "me", "My Script", "[string]", 0, 3)
OVMS# me xx
Hello There: me
Script: xx
OVMS# me xx yy
Hello There: me
Script: xx
OVMS# me xx yy
Hello There: me
Script: xx
OVMS#