spring-shell
spring-shell copied to clipboard
feat: adding mandatory flag to string input
As I began to use spring-shell for some CLI workflow, I found that there is no input required for string (altough there is default).
For the moment, I used this kind of code:
public String input(String message, String defaultValue, boolean mandatory) {
var component = new StringInput(getTerminal(), message, defaultValue);
component.setResourceLoader(getResourceLoader());
component.setTemplateExecutor(getTemplateExecutor());
var context = component.run(ConfirmationInput.ConfirmationInputContext.empty());
var result = context.getInput();
while (mandatory && !StringUtils.hasText(result)) {
result = component.run(ConfirmationInput.ConfirmationInputContext.empty()).getInput();
}
return result;
}
or this as flow:
componentFlowBuilder.clone().reset() //
.withStringInput("id")
.name("Select an identifier")
.next(ctx -> StringUtils.isBlank(ctx.getInput()) ? "id" : null)
.and().build();
Which work but it could be simpler, hence my PR.
So I:
- Added to flow the mandatory flag
- Added to component the mandatory flag
- Add simple test case
- Add samples
Result:
Issue with jdk8 compilation doesn't seem to originate from me, right?
(investigating though to be sure)
As suspected I broke something :D, I'll check it
I'm just thinking if this is going to be a bit confusing for a user if there's a default value.
my-shell:>component string required
? Enter value [Required][Default myvalue]
I'm kinda questioning if there's better way to give indication to user that something is required.
I'm just thinking if this is going to be a bit confusing for a user if there's a default value.
my-shell:>component string required ? Enter value [Required][Default myvalue]
I'm kinda questioning if there's better way to give indication to user that something is required.
I also wondered about it. I think the easiest way would be to display Required only if there is no default (and it also matches with what really happens)