grails - 在grails命令中指定参数

标签 grails

我正在使用grails 3.2.6。我正在编写一个自定义的grails命令。这是命令而不是脚本,因为我需要在命令的spring应用程序上下文中访问对象。我想让我的命令接受带有“名称”,“组”和“id”等值的命令行参数。例如,我希望能够运行如下命令:

grails run-command process-group --name=foo --group=bar --id=2

但是,当我这样做时,我的命令实现类似乎无法访问任何这些参数(名称,组或ID)。我试过通过GrailsApplicationCommand特征的args属性访问它们,但该属性为空。我还尝试通过CommandLine对象的各种方法来访问它,该方法也可以通过executionContext.commandLine对象访问,该对象也来自GrailsApplicationCommand特性。但是,那里的所有内容似乎也都是空的(undeclaredOptions为空,remainingArgs为空,rawArguments的命令名只是“process-group”,等等)。如果从这样的参数名称中删除前导“-”:
grails run-command process-group name=foo group=bar id=2

那么args属性包含3个条目:[“name = foo”,“group = bar”和“id = 2”]。我想我可以用这种方式实现我的命令(命令参数上不能以“-”开头),但这有点丑陋,这也意味着我必须自己解析这些参数。 grails文档(http://docs.grails.org/latest/guide/single.html#creatingCustomCommands)说,“自Grails 3.2.0起,命令在检索参数,模板生成,文件访问和模型构建方面具有与脚本相似的功能。”我对该语句的检索参数部分感兴趣。脚本文档(不是命令)中显示的示例显示(http://docs.grails.org/latest/guide/single.html#creatingCustomScripts)如何声明脚本将从命令行获取的参数。例如,此命令行应该与一个自定义脚本一起使用,该脚本为所有生成的自定义脚本声明“force”参数:
grails generate-all MyClass --force

所有这些似乎都与脚本中description()方法的使用有关:
description( "Generates a controller that performs CRUD operations and the associated views" ) {
  usage "grails generate-all <<DOMAIN CLASS>>"
  flag name:'force', description:"Whether to overwrite existing files"
  argument name:'Domain Class', description:'The name of the domain class'
}

但是,似乎没有自定义命令可使用的description()方法用于自定义命令。我只是对grails文档如何声明检索参数现在应该对自定义脚本和自定义命令都起作用感到困惑,但是我似乎无法像从自定义脚本中那样从自定义命令中检索参数。有没有办法像对自定义脚本一样检索自定义命令的参数?

最佳答案

使用-D选项设置属性:

-Dsample.message=foo

使用以下代码来获取系统属性:
System.properties['sample.message']

链接到我找到此示例的位置:

Passing Properties

关于grails - 在grails命令中指定参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42938909/

相关文章:

grails - 如何使用条件检索域对象

grails - grails如何在关系表上创建触发器(很多)

javascript - 如果没有此警报框,为什么我的代码无法工作?

grails - 我怎么知道Grails应用程序已满载?

tomcat - 对 EC2 上文件系统的 HTTP 访问

sql-server - 在grails 3.3.9中提供默认的辅助排序列

grails db-generate-changelog 错误

grails - Grails构建异常失败

mongodb - 如何拦截和记录 mongodb 查询?

Grails Spring Security插件@Secured注解未解析