Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2003-2007 the original author or authors.
*
* 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 groovy.ui;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import java.io.*;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.runtime.InvokerInvocationException;
/**
* A Command line to execute groovy.
*
* @author Jeremy Rayner
* @author Yuri Schimke
* @version $Revision: 7978 $
*/
public class GroovyMain {
// arguments to the script
private List args;
// is this a file on disk
private boolean isScriptFile;
// filename or content of script
private String script;
// process args as input files
private boolean processFiles;
// edit input files in place
private boolean editFiles;
// automatically output the result of each script
private boolean autoOutput;
// automatically split each line using the splitpattern
private boolean autoSplit;
// The pattern used to split the current line
private String splitPattern = " ";
// process sockets
private boolean processSockets;
// port to listen on when processing sockets
private int port;
// backup input files with extension
private String backupExtension;
// do you want full stack traces in script exceptions?
private boolean debug = false;
// Compiler configuration, used to set the encodings of the scripts/classes
private CompilerConfiguration conf = new CompilerConfiguration();
/**
* Main CLI interface.
*
* @param args all command line args.
*/
public static void main(String args[]) {
processArgs(args, System.out);
}
// package-level visibility for testing purposes (just usage/errors at this stage)
// TODO: should we have an 'err' printstream too for ParseException?
static void processArgs(String[] args, final PrintStream out) {
Options options = buildOptions();
try {
CommandLine cmd = parseCommandLine(options, args);
if (cmd.hasOption('h')) {
printHelp(out, options);
} else if (cmd.hasOption('v')) {
String version = InvokerHelper.getVersion();
out.println("Groovy Version: " + version + " JVM: " + System.getProperty("java.vm.version"));
} else {
// If we fail, then exit with an error so scripting frameworks can catch it
// TODO: pass printstream(s) down through process
if (!process(cmd)) {
System.exit(1);
}
}
} catch (ParseException pe) {
out.println("error: " + pe.getMessage());
printHelp(out, options);
}
}
private static void printHelp(PrintStream out, Options options) {
HelpFormatter formatter = new HelpFormatter();
PrintWriter pw = new PrintWriter(out);
formatter.printHelp(
pw,
80,
"groovy [options] [args]",
"options:",
options,
2,
4,
null, // footer
false);
pw.flush();
}
/**
* Parse the command line.
*
* @param options the options parser.
* @param args the command line args.
* @return parsed command line.
* @throws ParseException if there was a problem.
*/
private static CommandLine parseCommandLine(Options options, String[] args) throws ParseException {
CommandLineParser parser = new PosixParser();
return parser.parse(options, args, true);
}
/**
* Build the options parser. Has to be synchronized because of the way Options are constructed.
*
* @return an options parser.
*/
private static synchronized Options buildOptions() {
Options options = new Options();
options.addOption(
OptionBuilder.withLongOpt("define").
withDescription("define a system property").
hasArg(true).
withArgName("name=value").
create('D')
);
options.addOption(
OptionBuilder.hasArg(false)
.withDescription("usage information")
.withLongOpt("help")
.create('h'));
options.addOption(
OptionBuilder.hasArg(false)
.withDescription("debug mode will print out full stack traces")
.withLongOpt("debug")
.create('d'));
options.addOption(
OptionBuilder.hasArg(false)
.withDescription("display the Groovy and JVM versions")
.withLongOpt("version")
.create('v'));
options.addOption(
OptionBuilder.withArgName("charset")
.hasArg()
.withDescription("specify the encoding of the files")
.withLongOpt("encoding")
.create('c'));
options.addOption(
OptionBuilder.withArgName("script")
.hasArg()
.withDescription("specify a command line script")
.create('e'));
options.addOption(
OptionBuilder.withArgName("extension")
.hasOptionalArg()
.withDescription("modify files in place, create backup if extension is given (e.g. \'.bak\')")
.create('i'));
options.addOption(
OptionBuilder.hasArg(false)
.withDescription("process files line by line")
.create('n'));
options.addOption(
OptionBuilder.hasArg(false)
.withDescription("process files line by line and print result")
.create('p'));
options.addOption(
OptionBuilder.withArgName("port")
.hasOptionalArg()
.withDescription("listen on a port and process inbound lines")
.create('l'));
options.addOption(
OptionBuilder.withArgName("splitPattern")
.hasOptionalArg()
.withDescription("automatically split current line (defaults to '\\s')")
.withLongOpt("autosplit")
.create('a'));
return options;
}
private static void setSystemPropertyFrom(final String nameValue) {
if(nameValue==null) throw new IllegalArgumentException("argument should not be null");
String name, value;
int i = nameValue.indexOf("=");
if (i == -1) {
name = nameValue;
value = Boolean.TRUE.toString();
}
else {
name = nameValue.substring(0, i);
value = nameValue.substring(i + 1, nameValue.length());
}
name = name.trim();
System.setProperty(name, value);
}
/**
* Process the users request.
*
* @param line the parsed command line.
* @throws ParseException if invalid options are chosen
*/
private static boolean process(CommandLine line) throws ParseException {
GroovyMain main = new GroovyMain();
List args = line.getArgList();
if (line.hasOption('D')) {
String[] values = line.getOptionValues('D');
for (int i=0; i