From: <otm...@us...> - 2008-10-16 23:25:34
|
Revision: 5451 http://jython.svn.sourceforge.net/jython/?rev=5451&view=rev Author: otmarhumbel Date: 2008-10-16 23:25:28 +0000 (Thu, 16 Oct 2008) Log Message: ----------- fixed auto switching to console mode Modified Paths: -------------- trunk/installer/src/java/org/python/util/install/InstallerCommandLine.java trunk/installer/test/java/org/python/util/install/InstallerCommandLineTest.java Modified: trunk/installer/src/java/org/python/util/install/InstallerCommandLine.java =================================================================== --- trunk/installer/src/java/org/python/util/install/InstallerCommandLine.java 2008-10-16 22:20:26 UTC (rev 5450) +++ trunk/installer/src/java/org/python/util/install/InstallerCommandLine.java 2008-10-16 23:25:28 UTC (rev 5451) @@ -21,13 +21,13 @@ protected static final String INEXCLUDE_DOCUMENTATION = "doc"; protected static final String INEXCLUDE_SOURCES = "src"; - private static final String CONSOLE_SHORT = "c"; - private static final String CONSOLE_LONG = "console"; + protected static final String CONSOLE_SHORT = "c"; + protected static final String CONSOLE_LONG = "console"; private static final String CONSOLE_DESC = "console based installation (user interaction)\n" + "any other options will be ignored (except 'verbose')"; - private static final String SILENT_SHORT = "s"; - private static final String SILENT_LONG = "silent"; + protected static final String SILENT_SHORT = "s"; + protected static final String SILENT_LONG = "silent"; private static final String SILENT_DESC = "silent installation (without user interaction)"; protected static final String VERBOSE_SHORT = "v"; @@ -130,15 +130,32 @@ public static final boolean hasVerboseOptionInArgs(String[] args) { String shortVerbose = "-".concat(VERBOSE_SHORT); String longVerbose = "--".concat(VERBOSE_LONG); - for (String arg : args) { - if (shortVerbose.equals(arg) || longVerbose.equals(arg)) { - return true; - } - } - return false; + return hasOptionInArgs(args, shortVerbose, longVerbose); } /** + * Pre-scan of the arguments to detect a console flag + * @param args + * @return <code>true</code> if there is a console option + */ + public static final boolean hasConsoleOptionInArgs(String[] args) { + String shortConsole = "-".concat(CONSOLE_SHORT); + String longConsole = "--".concat(CONSOLE_LONG); + return hasOptionInArgs(args, shortConsole, longConsole); + } + + /** + * Pre-scan of the arguments to detect a silent flag + * @param args + * @return <code>true</code> if there is a silent option + */ + public static final boolean hasSilentOptionInArgs(String[] args) { + String shortSilent = "-".concat(SILENT_SHORT); + String longSilent = "--".concat(SILENT_LONG); + return hasOptionInArgs(args, shortSilent, longSilent); + } + + /** * constructor intended for JUnit tests only. */ public InstallerCommandLine() { @@ -154,14 +171,20 @@ */ public boolean setArgs(String args[]) { _args = args; - if (args.length == 0) { - // switch to console mode if gui is not allowed - if (!Installation.isGuiAllowed()) { - _args = new String[] { "-" + CONSOLE_SHORT }; + if (!hasConsoleOptionInArgs(args) && !hasSilentOptionInArgs(args) + && !Installation.isGuiAllowed()) { + // auto switch to console mode + if (hasVerboseOptionInArgs(args)) { + ConsoleInstaller.message("auto-switching to console mode"); } + String[] newArgs = new String[args.length + 1]; + System.arraycopy(args, 0, newArgs, 0, args.length); + newArgs[args.length] = "-" + CONSOLE_SHORT; + _args = newArgs; } try { - _commandLine = _parser.parse(_options, _args, false); // throw for missing or unknown options / arguments + // throws for missing or unknown options / arguments + _commandLine = _parser.parse(_options, _args, false); } catch (MissingArgumentException mae) { System.err.println(mae.getMessage()); return false; @@ -353,6 +376,18 @@ // // private methods // + + private static final boolean hasOptionInArgs(String[] args, String shortOption, String longOption) { + boolean hasOption = false; + int i = 0; + while (!hasOption && i < args.length) { + if (shortOption.equals(args[i]) || longOption.equals(args[i])) { + hasOption = true; + } + i++; + } + return hasOption; + } private void createOptions() { _options = new Options(); Modified: trunk/installer/test/java/org/python/util/install/InstallerCommandLineTest.java =================================================================== --- trunk/installer/test/java/org/python/util/install/InstallerCommandLineTest.java 2008-10-16 22:20:26 UTC (rev 5450) +++ trunk/installer/test/java/org/python/util/install/InstallerCommandLineTest.java 2008-10-16 23:25:28 UTC (rev 5451) @@ -1,5 +1,7 @@ package org.python.util.install; +import java.io.File; + import junit.framework.TestCase; public class InstallerCommandLineTest extends TestCase { @@ -203,7 +205,7 @@ assertTrue(commandLine.hasConsoleOption()); } - public void testHeadless() { + public void testGui() { String[] args; InstallerCommandLine commandLine; @@ -214,24 +216,64 @@ assertTrue(commandLine.setArgs(args)); assertFalse(commandLine.hasConsoleOption()); assertFalse(commandLine.hasSilentOption()); - - // simulate startup without any arguments on a headless system + } + + /** + * simulate startup on a headless system (auto-switch to console mode) + */ + public void testHeadless() { + String[] args; + InstallerCommandLine commandLine; boolean originalHeadless = Boolean.getBoolean(Installation.HEADLESS_PROPERTY_NAME); try { - if(!originalHeadless) { + if (!originalHeadless) { System.setProperty(Installation.HEADLESS_PROPERTY_NAME, "true"); - assertFalse(Installation.isGuiAllowed()); - args = new String[0]; - commandLine = new InstallerCommandLine(); - assertTrue(commandLine.setArgs(args)); - assertTrue(commandLine.hasConsoleOption()); - assertFalse(commandLine.hasSilentOption()); } + assertFalse(Installation.isGuiAllowed()); + + // without any arguments + args = new String[0]; + commandLine = new InstallerCommandLine(); + assertTrue(commandLine.setArgs(args)); + assertTrue(commandLine.hasConsoleOption()); // auto switch + assertFalse(commandLine.hasSilentOption()); + + // with one argument + args = new String[] {"-v"}; + commandLine = new InstallerCommandLine(); + assertTrue(commandLine.setArgs(args)); + assertTrue(commandLine.hasVerboseOption()); + assertTrue(commandLine.hasConsoleOption()); // auto switch + assertFalse(commandLine.hasSilentOption()); + + // with more arguments + args = new String[] {"-v", "-t", "minimum" }; + commandLine = new InstallerCommandLine(); + assertTrue(commandLine.setArgs(args)); + assertTrue(commandLine.hasVerboseOption()); + assertTrue(commandLine.hasConsoleOption()); // auto switch + assertFalse(commandLine.hasSilentOption()); + assertTrue(commandLine.hasTypeOption()); + InstallationType type = commandLine.getInstallationType(); + assertNotNull(type); + assertTrue(type.isMinimum()); + + // silent should override! + args = new String[] {"-v", "-s", "-d", "some_dir"}; + commandLine = new InstallerCommandLine(); + assertTrue(commandLine.setArgs(args)); + assertTrue(commandLine.hasVerboseOption()); + assertFalse(commandLine.hasConsoleOption()); // no auto switch + assertTrue(commandLine.hasSilentOption()); + assertTrue(commandLine.hasDirectoryOption()); + File dir = commandLine.getTargetDirectory(); + assertNotNull(dir); + assertEquals("some_dir", dir.getName()); } finally { - if(!originalHeadless) { + if (!originalHeadless) { System.setProperty(Installation.HEADLESS_PROPERTY_NAME, "false"); + assertTrue(Installation.isGuiAllowed()); } - assertTrue(Installation.isGuiAllowed()); } } @@ -533,5 +575,51 @@ args = new String[] {"a", "--" + InstallerCommandLine.VERBOSE_LONG, "c"}; assertTrue(InstallerCommandLine.hasVerboseOptionInArgs(args)); } + + public void testHasConsoleOptionInArgs() { + String[] args = new String[0]; + assertFalse(InstallerCommandLine.hasConsoleOptionInArgs(args)); + + args = new String[] {"a", "b", "c"}; + assertFalse(InstallerCommandLine.hasConsoleOptionInArgs(args)); + + args = new String[] {"a", InstallerCommandLine.CONSOLE_SHORT, "c"}; + assertFalse(InstallerCommandLine.hasConsoleOptionInArgs(args)); + + args = new String[] {"a", "-" + InstallerCommandLine.CONSOLE_SHORT, "c"}; + assertTrue(InstallerCommandLine.hasConsoleOptionInArgs(args)); + + args = new String[] {"a", InstallerCommandLine.CONSOLE_LONG, "c"}; + assertFalse(InstallerCommandLine.hasConsoleOptionInArgs(args)); + + args = new String[] {"a", "-" + InstallerCommandLine.CONSOLE_LONG, "c"}; + assertFalse(InstallerCommandLine.hasConsoleOptionInArgs(args)); + + args = new String[] {"a", "--" + InstallerCommandLine.CONSOLE_LONG, "c"}; + assertTrue(InstallerCommandLine.hasConsoleOptionInArgs(args)); + } + + public void testHasSilentOptionInArgs() { + String[] args = new String[0]; + assertFalse(InstallerCommandLine.hasSilentOptionInArgs(args)); + + args = new String[] {"a", "b", "c"}; + assertFalse(InstallerCommandLine.hasSilentOptionInArgs(args)); + + args = new String[] {"a", InstallerCommandLine.SILENT_SHORT, "c"}; + assertFalse(InstallerCommandLine.hasSilentOptionInArgs(args)); + + args = new String[] {"a", "-" + InstallerCommandLine.SILENT_SHORT, "c"}; + assertTrue(InstallerCommandLine.hasSilentOptionInArgs(args)); + + args = new String[] {"a", InstallerCommandLine.SILENT_LONG, "c"}; + assertFalse(InstallerCommandLine.hasSilentOptionInArgs(args)); + + args = new String[] {"a", "-" + InstallerCommandLine.SILENT_LONG, "c"}; + assertFalse(InstallerCommandLine.hasSilentOptionInArgs(args)); + + args = new String[] {"a", "--" + InstallerCommandLine.SILENT_LONG, "c"}; + assertTrue(InstallerCommandLine.hasSilentOptionInArgs(args)); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |