Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
Open man page when calling --help
Browse files Browse the repository at this point in the history
  • Loading branch information
binwiederhier committed Jun 13, 2014
1 parent f3911ac commit dddd5eb
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions syncany-cli/src/main/java/org/syncany/cli/CommandLineClient.java
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit dddd5eb

Please sign in to comment.