Class Commandline

java.lang.Object
org.codehaus.plexus.util.cli.Commandline
All Implemented Interfaces:
Cloneable

public class Commandline extends Object implements Cloneable

Commandline objects help handling command lines specifying processes to execute.

The class can be used to define a command line as nested elements or as a helper to define a command line by an application.

<someelement>
  <acommandline executable="/executable/to/run">
    <argument value="argument 1" />
    <argument line="argument_1 argument_2 argument_3" />
    <argument value="argument 4" />
  </acommandline>
</someelement>

The element someelement must provide a method createAcommandline which returns an instance of this class.

  • Field Details

  • Constructor Details

    • Commandline

      public Commandline(String toProcess, Shell shell)
      Create a new command line object. Shell is autodetected from operating system Shell usage is only desirable when generating code for remote execution.
      Parameters:
      toProcess -
    • Commandline

      public Commandline(Shell shell)
      Create a new command line object. Shell is autodetected from operating system Shell usage is only desirable when generating code for remote execution.
    • Commandline

      public Commandline(String toProcess)
      Create a new command line object, given a command following POSIX sh quoting rules
      Parameters:
      toProcess -
    • Commandline

      public Commandline()
      Create a new command line object.
  • Method Details

    • getPid

      public long getPid()
    • setPid

      public void setPid(long pid)
    • setDefaultShell

      private void setDefaultShell()

      Sets the shell or command-line interpreter for the detected operating system, and the shell arguments.

    • createArgument

      public Commandline.Argument createArgument()
      Deprecated.
      Use createArg() instead

      Creates an argument object.

      Each commandline object has at most one instance of the argument class. This method calls this.createArgument(false).

      Returns:
      the argument object.
      See Also:
    • createArgument

      public Commandline.Argument createArgument(boolean insertAtStart)
      Deprecated.
      Use createArg(boolean) instead

      Creates an argument object and adds it to our list of args.

      Each commandline object has at most one instance of the argument class.

      Parameters:
      insertAtStart - if true, the argument is inserted at the beginning of the list of args, otherwise it is appended.
    • createArg

      public Arg createArg()

      Creates an argument object.

      Each commandline object has at most one instance of the argument class. This method calls this.createArgument(false).

      Returns:
      the argument object.
      See Also:
    • createArg

      public Arg createArg(boolean insertAtStart)

      Creates an argument object and adds it to our list of args.

      Each commandline object has at most one instance of the argument class.

      Parameters:
      insertAtStart - if true, the argument is inserted at the beginning of the list of args, otherwise it is appended.
    • addArg

      public void addArg(Arg argument)
      Adds an argument object to our list of args.
      See Also:
    • addArg

      public void addArg(Arg argument, boolean insertAtStart)
      Adds an argument object to our list of args.
      Parameters:
      insertAtStart - if true, the argument is inserted at the beginning of the list of args, otherwise it is appended.
    • setExecutable

      public void setExecutable(String executable)
      Sets the executable to run.
    • getLiteralExecutable

      public String getLiteralExecutable()
      Returns:
      Executable to be run, as a literal string (no shell quoting/munging)
    • getExecutable

      public String getExecutable()
      Return an executable name, quoted for shell use. Shell usage is only desirable when generating code for remote execution.
      Returns:
      Executable to be run, quoted for shell interpretation
    • addArguments

      public void addArguments(String[] line)
    • addEnvironment

      public void addEnvironment(String name, String value)
      Add an environment variable
    • addSystemEnvironment

      public void addSystemEnvironment() throws Exception
      Add system environment variables
      Throws:
      Exception
    • getEnvironmentVariables

      public String[] getEnvironmentVariables() throws CommandLineException
      Return the list of environment variables
      Throws:
      CommandLineException
    • getCommandline

      public String[] getCommandline()
      Returns the executable and all defined arguments.
      For Windows Family, getShellCommandline() is returned
    • getRawCommandline

      public String[] getRawCommandline()
      Returns the executable and all defined arguments.
    • getShellCommandline

      public String[] getShellCommandline()
      Returns the shell, executable and all defined arguments. Shell usage is only desirable when generating code for remote execution.
    • getArguments

      public String[] getArguments()
      Returns all arguments defined by addLine, addValue or the argument object.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • size

      public int size()
    • clone

      public Object clone()
      Overrides:
      clone in class Object
    • clear

      public void clear()
      Clear out the whole command line.
    • clearArgs

      public void clearArgs()
      Clear out the arguments but leave the executable in place for another operation.
    • createMarker

      public Commandline.Marker createMarker()

      Return a marker.

      This marker can be used to locate a position on the commandline - to insert something for example - when all parameters have been set.

    • setWorkingDirectory

      public void setWorkingDirectory(String path)
      Sets execution directory.
    • setWorkingDirectory

      public void setWorkingDirectory(File workingDirectory)
      Sets execution directory.
    • getWorkingDirectory

      public File getWorkingDirectory()
    • execute

      public Process execute() throws CommandLineException
      Executes the command.
      Throws:
      CommandLineException
    • verifyShellState

      private void verifyShellState()
      Deprecated.
      Remove once backward compat with plexus-utils <= 1.4 is no longer a consideration
    • getSystemEnvVars

      public Properties getSystemEnvVars() throws Exception
      Throws:
      Exception
    • setShell

      public void setShell(Shell shell)
      Allows to set the shell to be used in this command line. Shell usage is only desirable when generating code for remote execution.
      Parameters:
      shell -
      Since:
      1.2
    • getShell

      public Shell getShell()
      Get the shell to be used in this command line. Shell usage is only desirable when generating code for remote execution.
      Since:
      1.2
    • translateCommandline

      public static String[] translateCommandline(String toProcess) throws Exception
      Throws:
      Exception
    • quoteArgument

      public static String quoteArgument(String argument) throws CommandLineException
      Deprecated.
      Throws:
      CommandLineException
    • toString

      public static String toString(String[] line)
      Deprecated.