Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding mandatory flag to string input #844

Open
wants to merge 4 commits into
base: 2.1.x
Choose a base branch
from

Conversation

Nico-DF
Copy link

@Nico-DF Nico-DF commented Aug 4, 2023

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

- Added to flow
- Added to component
- Add simple test case
- Add samples
@Nico-DF
Copy link
Author

Nico-DF commented Aug 8, 2023

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

(investigating though to be sure)

@Nico-DF
Copy link
Author

Nico-DF commented Aug 8, 2023

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

@jvalkeal
Copy link
Contributor

jvalkeal commented Aug 8, 2023

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.

@Nico-DF
Copy link
Author

Nico-DF commented Aug 8, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants