/**
   * *************************************************************************** {@link Runnable}
   * Interface **************************************************************************
   */
  public void run(String[] args) throws IOException {

    if (needsHelp(args)) {
      printHelp();
      System.exit(0);
    }

    try {
      CommandLine cli = _parser.parse(_options, args, true);
      runApplication(cli, args);
    } catch (MissingOptionException ex) {
      System.err.println("Missing argument: " + ex.getMessage());
      printHelp();
    } catch (MissingArgumentException ex) {
      System.err.println("Missing argument: " + ex.getMessage());
      printHelp();
    } catch (UnrecognizedOptionException ex) {
      System.err.println("Unknown argument: " + ex.getMessage());
      printHelp();
    } catch (AlreadySelectedException ex) {
      System.err.println("Argument already selected: " + ex.getMessage());
      printHelp();
    } catch (ParseException ex) {
      System.err.println(ex.getMessage());
      printHelp();
    } catch (TransformSpecificationException ex) {
      System.err.println("error with transform line: " + ex.getLine());
      System.err.println(ex.getMessage());
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
Example #2
0
 void parseCommandLine(String[] args) {
   // @formatter:on
   CommandLineParser parser = new DefaultParser();
   try {
     commandline = parser.parse(options, args);
   } catch (AlreadySelectedException ase) {
     help(CliBase.InvalidCommandLine, "Already Selected: " + ase.getOption());
   } catch (AmbiguousOptionException aoe) {
     help(CliBase.InvalidCommandLine, "Ambiguous Option: " + aoe.getMatchingOptions());
   } catch (MissingArgumentException mae) {
     help(CliBase.InvalidCommandLine, "Missing Argument: " + mae.getOption());
   } catch (MissingOptionException moe) {
     help(CliBase.InvalidCommandLine, "Missing Option: " + moe.getMissingOptions());
   } catch (UnrecognizedOptionException uoe) {
     help(CliBase.InvalidCommandLine, "Unrecongnized Option: " + uoe.getOption());
   } catch (Exception e) {
     help(CliBase.InvalidCommandLine, "Unexpected Exception: " + e.getClass().getName());
   }
   if (commandline.hasOption(CliBase.HelpShortCl)) help(0, null);
   if (commandline.hasOption(CliBase.VersionShortCl)) cliBase.version();
 }
	@SuppressWarnings("static-access")
	public static void main(String[] args) {
		try {
			final String vers = System.getProperty("java.version");
			final String requiredJVM = "1.5.0";
			final Package self = Package
					.getPackage("org.openscience.jchempaint");
			String version = GT._("Could not determine JCP version");
			if (self != null) {
				version = JCPPropertyHandler.getInstance(true).getVersion();
			}
			if (vers.compareTo(requiredJVM) < 0) {
				System.err
						.println(GT
								._("WARNING: JChemPaint {0} must be run with a Java VM version {1} or higher.",
										new String[] { version, requiredJVM }));
				System.err.println(GT._("Your JVM version is {0}", vers));
				System.exit(1);
			}

			final Options options = new Options();
			options.addOption("h", "help", false, GT._("gives this help page"));
			options.addOption("v", "version", false,
					GT._("gives JChemPaints version number"));
			options.addOption("d", "debug", false,
					"switches on various debug options");
			options.addOption(OptionBuilder.withArgName("property=value")
					.hasArg().withValueSeparator()
					.withDescription(GT._("supported options are given below"))
					.create("D"));

			CommandLine line = null;
			try {
				final CommandLineParser parser = new PosixParser();
				line = parser.parse(options, args);
			} catch (final UnrecognizedOptionException exception) {
				System.err.println(exception.getMessage());
				System.exit(-1);
			} catch (final ParseException exception) {
				System.err.println("Unexpected exception: "
						+ exception.toString());
			}

			if (line.hasOption("v")) {
				System.out.println("JChemPaint v." + version + "\n");
				System.exit(0);
			}

			if (line.hasOption("h")) {
				System.out.println("JChemPaint v." + version + "\n");

				final HelpFormatter formatter = new HelpFormatter();
				formatter.printHelp("JChemPaint", options);

				// now report on the -D options
				System.out.println();
				System.out
						.println("The -D options are as follows (defaults in parathesis):");
				System.out.println("  cdk.debugging     [true|false] (false)");
				System.out.println("  cdk.debug.stdout  [true|false] (false)");
				System.out
						.println("  user.language     [ar|ca|cs|de|en|es|hu|nb|nl|pl|pt|ru|th] (en)");
				System.out
						.println("  user.language     [ar|ca|cs|de|hu|nb|nl|pl|pt_BR|ru|th] (EN)");

				System.exit(0);
			}
			boolean debug = false;
			if (line.hasOption("d")) {
				debug = true;
			}

			// Set Look&Feel
			final Properties props = JCPPropertyHandler.getInstance(true)
					.getJCPProperties();
			try {
				UIManager.setLookAndFeel(props.getProperty("LookAndFeelClass"));
			} catch (final Throwable e) {
				final String sys = UIManager.getSystemLookAndFeelClassName();
				UIManager.setLookAndFeel(sys);
				props.setProperty("LookAndFeelClass", sys);
			}

			// Language
			props.setProperty("General.language",
					System.getProperty("user.language", "en"));

			// Process command line arguments
			String modelFilename = "";
			args = line.getArgs();
			if (args.length > 0) {
				modelFilename = args[0];
				final File file = new File(modelFilename);
				if (!file.exists()) {
					System.err.println(GT._("File does not exist") + ": "
							+ modelFilename);
					System.exit(-1);
				}
				showInstance(file, null, null, debug);
			} else {
				showEmptyInstance(debug);
			}

		} catch (final Throwable t) {
			System.err.println("uncaught exception: " + t);
			t.printStackTrace(System.err);
		}
	}