org.apache.sis.console.Command Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.sis.console;
import java.util.Locale;
import java.io.Console;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.sql.SQLException;
import org.opengis.referencing.operation.TransformException;
import org.apache.sis.storage.DataStoreException;
import org.apache.sis.util.resources.Errors;
import org.apache.sis.util.logging.MonolineFormatter;
/**
* Command line interface for Apache SIS. The {@link #main(String[])} method accepts the following commands:
*
*
* {@code help} Show a help overview.
* {@code about} Show information about Apache SIS and system configuration.
* {@code mime-type} Show MIME type for the given file.
* {@code metadata} Show metadata information for the given file.
*
*
* Each command can accepts an arbitrary amount of the following options:
*
*
* {@code --format} The output format: {@code xml}, {@code wkt}, {@code wkt1} or {@code text}.
* {@code --locale} The locale to use for the command output.
* {@code --timezone} The timezone for the dates to be formatted.
* {@code --encoding} The encoding to use for the command output.
* {@code --colors} Whether colorized output shall be enabled.
* {@code --brief} Whether the output should contains only brief information.
* {@code --verbose} Whether the output should contains more detailed information.
* {@code --help} Lists the options available for a specific command.
*
*
* The {@code --locale}, {@code --timezone} and {@code --encoding} options apply to the command output sent
* to the {@linkplain System#out standard output stream}, but usually do not apply to the error messages sent
* to the {@linkplain System#err standard error stream}. The reason is that command output may be targeted to
* a client, while the error messages are usually for the operator.
*
* {@section SIS installation on remote machines}
* Some sub-commands can operate on SIS installation on remote machines, provided that remote access has been enabled
* at the Java Virtual Machine startup time. See {@link org.apache.sis.console} package javadoc for more information.
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.3
* @version 0.4
* @module
*/
public final class Command {
/**
* The code given to {@link System#exit(int)} when the program failed because of a unknown sub-command.
*/
public static final int INVALID_COMMAND_EXIT_CODE = 1;
/**
* The code given to {@link System#exit(int)} when the program failed because of a unknown option.
* The set of valid options depend on the sub-command to execute.
*/
public static final int INVALID_OPTION_EXIT_CODE = 2;
/**
* The code given to {@link System#exit(int)} when the program failed because of an illegal user argument.
* The user arguments are everything which is not a command name or an option. They are typically file names,
* but can occasionally be other types like URL.
*/
public static final int INVALID_ARGUMENT_EXIT_CODE = 3;
/**
* The code given to {@link System#exit(int)} when a file given in argument uses an unknown file format.
*/
public static final int UNKNOWN_STORAGE_EXIT_CODE = 4;
/**
* The code given to {@link System#exit(int)} when the program failed because of an {@link java.io.IOException}.
*/
public static final int IO_EXCEPTION_EXIT_CODE = 100;
/**
* The code given to {@link System#exit(int)} when the program failed because of an {@link java.sql.SQLException}.
*/
public static final int SQL_EXCEPTION_EXIT_CODE = 101;
/**
* The code given to {@link System#exit(int)} when the program failed for a reason
* other than the ones enumerated in the above constants.
*/
public static final int OTHER_ERROR_EXIT_CODE = 199;
/**
* The sub-command name.
*/
private final String commandName;
/**
* The sub-command to execute.
*/
private final SubCommand command;
/**
* Creates a new command for the given arguments. The first value in the given array which is
* not an option is taken as the command name. All other values are options or filenames.
*
* @param args The command-line arguments.
* @throws InvalidCommandException If an invalid command has been given.
* @throws InvalidOptionException If the given arguments contain an invalid option.
*/
protected Command(final String[] args) throws InvalidCommandException, InvalidOptionException {
int commandIndex = -1;
String commandName = null;
for (int i=0; i