spring-shell icon indicating copy to clipboard operation
spring-shell copied to clipboard

feat: adding mandatory flag to string input

Open Nico-DF opened this issue 1 year ago • 4 comments

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: image image image

Nico-DF avatar Aug 04 '23 13:08 Nico-DF

Issue with jdk8 compilation doesn't seem to originate from me, right?

(investigating though to be sure)

Nico-DF avatar Aug 08 '23 07:08 Nico-DF

As suspected I broke something :D, I'll check it

Nico-DF avatar Aug 08 '23 07:08 Nico-DF

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.

jvalkeal avatar Aug 08 '23 07:08 jvalkeal

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)

Nico-DF avatar Aug 08 '23 08:08 Nico-DF