All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cinchapi.concourse.cli.CommandLineInterfaceRunner Maven / Gradle / Ivy

/*
 * Copyright (c) 2013-2017 Cinchapi Inc.
 *
 * Licensed 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 com.cinchapi.concourse.cli;

import java.lang.reflect.Constructor;
import java.text.MessageFormat;
import java.util.Arrays;

/**
 * A single runner that can execute any {@link CommandLineInterface} class that
 * is specified as the first argument. This class is designed to make it easier
 * to create CLI applications.
 * 

*

    *
  1. Write a CLI class that extends {@link CommandLineInterface}. The CLI must * define a public constructor that takes a single array of string arguments * (i.e. similar to a main method).
  2. *
  3. Write a wrapper script that invokes * com.cinchapi.concourse.cli.CommandLineInterfaceRunner <cli> <cli-args> * where {@code cli} is the fully name of the CLI class from step 1 and * {@code cli-args} are the arguments that should be passed to tht CLI
  4. *

    * NOTE: Be sure to add both CommandLineInterfaceRunner and the * CLI class from step 1 to the classpath you feed to the JVM in the shell * script. *

    *
*

* * @author Jeff Nelson */ @SuppressWarnings("unchecked") public final class CommandLineInterfaceRunner { /** * Run the appropriate CLI program * * @param args */ public static void main(String... args) { if(args.length == 0) { System.err.println( "ERROR: Please specify a " + "CommandLineInterface to run"); System.exit(1); } String name = args[0]; args = Arrays.copyOfRange(args, 1, args.length); Constructor constructor = null; try { Class clazz = Class.forName(name); constructor = (Constructor) clazz .getConstructor(String[].class); } catch (Exception e) { System.err.println(MessageFormat.format( "Cannot execute CommandLineInterface named {0}", name)); e.printStackTrace(); System.exit(1); } // Not worried about NPE because #constructor is guaranteed to be // initialized if we make it this far try { CommandLineInterface cli = constructor.newInstance((Object) args); System.exit(cli.run()); } catch (Exception e) { // At this point, the Exception is thrown from the CLI (i.e. the // user did not pass in a required arg, etc). if(e instanceof ReflectiveOperationException && e.getCause().getMessage() != null) { System.err.println(MessageFormat.format("ERROR: {0}", e.getCause().getMessage())); } System.exit(1); } } /** * A hook to run {@link CommandLineInterface clis} programmatically. * * @param clazz the CLI class * @param flags the flags to pass to the cli, formatted the same as they * would be on the command line */ public static void run(Class clazz, String flags) { String[] args0 = flags.split("\\s"); String[] args = new String[args0.length + 1]; args[0] = clazz.getName(); System.arraycopy(args0, 0, args, 1, args0.length); main(args); } private CommandLineInterfaceRunner() {} }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy