本文介绍了启动一个非交互式的基于Spring-Shell的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以启动Spring-Shell应用程序的特定命令,然后在命令完成后返回/退出该Shell?还有可能将应用程序的退出代码(System.exit)暴露给操作系统外壳程序吗?

Is it possible to start a specific command of a Spring-Shell app and then return/exit the shell after the command is finished? Further is it possible to expose the exit code (System.exit) of the app to the operating system shell?

出于我的目的,我将利用插件机制和Spring-Shell的CLI注释.但通常来说,与该应用程序之间没有人工交互,相反,作业调度程序(UC4)将启动该应用程序,并在退出代码不等于0的情况下检查退出代码以生成电子邮件.另一方面,对于手动测试我们的客户还需要完成制表符,使用帮助等.

For my purpose i will take advantage of the plugin mechanism and the CLI-Annotations of Spring-Shell. But in general there is no human interaction with the app, instead a job scheduler (UC4) will start the app and check the exit code to generate an email in case of an exit code not equal to 0. On the other hand for manual tests by our customer, there is also the need of tab completion, usage help etc.

推荐答案

此行为已经是内置的(尽管我们考虑将其删除,或者至少使其变为可选项).我现在看到它很有用:)

This behavior is already built-in (although we considered removing it, or at least make it optional). I see now that it is useful :)

使用您的(唯一的)命令简单地调用外壳,外壳将启动,执行该命令,然后退出.另外,shell的返回码已经指示出是否存在错误(例如,使用不存在的命令进行了尝试).当然,如果您的自定义命令未正确指示错误(即打印错误消息,但执行正常返回),则此操作将无效.您应该改为抛出异常.

Simply invoke the shell with your (sole) command and the shell will spin up, execute the command, and quit. Also, the return code of the shell already indicates whether there was an error or not (tried with an inexistant command for example). Of course, if your custom commands do not properly indicate an error (i.e. print an error message but perform a normal return) this will not work. You should throw an exception instead.

这篇关于启动一个非交互式的基于Spring-Shell的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 21:52