Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print help usage before spring boot application startup #1406

Closed
bonlai opened this issue Aug 12, 2021 · 2 comments
Closed

Print help usage before spring boot application startup #1406

bonlai opened this issue Aug 12, 2021 · 2 comments
Labels
theme: integration An issue or change related to integration with other frameworks, shells or operating systems type: question ❔

Comments

@bonlai
Copy link

bonlai commented Aug 12, 2021

Hi,

My spring boot application startup process take about 7 second to startup.
I need to wait 7 seconds for the help usage to be printed out.
However, it is not necessary for the help usage waiting for the spring boot stuffs to be initialized.

Is it possible to print the help usage and exit before spring boot application to startup?

Thanks.

@remkop
Copy link
Owner

remkop commented Aug 12, 2021

One idea is to do parsing in two phases:

  • first only look for --help (and --version) and ignore all other options and positional parameters
  • next, parse again, this time with the "real" application and all other options and positional parameters

Example

@Command(name = "mycommand", mixinStandardHelpOptions = true, version = "mycommand 1.0",
         exitCodeOnUsageHelp = 3, exitCodeOnVersionHelp = 3,
         description = "...")
class Dummy {
  @Unmatched List<String> ignored;
}

@Command(name = "mycommand", mixinStandardHelpOptions = true, version = "mycommand 1.0",
         description = "...")
class ActualCommand implements Runnable {
  @Option(names = "-x", description = "some actual option..."
  int x;

  public static void main(String[] args) {
    // phase 1: parse without initializing any Spring beans
    int exitCode = new CommandLine(new Dummy()).execute(args);
    if (exitCode == 3) {
      return; // the user requested help or version info 
    }
    // phase 2: initialize the Spring beans before parsing
    System.exit(SpringApplication.exit(SpringApplication.run(ActualCommand.class, args)));
  }
}

@remkop remkop added theme: integration An issue or change related to integration with other frameworks, shells or operating systems type: question ❔ labels Aug 12, 2021
@bonlai
Copy link
Author

bonlai commented Aug 13, 2021

Solved my problem. Many thanks!

@bonlai bonlai closed this as completed Aug 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: integration An issue or change related to integration with other frameworks, shells or operating systems type: question ❔
Projects
None yet
Development

No branches or pull requests

2 participants