Spring Shell : Build your own word counter

Discover Spring Shell by crafting your own command line application

Rahul Gite
Javarevisited
4 min readJun 2, 2024

--

Recently, I took part in Coding Challenges and got my first challenge to build a work counter analogous to the wc command in linux.

I decided to complete this challenge by using Java and Spring entirely.

Photo by Luca Bravo on Unsplash

To my surprise, Spring has a whole library called Spring Shell to build command line application which made the challenge feel like a breeze!

Lets learn how to use Spring Shell by building our own word counter tool! Find the link to my repository below.

Tool Requirements

We will forge a simple yet potent command-line word counter that accepts a valid file path and one of three options:

  • -c : count the number of bytes in the file.
  • -w : count the number of words in the file.
  • -l : count the number of lines in the file.

The output will reflect the corresponding value based on the provided option.

Project Setup

I have used this Spring project setup from Spring Initializr. Generate and download the folder structure in your local machine.

I opted for Maven as a build tool and specific versions of Spring and Java. Ensure Java version 17 or higher to support Spring Shell.

Here is my POM file from the repo. Note that Spring and Java versions may vary.

POM File for the word counter project

Additionally, I have integrated GraalVM support, enabling us to create an executable application that runs anywhere without the source code. This step is optional but enhances flexibility.

Accepting Command-Line Input

Let’s dive into coding the methods and components to capture and process command-line inputs.

First, we will construct the component the application will recognize.

As you can see, we have defined a component in which we will be writing our methods which will accept our inputs.

We have marked this class as a component using a special annotation called @ShellComponent. This is used to restrict the list of classes which can have descriptions for commands in the application.

Now, to define our main command, we make a method inside the component and annotate it with another special annotation, the @ShellMethod.

Shell Component with Shell Methods and Options

Here I have named my function getBytes() which is annotated with the @ShellMethod annotation .

The key argument denotes the name of the command , basically functioning as an identifier to call this method.

The value argument provides a help message when calling help gwc on the command line. Here, it outputs a straightforward help message.

For the method arguments, we accept three command-line options.

The @ShellOption annotation includes parameters:

  • value: A String array specifying the name and aliases of the command-line option.
  • defaultValue: The default value for this option.
  • help: A general help message displayed to the user.

To accept a file path, we take it as a String.

Coding up utilities

To make our component class look clean, lets make a separate utility class which will have our core calculation logic. The file is presented below for your reference.

Utility class to handle core functionality

This file defines methods for calculating byte, word, and line counts, and a method for validating the file path.

Bringing it all together

With our project set up and our command-line methods and utilities defined, let’s finalise the project by implementing the functionality inside getBytes(), our shell method. Below is the final code of our command class for reference.

Final code for the component class

Here, I added basic validation using our file path checker utility function and ensured accurate output based on the provided options.

Running our tool

As a Spring application, run it as you would any standard Spring application.

I tested our tool with various files and options, as shown below. When the file path is invalid, we use the provided text as input( the file path) and calculate the values.

Running the tool with different inputs. Image by the Author

We can also club multiple options to get multiple outputs simultaneously as seen above.

The format of the output is <byteCount> <lineCount> <wordCount> fileName where the counts are present only if the specific option is given.

In this post, we explored the core functionalities of Spring Shell by crafting our own word counter.

Experiment with the tool yourself by checking out the repo!

If you like this content, please give a clap. .

Stay tuned as I continue to share my learning journey. Feel free to comment on topics you’d like to see in future posts. Happy coding!

--

--

Rahul Gite
Javarevisited

I love to write about anything new that I learn from my work and in general. Lets connect on LinkedIn: https://www.linkedin.com/in/rahul-gite-connect/