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

Feature Suggestion: exec method #728

Closed
yschimke opened this issue Jun 16, 2019 · 8 comments
Closed

Feature Suggestion: exec method #728

yschimke opened this issue Jun 16, 2019 · 8 comments

Comments

@yschimke
Copy link

This method seems like a useful default to provide within the library. Covers help, shell completion hooks, error handling etc.

https:/rsocket/rsocket-cli/blob/master/src/main/kotlin/io/rsocket/cli/Main.kt#L363

private fun exec(runnable: Main, args: List<String>) {
      val cmd = CommandLine(runnable)
      try {
        val parseResult = cmd.parseArgs(*args.toTypedArray())

        if (cmd.isUsageHelpRequested) {
          cmd.usage(cmd.out)
          exitProcess(parseResult.commandSpec().exitCodeOnUsageHelp())
        } else if (cmd.isVersionHelpRequested) {
          cmd.printVersionHelp(cmd.out)
          exitProcess(parseResult.commandSpec().exitCodeOnVersionHelp())
        }

        if (runnable.completionScript) {
          print(picocli.AutoComplete.bash("rsocket-cli", cmd))
        } else {
          runnable.run()
        }

        exitProcess(0)
      } catch (pe: CommandLine.ParameterException) {
        cmd.err.println(pe.message)
        if (!CommandLine.UnmatchedArgumentException.printSuggestions(pe, cmd.err)) {
          cmd.usage(cmd.err)
        }
        exitProcess(cmd.commandSpec.exitCodeOnInvalidInput())
      } catch (ue: UsageException) {
        cmd.err.println(ue.message)
        cmd.usage(cmd.err)
        exitProcess(cmd.commandSpec.exitCodeOnInvalidInput())
      } catch (ex: Exception) {
        ex.printStackTrace(cmd.err)
        exitProcess(cmd.commandSpec.exitCodeOnExecutionException())
      }
}
@yschimke
Copy link
Author

@remkop I might start with a PR for this function. On the other random suggestions, let me know if you are keen and we can dicsuss on those issues what the APIs could look like and I'll contribute some PRs over time.

@yschimke
Copy link
Author

Just found CommandLine.execute()

@remkop
Copy link
Owner

remkop commented Jun 16, 2019

Oh, I see, I was looking at rsocket/rsocket-cli@099792f and wondering if there was a problem with CommandLine.execute() that you needed to work around. 😟
Mystery solved! 😄

@yschimke
Copy link
Author

It would be useful to have some of the value adds supported without additional main entry points e.g. generating the completion file. Would make installations simpler as they could call the app on the fly. But nope, all good. Just missed it.

@yschimke
Copy link
Author

Is it mentioned in the getting started docs? It’s a useful first approach.

@remkop
Copy link
Owner

remkop commented Jun 16, 2019

The execute API is relatively new (previously there were run and call static methods, but these didn’t have exit code support).

I added a new section to the user manual. Maybe I should add additional pointers.
Where would be a good place? The readme?

@yschimke
Copy link
Author

I think as the first item for any user facing docs. The minimal example should be that method and a main class runnable with a few options.

@remkop
Copy link
Owner

remkop commented Jun 16, 2019

Makes sense. I updated the README, will also update the javadoc for CommandLine and any other place I find.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants