Skip to content

Commit d4c1135

Browse files
czpilarfmbenhassine
authored andcommitted
Fix script command to accept the script file as an option
Resolves #1301 Signed-off-by: czpilar <[email protected]>
1 parent 439676e commit d4c1135

File tree

2 files changed

+25
-11
lines changed
  • spring-shell-core/src/main/java/org/springframework/shell/core/command
  • spring-shell-docs/modules/ROOT/pages/commands/builtin

2 files changed

+25
-11
lines changed

spring-shell-core/src/main/java/org/springframework/shell/core/command/Script.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.File;
1919
import java.util.List;
20+
import java.util.Objects;
2021

2122
import org.springframework.shell.core.FileInputProvider;
2223
import org.springframework.shell.core.NonInteractiveShellRunner;
@@ -27,6 +28,7 @@
2728
* @author Eric Bottard
2829
* @author Janne Valkealahti
2930
* @author Mahmoud Ben Hassine
31+
* @author David Pilar
3032
*/
3133
public class Script implements Command {
3234

@@ -42,15 +44,29 @@ public String getGroup() {
4244
return "Built-In Commands";
4345
}
4446

47+
@Override
48+
public List<CommandOption> getOptions() {
49+
return List.of(CommandOption.with()
50+
.type(String.class)
51+
.shortName('f')
52+
.longName("file")
53+
.required(true)
54+
.description("The absolute path to the script file to execute")
55+
.build());
56+
}
57+
4558
@Override
4659
public ExitStatus execute(CommandContext commandContext) throws Exception {
47-
List<CommandArgument> arguments = commandContext.parsedInput().arguments();
48-
if (arguments.size() != 1) {
49-
throw new IllegalArgumentException(
50-
"Script command expects exactly one argument: the absolute path to the script file to execute.");
51-
}
52-
String scriptFile = arguments.get(0).value();
53-
File file = new File(scriptFile);
60+
String scriptFile = commandContext.parsedInput()
61+
.options()
62+
.stream()
63+
.filter(o -> "file".equals(o.longName()) || 'f' == o.shortName())
64+
.map(CommandOption::value)
65+
.filter(Objects::nonNull)
66+
.findFirst()
67+
.orElseThrow(() -> new IllegalArgumentException(
68+
"Script command expects option --file or -f with exactly one argument: the absolute path to the script file to execute."));
69+
File file = new File(Objects.requireNonNull(scriptFile));
5470
try (FileInputProvider inputProvider = new FileInputProvider(file)) {
5571
String input;
5672
while ((input = inputProvider.readInput()) != null) {

spring-shell-docs/modules/ROOT/pages/commands/builtin/script.adoc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33

44
The `script` command allows you to execute a series of commands from a specified script file. This is useful for automating repetitive tasks or setting up an environment quickly.
55

6-
This command expects a file path as an argument, which points to the script file containing the commands to be executed. For example:
6+
This command expects a file absolute path as an option, which points to the script file containing the commands to be executed. For example:
77

88
[source,shell]
99
----
10-
$>script -- /path/to/your/script.txt
10+
$>script --file /absolute/path/to/your/script.txt
1111
----
1212

13-
Since the script command does not define any options, you need to use the `--` separator before specifying the script file path to avoid ambiguity. Please refer to the xref:commands/syntax.adoc[] section fpr more details about command syntax.
14-
1513
The script file should contain one command per line, and the commands will be executed in the order they appear in the file. Comments can be added to the script file by starting a line with a `#` character.

0 commit comments

Comments
 (0)