From dddd5eb1239f7f8c9a5d4eda928cea380e4edbfe Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Fri, 13 Jun 2014 20:03:49 +0200 Subject: [PATCH] Open man page when calling --help --- .../org/syncany/cli/CommandLineClient.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/syncany-cli/src/main/java/org/syncany/cli/CommandLineClient.java b/syncany-cli/src/main/java/org/syncany/cli/CommandLineClient.java index cabc61ad4..adf751be2 100644 --- a/syncany-cli/src/main/java/org/syncany/cli/CommandLineClient.java +++ b/syncany-cli/src/main/java/org/syncany/cli/CommandLineClient.java @@ -46,6 +46,7 @@ import org.syncany.config.ConfigHelper; import org.syncany.config.LogFormatter; import org.syncany.config.Logging; +import org.syncany.util.EnvironmentUtil; /** * The command line client implements a typical CLI. It represents the first entry @@ -68,6 +69,9 @@ public class CommandLineClient extends Client { private static final String HELP_TEXT_VERSION_FULL_SKEL_RESOURCE = "incl/version_full.skel"; private static final String HELP_TEXT_USAGE_SKEL_RESOURCE = "incl/usage.skel"; private static final String HELP_TEXT_CMD_SKEL_RESOURCE = "cmd/help.%s.skel"; + + private static final String MAN_PAGE_MAIN = "sy"; + private static final String MAN_PAGE_COMMAND_FORMAT = "sy-%s"; private String[] args; private File localDir; @@ -306,14 +310,43 @@ private void showUsageAndExit() throws IOException { } private void showHelpAndExit() throws IOException { + // Try opening man page (if on Linux) + if (EnvironmentUtil.isUnixLikeOperatingSystem()) { + execManPageAndExit(MAN_PAGE_MAIN); + } + + // Fallback (and on Windows): Display man page on STDOUT printHelpTextAndExit(HELP_TEXT_HELP_SKEL_RESOURCE); } private int showCommandHelpAndExit(String commandName) throws IOException { + // Try opening man page (if on Linux) + if (EnvironmentUtil.isUnixLikeOperatingSystem()) { + String commandManPage = String.format(MAN_PAGE_COMMAND_FORMAT, commandName); + execManPageAndExit(commandManPage); + } + + // Fallback (and on Windows): Display man page on STDOUT String helpTextResource = String.format(HELP_TEXT_CMD_SKEL_RESOURCE, commandName); return printHelpTextAndExit(helpTextResource); } + private void execManPageAndExit(String manPage) { + try { + Runtime runtime = Runtime.getRuntime(); + Process manProcess = runtime.exec(new String[] { "sh", "-c", "man " + manPage + " > /dev/tty" }); + + int manProcessExitCode = manProcess.waitFor(); + + if (manProcessExitCode == 0) { + System.exit(0); + } + } + catch (Exception e) { + // Don't care! + } + } + private int printHelpTextAndExit(String helpTextResource) throws IOException { String fullHelpTextResource = HELP_TEXT_RESOURCE_ROOT + helpTextResource; InputStream helpTextInputStream = CommandLineClient.class.getResourceAsStream(fullHelpTextResource);