Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Write a small Spring Shell command 'import-users --file users.csv --dry-run' and explain how the command is registered, how options are parsed, and how you run the same command non-interactively from a script.

In current Spring Shell versions we write a method annotated with @Command(command = "import-users") and parameters annotated with @Option, and we enable the class with @EnableCommand or @CommandScan; the shell parses --file and --dry-run into typed parameters for us. Whatever the method returns is printed. Passing the command as program arguments, java -jar tool.jar import-users --file users.csv, runs it once and exits, which is how scripts and CI use it, and failures are turned into exit codes through exit code mappings rather than System.exit.

Why this question is asked

Many teams need small internal tools: importing data, fixing records, running checks. Spring Shell lets us write them as Spring Boot applications with the same services and configuration as the main system. Interviewers ask for a concrete command because it shows whether we know the current programming model and the practical details, such as how a flag becomes a boolean and how the tool behaves in a script. In this answer, we will recap what Spring Shell is, write the command, see how it is registered and how options are parsed, run it interactively and from a script, and handle exit codes.

1A Quick Recap of Spring Shell

Spring Shell is a Spring project for building command-line applications. When the application starts, it shows a prompt (shell:>) with history, tab completion and built-in commands such as help and exit. Each command we write is a Java method; the shell finds it, reads the words the user typed, converts them into the method's parameters and calls it.

There are two programming models, and this is the first thing to check in any codebase. The older model uses @ShellComponent on the class and @ShellMethod and @ShellOption on methods and parameters. The newer annotation model, from Spring Shell 3, uses @Command and @Option and is the one the project recommends now; the old one is legacy. Spring Shell 4 continues the new model, but annotation attributes have moved between releases, so check the reference documentation of your version.

2Writing the Command

Here is the import command. The import logic lives in an ordinary service bean, so the command method only parses, calls and reports.

Java
@SpringBootApplication
@CommandScan // finds classes annotated with @Command in this package and below
public class AdminToolApplication {
public static void main(String[] args) {
SpringApplication.run(AdminToolApplication.class, args);
}
}
@Command // marks the class as holding commands
class UserImportCommands {
private final UserImporter importer;
UserImportCommands(UserImporter importer) { this.importer = importer; }
@Command(command = "import-users", description = "Import users from a CSV file")
public String importUsers(
@Option(longNames = "file", required = true, description = "CSV file to read") String file,
@Option(longNames = "dry-run", description = "Validate without saving") boolean dryRun) {
ImportResult r = importer.importFile(Path.of(file), dryRun);
return (dryRun ? "Dry run: " : "Imported: ") + r.valid() + " valid, " + r.invalid() + " rejected";
}
}
// shell:>import-users --file users.csv --dry-run
// Dry run: 120 valid, 3 rejected
// shell:>import-users
// Missing mandatory option '--file'

Notice that dryRun is a boolean with no value on the command line. For boolean options the shell treats the bare flag as true and its absence as false, which is exactly how command-line users expect a dry-run switch to behave.

3How the Command Is Registered

At startup, @CommandScan (or @EnableCommand(UserImportCommands.class), which lists classes explicitly) tells Spring Shell to look at those classes. For each method with @Command, it builds a command registration: the command name, its description, its options with their types and whether they are required, and the method to call. These registrations are stored in a command catalog, which is what help lists and tab completion uses.

We can also register commands programmatically with a CommandRegistration bean, which is useful when commands are built from configuration. The annotation model is simply a convenient way to produce the same registrations.

4How Options Are Parsed

When the user types a line, the shell splits it into tokens, matches the first words against the command names, and then reads the rest as options. --file users.csv becomes the value of the file option. Each value is converted to the parameter's type with Spring's conversion service, so a parameter of type int, Path or an enum works without extra code, and a value that cannot be converted produces a clear error instead of reaching our method.

Parameters can also carry Bean Validation annotations such as @Pattern or @Min, and the shell reports violations before calling the method; check your version for how validation is switched on. Required options that are missing are reported in the same way, as we saw above.

