1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-08-11 16:24:31 +00:00

Remove support for verifying optional named command parameters

The idea was to be able to flag unknown parameter names, but requiring a command to pre-specify all the parameter names makes it harder for (say) the listen command to be extensible so that plugins can add new optional parameters that they handle. (This is particularly in the context of work in progress to encapsulate authenticators into their own modules).
This commit is contained in:
Jermolene 2018-06-23 16:23:04 +01:00
parent 1c24b3a761
commit 9f4c997292
2 changed files with 5 additions and 20 deletions

View File

@ -96,7 +96,7 @@ Commander.prototype.executeNextCommand = function() {
}
// Parse named parameters if required
if(command.info.namedParameterMode) {
params = this.extractNamedParameters(params,command.info.mandatoryParameters,command.info.optionalParameters);
params = this.extractNamedParameters(params,command.info.mandatoryParameters);
if(typeof params === "string") {
return this.callback(params);
}
@ -130,12 +130,10 @@ Commander.prototype.executeNextCommand = function() {
};
/*
Given an array of parameter strings `params` in name:value format, and an array of mandatory parameter names in `mandatoryParameters`, and an array of optional parameters in
`optionalParameter`, returns a hashmap of values or a string if error
Given an array of parameter strings `params` in name:value format, and an array of mandatory parameter names in `mandatoryParameters`, returns a hashmap of values or a string if error
*/
Commander.prototype.extractNamedParameters = function(params,mandatoryParameters,optionalParameters) {
Commander.prototype.extractNamedParameters = function(params,mandatoryParameters) {
mandatoryParameters = mandatoryParameters || [];
optionalParameters = optionalParameters || [];
var errors = [],
paramsByName = Object.create(null);
// Extract the parameters
@ -144,7 +142,7 @@ Commander.prototype.extractNamedParameters = function(params,mandatoryParameters
if(index < 1) {
errors.push("malformed named parameter: '" + param + "'");
}
paramsByName[param.slice(0,index)] = param.slice(index+1);
paramsByName[param.slice(0,index)] = $tw.utils.trim(param.slice(index+1));
});
// Check the mandatory parameters are present
$tw.utils.each(mandatoryParameters,function(mandatoryParameter) {
@ -152,12 +150,6 @@ Commander.prototype.extractNamedParameters = function(params,mandatoryParameters
errors.push("missing mandatory parameter: '" + mandatoryParameter + "'");
}
});
// Check there no parameters that are not mandatory or optional
$tw.utils.each(paramsByName,function(value,name) {
if(mandatoryParameters.indexOf(name) === -1 && optionalParameters.indexOf(name) === -1) {
errors.push("unknown parameter: '" + name + "'");
}
});
// Return any errors
if(errors.length > 0) {
return errors.join(" and\n");

View File

@ -19,7 +19,6 @@ exports.info = {
synchronous: true,
namedParameterMode: true,
mandatoryParameters: [],
optionalParameters: ["port","host","rootTiddler","renderType","serveType","username","password","pathprefix","debugLevel","credentials"]
};
var Command = function(params,commander,callback) {
@ -35,15 +34,9 @@ Command.prototype.execute = function() {
$tw.utils.warning("Warning: Wiki folder '" + $tw.boot.wikiPath + "' does not exist or is missing a tiddlywiki.info file");
}
// Set up server
var variables = Object.create(null);
$tw.utils.each(exports.info.optionalParameters,function(name) {
if($tw.utils.hop(self.params,name)) {
variables[name] = self.params[name];
}
});
this.server = new Server({
wiki: this.commander.wiki,
variables: variables
variables: self.params
});
var nodeServer = this.server.listen();
$tw.hooks.invokeHook("th-server-command-post-start",this.server,nodeServer);