Sunday 14 February 2021

Spring Shell: Organizing commands

When your shell application has lots of commands, we should organize them properly, so users can consume them easily.

 

What is the default behavior of help command?

By default ‘help’ command would see a daunting list of commands, organized by alphabetical order and commands group together according to the class they are implemented in, turning the camel case class name into separate words. For example, for the class name 'ArithmeticCommandCustomizeCommandName' group name will be 'Arithmetic Command Customize Command Name').

shell:>help
AVAILABLE COMMANDS

Arithmetic Command Customize Command Name
        sum: Add two integers together.

Arithmetic Commands
        add: Add two integers together.
        div: Devide two integers together.
        mul: Multiply two integers together.
        sub: Subtract two integers together.

Boolean Type Demo
        shutdown: Shutdown the system

Built-In Commands
        clear: Clear the shell screen.
        exit, quit: Exit the shell.
        help: Display help about available commands.
        history: Display or save the history of previously run commands
        script: Read and execute commands from a file.
        stacktrace: Display the full stacktrace of the last error.

Customize Multiple Named Keys For Single Parameter
        print-person: Commands to print Person Details

Customize Named Param Keys
        print-employee: Commands to print Employee Details

Customize Named Param Keys Prefix
        print-student: Commands to print Student Details

Default Values
        greet-user: Commands to greet user

Dynamic Command Validation Demo1
        connect: Connect to Database
      * print-table-records: Print all records of table

Dynamic Command Validation Demo2
        connect-to-oracle: Connect to Oracle Database
      * print-table-records-from-oracle: Print all records of table from Oracle DB

Dynamic Command Validation Demo3
        connect-to-db2: Connect to Db2 Database
      * download-table-records-from-db2: Download all records of table from DB2
      * print-table-records-from-db2: Print all records of table from DB2

Multi Valued Parameters Demo
        square: Commands to Calculate Square of given numbers

Multiple Aliases
        say-hello, welcome, wish: Commands to welcome user

Named Params
        get-user: Commands to print User Details

Commands marked with (*) are currently unavailable.
Type `help <command>` to learn more.

 

How can we customize this grouping behaviour?

‘Spring Shell’ provide following ways to group the related commands together.

 

a.   specifying a group() in the @ShellMethod annotation

 

b.   placing a @ShellCommandGroup on the class the command is defined in. This will apply the group for all commands defined in that class (unless overridden as above)

 

c.    placing a @ShellCommandGroup on the package (via package-info.java) the command is defined in. This will apply to all commands defined in the package (unless overridden at the method or class level as explained above)

 

Example 1:

 

public class ArithmeticCommands {
    @ShellMethod(value = "This command ends up in the 'Arithmetic Commands' group")
    public void foo() {}

    @ShellMethod(value = "This command ends up in the 'Other Commands' group",
            group = "Other Commands")
    public void bar() {}
}


Example 2:

@ShellCommandGroup("Other Commands")
public class SomeCommands {
        @ShellMethod(value = "This one is in 'Other Commands'")
        public void wizz() {}

        @ShellMethod(value = "And this one is 'Yet Another Group'",
                group = "Yet Another Group")
        public void last() {}
}


After I group the commands according to their behaviour, help command output looks like below.

shell:>help
AVAILABLE COMMANDS

Arithmetic Commands
        add: Add two integers together.
        div: Devide two integers together.
        mul: Multiply two integers together.
        sub: Subtract two integers together.
        sum: Add two integers together.

Built-In Commands
        clear: Clear the shell screen.
        exit, quit: Exit the shell.
        help: Display help about available commands.
        history: Display or save the history of previously run commands
        script: Read and execute commands from a file.
        stacktrace: Display the full stacktrace of the last error.

Dynamic Command Validation
        connect: Connect to Database
        connect-to-db2: Connect to Db2 Database
        connect-to-oracle: Connect to Oracle Database
      * download-table-records-from-db2: Download all records of table from DB2
      * print-table-records: Print all records of table
      * print-table-records-from-db2: Print all records of table from DB2
      * print-table-records-from-oracle: Print all records of table from Oracle DB

Miscellaneous
        greet-user: Commands to greet user
        say-hello, welcome, wish: Commands to welcome user
        shutdown: Shutdown the system
        square: Commands to Calculate Square of given numbers

Named Parameters
        get-user: Commands to print User Details
        print-employee: Commands to print Employee Details
        print-person: Commands to print Person Details
        print-student: Commands to print Student Details

Commands marked with (*) are currently unavailable.
Type `help <command>` to learn more.


You can download complete application from this link.

https://github.com/harikrishna553/springboot/tree/master/shell/hello-world






Previous                                                    Next                                                    Home

No comments:

Post a Comment