Step throughFrom a typed line to a method call
  1. Tokenize the lineimport-users --file users.csv --dry-run
  2. Match the command nameimport-users -> UserImportCommands.importUsers
  3. Bind options--file -> file, --dry-run -> dryRun = true
  4. All required options present and valid?
    YesCall the methodNoPrint the error, do not call
  5. Print the return valueDry run: 120 valid, 3 rejected

5 steps in order. Play them, or step through them one at a time.

5Running It From a Script

Spring Shell has an interactive runner and a non-interactive runner. If the application is started with program arguments, the non-interactive runner treats them as one command, runs it and exits, without showing a prompt. That is what makes the same tool usable from cron jobs, CI pipelines and shell scripts.

Shell
$ java -jar admin-tool.jar import-users --file users.csv --dry-run
Dry run: 120 valid, 3 rejected
$ echo $?
0
# in a CI step
java -jar admin-tool.jar import-users --file build/users.csv || exit 1

Since the tool is a Spring Boot application, we usually also set spring.main.web-application-type=none and a quiet banner so that script output contains only the command's result.

6Exit Codes Without System.exit

A script needs a non-zero exit code when the command fails. Calling System.exit inside a command would skip Spring's shutdown and is hard to test. Instead, Spring Shell maps exceptions to exit codes: on a programmatic registration we can declare mappings such as IllegalArgumentException to 2, and in the annotation model an exception resolver can return a result with a message and a code. The non-interactive runner then exits with that code. The exact API has changed between releases, so check your version's section on exit codes.

With that in place, a missing file can throw an exception that becomes exit code 2 with a readable message, and a successful import returns the summary string and exit code 0.

7Follow-up questions

How would you test a Spring Shell command?

The simplest test calls the command method directly on the bean with a test file and checks the returned string, which needs no shell at all. For a test that includes parsing, Spring Shell has test support, @ShellTest with a ShellTestClient, which starts the shell in the test, sends a line such as import-users --file users.csv --dry-run and lets us assert on the screen output. Checking your version's name for these classes matters, but the idea is the same: unit test the logic, and keep one or two tests that go through parsing.

When would you still see @ShellMethod?

In code written for Spring Shell 2 or early 3. It still works in many versions, but new commands should use @Command, and mixing both models in one tool makes help output and configuration confusing.

How do you keep a destructive command safe?

Make --dry-run the default or require a --confirm flag, print what will change before changing it, and process in batches with a limit so a mistake affects a bounded number of rows.

8Conclusion

In this answer, we wrote an import-users command with the current annotation model: a method with @Command and parameters with @Option, enabled through @CommandScan or @EnableCommand. We saw that registration builds a command catalog at startup, that the shell tokenizes the line and converts options to typed parameters, with booleans acting as flags and validation before the call, that program arguments run the command once and exit for scripts, and that exit codes come from exception mappings rather than System.exit.

What to say in the interviewAbout 80 seconds aloud

Open with

In current Spring Shell versions I would use the annotation model: a class annotated with @Command, enabled with @CommandScan or @EnableCommand, and a method annotated @Command with command equal to import-users.

Then, if they want more

The parameters are a String file with @Option longNames file and required true, and a boolean dryRun with @Option longNames dry-run. The method calls an importer service and returns a summary string, which the shell prints. The older @ShellComponent and @ShellMethod model still exists but is legacy, so I would check which version the project is on.

At startup the shell turns each annotated method into a command registration in its catalog, which drives help and completion.

When a line is typed, it matches the command name, binds each option to its parameter, converts values with Spring's conversion service, treats a boolean option as a flag, reports missing required options or validation errors, and only then calls the method.

To use it from a script, I pass the command as program arguments, like java -jar tool.jar import-users --file users.csv; the non-interactive runner executes it and exits. For failures I would not call System.exit; I would map exceptions to exit codes through the shell's exit code support, so scripts and CI get a non-zero status.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Boot reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Write a small Spring Shell command 'import-users --file users.csv --dry-run' and explain...": https://scrivio-interview-library.netlify.app/spring-boot/spring-projects/14f0c6ec8e/