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

java.com.cedarsolutions.util.CommandLine Maven / Gradle / Ivy

Go to download

Gradle plugins and other functionality for use with a standardized build process.

There is a newer version: 0.9.8
Show newest version
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *              C E D A R
 *          S O L U T I O N S       "Software done right."
 *           S O F T W A R E
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Copyright (c) 2013 Kenneth J. Pronovici.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Apache License, Version 2.0.
 * See LICENSE for more information about the licensing terms.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Author   : Kenneth J. Pronovici 
 * Language : Java 6
 * Project  : Common Java Functionality
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package com.cedarsolutions.util;

import java.util.ArrayList;
import java.util.List;

/**
 * Command line to be executed by CommandLineUtils.execute().
 * This class is duplicated from CedarCommon for build purposes.
 * @author Kenneth J. Pronovici 
 */
public class CommandLine {

    /** List of elements in the command line. */
    private List list = new ArrayList();

    /**
     * Create a command line, initializing it with a string command.
     * @param command  String command, the program to be executed
     */
    public CommandLine(String command) {
        list.add(command);
    }

    /**
     * Create a command line, initializing it with a command and arguments.
     * @param command  String command, the program to be executed
     * @param args     String arguments to the command
     */
    public CommandLine(String command, String ... args) {
        list.add(command);
        if (args != null && args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                list.add(args[i]);
            }
        }
    }

    /**
     * Create a command line, initializing it with a command and arguments.
     * @param command  String command, the program to be executed
     * @param args     String arguments to the command
     */
    public CommandLine(String command, List args) {
        list.add(command);
        list.addAll(args);
    }

    /** Get the command, the first element in the command line. */
    public String getCommand() {
        return this.list.size() >= 1 ? list.get(0) : null;
    }

    /** Get the arguments to the command. */
    public List getArgs() {
        List arguments = new ArrayList();

        if (this.list.size() > 1) {
            for (int i = 1; i < list.size(); i++) {
                arguments.add(list.get(i));
            }
        }

        return arguments;
    }

    /** Add an argument to the list of arguments. */
    public void addArg(String arg) {
        this.list.add(arg);
    }

    /** Get the entire command, suitable for passing to the constructor for ProcessBuilder. */
    public List getEntireCommand() {
        return new ArrayList(this.list);
    }

    /** Return the command-line as a string. */
    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer();

        for (String element : this.list) {
            buffer.append(element);
            buffer.append(" ");
        }

        return StringUtils.trim(buffer.toString());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy