This sounds like a really good idea though we’d need to think it through. I think the biggest issue will be whether or not a question being "ask"ed would ever have a legitimate answer of an empty string. If not, I don’t think it would be too hard.
Adding a default to ask could be as simple as adding a second 'defaultValue" parameter to the ask() method in shell.cfc. Just check what comes back from the following line
var input = variables.reader.readLine( arguments.message );
and if it’s an empty string, just use the default value. The “ask” method in baseCommand would also need its signature updated.
Using the default for command parameters would be handled here:
https://github.com/Ortus-Solutions/commandbox/blob/master/src/cfml/system/services/CommandService.cfc#L530
There’s a couple things to consider though:
- CFML allows runtime expressions to be for method arguments:
function foo( bar=myvariable, baz=[] ) {}
In those instances it’s impossible to get that value from the function’s metadata.
- We’d need to make sure noone ever set a command parameter to ‘required’ if they wanted the command to be executable without user input using the default. I think that would be easy enough.
- optional, no default – don’t ask if not provided, undefined
- optional, with default – don’t ask if not provided, default value used
- required, no default – ask if not provided, force user to enter something at gunpoint
- required, with default – ask if not provided, allow “empty” input to use the default value
And that last bold part would be where your suggestion fits in. With the assumption that a blank value is never a desired value and the default value in the component is not a runtime expression.
> Looking at the API… It looks like we could add Completors for the Ask command though… maybe?
No, the reason tab complete doesn’t work for "ask’ boils down to the Java jline libraries. They have a sort of narrow vision for how their libs are used in my opinion and they sort of view readLine() and readCharacter() (which we use in shell.waitForkey()) to be “internal method[s]” that we shouldn’t rely on. (https://github.com/jline/jline2/issues/152)
Since the tab complete captures the tab characters and outputs the completion options at the Java level we have no control over that. I am, however, in favor of bugging the Jline people to enhance their java library, but their lackluster response to my previous tickets don’t give me a great deal of hope. I think it’s truly a “community” project in people spare time so we’d be more likely to get it changed if we actually did the work and submitted pulls (which STILL isn’t a guarantee, of course).
Thanks!
~Brad
ColdBox Platform Evangelist
Ortus Solutions, Corp
E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com