【发布时间】:2021-06-02 00:35:29
【问题描述】:
我希望能够通过命令行使用我的应用程序。更具体地说,我想要实现的行为是这样的:
如果我在命令行中输入命令 create 我希望调用方法 generateMigrationFile,否则如果我输入的命令已运行我想运行我的迁移(SpringApplication.run 方法)。我怎样才能做到这一点?
我的应用主类
public static void main(String[] args) {
//if entered the command run in the command line
//SpringApplication.run(MigrationsApplication.class, args);
//if entered the command create in the command line
MigrationGenerator generator=new MigrationGenerator();
try {
generator.generateMigrationFile("TEST");
} catch (IOException e) {
System.out.println("There was an error generating the file");
}
我想这样称呼它:
migrationsApp run
migrationsApp create
【问题讨论】:
-
使用 arg 并解析它们...过于简单的例子
if(args[1].equals("generate")){ /* do generate code */ }else{ SpringApplication.run(MigrationsApplication.class, args); }或者你可以用另一种方式来做,如果参数 gui 被传递,则转到 GUI。对于操作系统而言,GUI 应用程序和 CLI 应用程序之间没有区别,只是 GUI 应用程序调用绘制 GUI,因为它运行 CLI 没有,应用程序被调用而不是从 CLI 或 GUI 调用跨度> -
我真的从没用过命令行,所以完整的命令应该是 java ApplicationName args 吧?但是,假设我想在使用 dotnet 命令的地方做类似 .Net 的事情。是否可以添加我想要的前缀(前缀我的意思是dotnet)
-
我会在问题中留下一个例子
标签: java command-line command