Examples of CmdLineParser


Examples of com.gitblit.utils.cli.CmdLineParser

   *             if the command line arguments were invalid.
   * @see Option
   * @see Argument
   */
  protected void parseCommandLine(Object options) throws UnloggedFailure {
    final CmdLineParser clp = newCmdLineParser(options);
    try {
      clp.parseArgument(argv);
    } catch (IllegalArgumentException err) {
      if (!clp.wasHelpRequestedByOption()) {
        throw new UnloggedFailure(1, "fatal: " + err.getMessage());
      }
    } catch (CmdLineException err) {
      if (!clp.wasHelpRequestedByOption()) {
        throw new UnloggedFailure(1, "fatal: " + err.getMessage());
      }
    }

    if (clp.wasHelpRequestedByOption()) {
      CommandMetaData meta = getClass().getAnnotation(CommandMetaData.class);
      String title = meta.name().toUpperCase() + ": " + meta.description();
      String b = com.gitblit.utils.StringUtils.leftPad("", title.length() + 2, '═');
      StringWriter msg = new StringWriter();
      msg.write('\n');
      msg.write(b);
      msg.write('\n');
      msg.write(' ');
      msg.write(title);
      msg.write('\n');
      msg.write(b);
      msg.write("\n\n");
      msg.write("USAGE\n");
      msg.write("─────\n");
      msg.write(' ');
      msg.write(commandName);
      msg.write('\n');
      msg.write("  ");
      clp.printSingleLineUsage(msg, null);
      msg.write("\n\n");
      String txt = getUsageText();
      if (!StringUtils.isEmpty(txt)) {
        msg.write(txt);
        msg.write("\n\n");
      }
      msg.write("ARGUMENTS & OPTIONS\n");
      msg.write("───────────────────\n");
      clp.printUsage(msg, null);
      msg.write('\n');
      String examples = usage().trim();
      if (!StringUtils.isEmpty(examples)) {
        msg.write('\n');
        msg.write("EXAMPLES\n");
View Full Code Here

Examples of com.google.gerrit.util.cli.CmdLineParser

    return n.toLowerCase();
  }

  public final int main(final String[] argv) throws Exception {
    final Injector empty = emptyInjector();
    final CmdLineParser clp = new CmdLineParser(empty, this);
    try {
      clp.parseArgument(argv);
    } catch (CmdLineException err) {
      if (!clp.wasHelpRequestedByOption()) {
        System.err.println("fatal: " + err.getMessage());
        return 1;
      }
    }

    if (clp.wasHelpRequestedByOption()) {
      StringWriter msg = new StringWriter();
      clp.printDetailedUsage(getName(), msg);
      System.err.println(msg.toString());
      return 1;
    }

    try {
View Full Code Here

Examples of com.sanityinc.jargs.CmdLineParser

        // The -d and -v options have no associated value -- they are either
        // present, or they are not.  The -s and -f options take integer and
        // double-precision floating-point values respectively.

        CmdLineParser parser = new CmdLineParser();
        Option<Boolean> debug = parser.addBooleanOption('d', "debug");
        Option<Boolean> verbose = parser.addBooleanOption('v', "verbose");
        Option<Integer> size = parser.addIntegerOption('s', "size");
        Option<Double> fraction = parser.addDoubleOption('f', "fraction");

        // Options may have just a long form with no corresponding short form.
        // Here, we add --alt and --name options.

        Option<Boolean> alt = parser.addBooleanOption("alt");
        Option<String> name = parser.addStringOption('n', "name");


        // Next, you must parse the user-provided command line arguments, and
        // catch any errors therein.

        // Options may appear on the command line in any order, and may even
        // appear after some or all of the non-option arguments.

        // If the user needs to specify non-option arguments that start with a
        // minus, then they may indicate the end of the parsable options with
        // -- , like this:

        // prog -f 20 -- -10 -fred

        // The -f 20 will be parsed as the fraction option, with the value 20.
        // The -10 and -fred arguments will be regarded as non-option
        // arguments, and passed through getRemainingArgs as unparsed Strings.

        // Short boolean options may be specified separately (-d -v) or
        // together (-dv).

        // Options with values may be given on the command line as -f 1.0 or
        // --fraction=1.0.

        try {
            parser.parse(args);
        }
        catch ( CmdLineParser.OptionException e ) {
            System.err.println(e.getMessage());
            printUsage();
            System.exit(2);
        }


        // For options that may be specified only zero or one time, the value
        // of that option may be extracted as shown below.  If the options
        // were not specified, the corresponding values will be null.

        Boolean debugValue = parser.getOptionValue(debug);
        String nameValue = parser.getOptionValue(name);

        // Alternatively, you may specify a default value.  This will be
        // returned (instead of null) when the command line argument is
        // missing.

        Boolean altValue = parser.getOptionValue(alt, Boolean.FALSE);
        Integer sizeValue = parser.getOptionValue(size, new Integer(42));

        // If your application requires it, options may be specified more than
        // once.  In this case, you may get all the values specified by the
        // user, as a Vector:

        Collection<Double> fractionValues = parser.getOptionValues(fraction);

        // Alternatively, you may make the loop explicit:

        int verbosity = 0;
        while (true) {
            Boolean verboseValue = parser.getOptionValue(verbose);

            if (verboseValue == null) {
                break;
            }
            else {
                verbosity++;
            }
        }

        // The remaining command-line arguments -- those that do not start
        // with a minus sign -- can be captured like this:

        String[] otherArgs = parser.getRemainingArgs();


        // For testing purposes, we just print out the option values and
        // remaining command-line arguments.  In a real program, of course,
        // one would pass them to a function that does something more useful.
View Full Code Here

Examples of jargs.gnu.CmdLineParser

  final private CmdLineParser.Option filePath;
  final private CmdLineParser.Option frameType;
  final private CmdLineParser.Option exportType;

  public MainProgramArgs(String [] args) {
    parser = new CmdLineParser();
    resource = parser.addStringOption('r', "resource");
    filePath = parser.addStringOption('f', "file");
    frameType = parser.addStringOption('t', "type");
    exportType = parser.addStringOption('x', "export");
        try {
View Full Code Here

Examples of jargs.gnu.CmdLineParser

    final private CmdLineParser.Option gtrWidth;
    final private CmdLineParser.Option below;
    final private CmdLineParser.Option logcenter;

    public DendroviewArgs(String [] args) {
      parser = new CmdLineParser();
      filePath = parser.addStringOption('o', "output");
      exportType = parser.addStringOption('f', "format");
      scaling = parser.addStringOption('s', "scaling");
      aHeaders = parser.addStringOption('a', "arrayHeaders");
      gHeaders = parser.addStringOption('g', "geneHeaders");
View Full Code Here

Examples of jargs.gnu.CmdLineParser

public class YUICompressor {

    public static void main(String args[]) {

        CmdLineParser parser = new CmdLineParser();
        CmdLineParser.Option typeOpt = parser.addStringOption("type");
        CmdLineParser.Option verboseOpt = parser.addBooleanOption('v', "verbose");
        CmdLineParser.Option nomungeOpt = parser.addBooleanOption("nomunge");
        CmdLineParser.Option linebreakOpt = parser.addStringOption("line-break");
        CmdLineParser.Option preserveSemiOpt = parser.addBooleanOption("preserve-semi");
        CmdLineParser.Option disableOptimizationsOpt = parser.addBooleanOption("disable-optimizations");
        CmdLineParser.Option helpOpt = parser.addBooleanOption('h', "help");
        CmdLineParser.Option charsetOpt = parser.addStringOption("charset");
        CmdLineParser.Option outputFilenameOpt = parser.addStringOption('o', "output");

        Reader in = null;
        Writer out = null;

        try {

            parser.parse(args);

            Boolean help = (Boolean) parser.getOptionValue(helpOpt);
            if (help != null && help.booleanValue()) {
                usage();
                System.exit(0);
            }

            boolean verbose = parser.getOptionValue(verboseOpt) != null;

            String charset = (String) parser.getOptionValue(charsetOpt);
            if (charset == null || !Charset.isSupported(charset)) {
                // charset = System.getProperty("file.encoding");
                // if (charset == null) {
                //     charset = "UTF-8";
                // }

                // UTF-8 seems to be a better choice than what the system is reporting
                charset = "UTF-8";


                if (verbose) {
                    System.err.println("\n[INFO] Using charset " + charset);
                }
            }

            int linebreakpos = -1;
            String linebreakstr = (String) parser.getOptionValue(linebreakOpt);
            if (linebreakstr != null) {
                try {
                    linebreakpos = Integer.parseInt(linebreakstr, 10);
                } catch (NumberFormatException e) {
                    usage();
                    System.exit(1);
                }
            }

            String type = (String) parser.getOptionValue(typeOpt);
            if (type != null && !type.equalsIgnoreCase("js") && !type.equalsIgnoreCase("css")) {
                usage();
                System.exit(1);
            }

            String[] fileArgs = parser.getRemainingArgs();
            java.util.List files = java.util.Arrays.asList(fileArgs);
            if (files.isEmpty()) {
                if (type == null) {
                    usage();
                    System.exit(1);
                }
                files = new java.util.ArrayList();
                files.add("-"); // read from stdin
            }

            String output = (String) parser.getOptionValue(outputFilenameOpt);
            String pattern[] = output != null ? output.split(":") : new String[0];

            java.util.Iterator filenames = files.iterator();
            while(filenames.hasNext()) {
                String inputFilename = (String)filenames.next();

                try {
                    if (inputFilename.equals("-")) {

                        in = new InputStreamReader(System.in, charset);

                    } else {

                        if (type == null) {
                            int idx = inputFilename.lastIndexOf('.');
                            if (idx >= 0 && idx < inputFilename.length() - 1) {
                                type = inputFilename.substring(idx + 1);
                            }
                        }

                        if (type == null || !type.equalsIgnoreCase("js") && !type.equalsIgnoreCase("css")) {
                            usage();
                            System.exit(1);
                        }

                        in = new InputStreamReader(new FileInputStream(inputFilename), charset);
                    }

                    String outputFilename = output;
                    // if a substitution pattern was passed in
                    if (pattern.length > 1 && files.size() > 1) {
                        outputFilename = inputFilename.replaceFirst(pattern[0], pattern[1]);
                    }

                    if (type.equalsIgnoreCase("js")) {

                        try {

                            JavaScriptCompressor compressor = new JavaScriptCompressor(in, new ErrorReporter() {

                                public void warning(String message, String sourceName,
                                        int line, String lineSource, int lineOffset) {
                                    if (line < 0) {
                                        System.err.println("\n[WARNING] " + message);
                                    } else {
                                        System.err.println("\n[WARNING] " + line + ':' + lineOffset + ':' + message);
                                    }
                                }

                                public void error(String message, String sourceName,
                                        int line, String lineSource, int lineOffset) {
                                    if (line < 0) {
                                        System.err.println("\n[ERROR] " + message);
                                    } else {
                                        System.err.println("\n[ERROR] " + line + ':' + lineOffset + ':' + message);
                                    }
                                }

                                public EvaluatorException runtimeError(String message, String sourceName,
                                        int line, String lineSource, int lineOffset) {
                                    error(message, sourceName, line, lineSource, lineOffset);
                                    return new EvaluatorException(message);
                                }
                            });

                            // Close the input stream first, and then open the output stream,
                            // in case the output file should override the input file.
                            in.close(); in = null;

                            if (outputFilename == null) {
                                out = new OutputStreamWriter(System.out, charset);
                            } else {
                                out = new OutputStreamWriter(new FileOutputStream(outputFilename), charset);
                            }

                            boolean munge = parser.getOptionValue(nomungeOpt) == null;
                            boolean preserveAllSemiColons = parser.getOptionValue(preserveSemiOpt) != null;
                            boolean disableOptimizations = parser.getOptionValue(disableOptimizationsOpt) != null;

                            compressor.compress(out, linebreakpos, munge, verbose,
                                    preserveAllSemiColons, disableOptimizations);

                        } catch (EvaluatorException e) {
View Full Code Here

Examples of jargs.gnu.CmdLineParser

        System.out.println("\nJSBuilder2 is a JavaScript and CSS project build tool.");
        System.out.println("For additional information, see http://extjs.com/products/jsbuilder/");
    }
   
    private static boolean parseArgs(String[] args) {
        CmdLineParser parser = new CmdLineParser();
        CmdLineParser.Option projectFileOpt = parser.addStringOption('p', "projectFile");
        CmdLineParser.Option homeDirOpt = parser.addStringOption('d', "homeDir");
        CmdLineParser.Option verboseOpt = parser.addBooleanOption('v', "verbose");
        CmdLineParser.Option helpOpt = parser.addBooleanOption('h', "help");       
        CmdLineParser.Option debugSuffixOpt = parser.addStringOption('s', "debugSuffix");       

        try {
            parser.parse(args);
        }
        catch ( CmdLineParser.OptionException e ) {
            System.err.println(e.getMessage());
            System.exit(2);
        }

        homeDir = (String)parser.getOptionValue(homeDirOpt, "");
        projectFile = (String)parser.getOptionValue(projectFileOpt, "");
        debugSuffix = (String)parser.getOptionValue(debugSuffixOpt, "-debug");
        verbose = (Boolean)parser.getOptionValue(verboseOpt, false);
        Boolean help = (Boolean)parser.getOptionValue(helpOpt, false);
        // if help dont proceed
        if (help) {
            return false;
        }
        if (homeDir == "") {
View Full Code Here

Examples of jargs.gnu.CmdLineParser

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        Writer out = null;
        Reader in = null;
       
        //initialize command line parser
        CmdLineParser parser = new CmdLineParser();
        CmdLineParser.Option verboseOpt = parser.addBooleanOption('v', "verbose");
        CmdLineParser.Option helpOpt = parser.addBooleanOption('h', "help");
        CmdLineParser.Option outputFilenameOpt = parser.addStringOption('o', "output");
        CmdLineParser.Option nameOpt = parser.addStringOption('n', "name");
        CmdLineParser.Option outputTypeOpt = parser.addStringOption('t', "to");
       
        try {
           
            //parse the arguments
            parser.parse(args);

            //figure out if the help option has been executed
            Boolean help = (Boolean) parser.getOptionValue(helpOpt);
            if (help != null && help.booleanValue()) {
                usage();
                System.exit(0);
            }
           
            //determine boolean options
            verbose = parser.getOptionValue(verboseOpt) != null;           
         
            //get the file arguments
            String[] fileArgs = parser.getRemainingArgs();
            String inputFilename = fileArgs[0];

            if (fileArgs.length == 0) {
                throw new Exception("No input filename specified.");
            }

            in = new InputStreamReader(new FileInputStream(inputFilename), "UTF-8");
            Properties properties = new Properties();
            properties.load(in);

            //get output type
            String outputType = (String) parser.getOptionValue(outputTypeOpt);
            if (outputType == null){
                outputType = "json";
                if (verbose){
                    System.err.println("[INFO] No output type specified, defaulting to json.");
                }
            } else {
                if (verbose){
                    System.err.println("[INFO] Output type set to " + outputType + ".");
                }
            }

            //get output filename
            outputFilename = (String) parser.getOptionValue(outputFilenameOpt);           
            if (outputFilename == null) {
                if (verbose){
                    System.err.println("[INFO] No output file specified, defaulting to stdout.");
                }               
               
                out = new OutputStreamWriter(System.out);
            } else {
                File outputFile = new File(outputFilename);
                if (verbose){
                    System.err.println("[INFO] Output file is '" + outputFile.getAbsolutePath() + "'");
                }
                out = new OutputStreamWriter(bytes, "UTF-8");
            }           

            String name = (String) parser.getOptionValue(nameOpt);
            if (name == null && !outputType.equalsIgnoreCase("json")){
                throw new Exception("Missing --name option.");
            }

            String result = "";
View Full Code Here

Examples of org.apache.pig.tools.cmdline.CmdLineParser

        boolean dryrun = false;
        ArrayList<String> params = new ArrayList<String>();
        ArrayList<String> paramFiles = new ArrayList<String>();
        HashSet<String> optimizerRules = new HashSet<String>();

        CmdLineParser opts = new CmdLineParser(args);
        opts.registerOpt('4', "log4jconf", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('b', "brief", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('c', "cluster", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('d', "debug", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('e', "execute", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('f', "file", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('h', "help", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('i', "version", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('j', "jar", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('l', "logfile", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('m', "param_file", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('p', "param", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('r', "dryrun", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('t', "optimizer_off", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('v', "verbose", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('w', "warning", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('x', "exectype", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('F', "stop_on_failure", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('M', "no_multiquery", CmdLineParser.ValueExpected.NOT_ACCEPTED);

        ExecMode mode = ExecMode.UNKNOWN;
        String file = null;
        ExecType execType = ExecType.MAPREDUCE ;
        String execTypeString = properties.getProperty("exectype");
        if(execTypeString!=null && execTypeString.length()>0){
            execType = PigServer.parseExecType(execTypeString);
        }
        String cluster = "local";
        String clusterConfigured = properties.getProperty("cluster");
        if(clusterConfigured != null && clusterConfigured.length() > 0){
            cluster = clusterConfigured;
        }
       
        //by default warning aggregation is on
        properties.setProperty("aggregate.warning", ""+true);

        //by default multiquery optimization is on
        properties.setProperty("opt.multiquery", ""+true);

        //by default we keep going on error on the backend
        properties.setProperty("stop.on.failure", ""+false);

        char opt;
        while ((opt = opts.getNextOpt()) != CmdLineParser.EndOfOpts) {
            switch (opt) {
            case '4':
                String log4jconf = opts.getValStr();
                if(log4jconf != null){
                    properties.setProperty(LOG4J_CONF, log4jconf);
                }
                break;

            case 'b':
                properties.setProperty(BRIEF, "true");
                break;

            case 'c':
                // Needed away to specify the cluster to run the MR job on
                // Bug 831708 - fixed
                String clusterParameter = opts.getValStr();
                if (clusterParameter != null && clusterParameter.length() > 0) {
                    cluster = clusterParameter;
                }
                break;

            case 'd':
                String logLevel = opts.getValStr();
                if (logLevel != null) {
                    properties.setProperty(DEBUG, logLevel);
                }
                debug = true;
                break;
               
            case 'e':
                mode = ExecMode.STRING;
                break;

            case 'f':
                mode = ExecMode.FILE;
                file = opts.getValStr();
                break;

            case 'F':
                properties.setProperty("stop.on.failure", ""+true);
                break;

            case 'h':
                usage();
                rc = 0;
                return;

            case 'i':
              System.out.println(getVersionString());
                rc = 0;
              return;

            case 'j':
                String jarsString = opts.getValStr();
                if(jarsString != null){
                    properties.setProperty(JAR, jarsString);
                }
                break;

            case 'l':
                //call to method that validates the path to the log file
                //and sets up the file to store the client side log file               
                String logFileParameter = opts.getValStr();
                if (logFileParameter != null && logFileParameter.length() > 0) {
                    logFileName = validateLogFile(logFileParameter, null);
                } else {
                    logFileName = validateLogFile(logFileName, null);
                }
                userSpecifiedLog = true;
                properties.setProperty("pig.logfile", (logFileName == null? "": logFileName));
                break;

            case 'm':
                paramFiles.add(opts.getValStr());
                break;

            case 'M':
                // turns off multiquery optimization
                properties.setProperty("opt.multiquery",""+false);
                break;
                           
            case 'p':
                String val = opts.getValStr();
                params.add(opts.getValStr());
                break;
                           
            case 'r':
                // currently only used for parameter substitution
                // will be extended in the future
                dryrun = true;
                break;

            case 't':
              optimizerRules.add(opts.getValStr());
                break;
                           
            case 'v':
                properties.setProperty(VERBOSE, ""+true);
                verbose = true;
                break;

            case 'w':
                properties.setProperty("aggregate.warning", ""+false);
                break;

            case 'x':
                try {
                    execType = PigServer.parseExecType(opts.getValStr());
                    } catch (IOException e) {
                        throw new RuntimeException("ERROR: Unrecognized exectype.", e);
                    }
                break;
            default: {
                Character cc = Character.valueOf(opt);
                throw new AssertionError("Unhandled option " + cc.toString());
                     }
            }
        }
        // create the context with the parameter
        PigContext pigContext = new PigContext(execType, properties);

        // configure logging
        configureLog4J(properties, pigContext);
       
        if(logFileName == null && !userSpecifiedLog) {
      logFileName = validateLogFile(properties.getProperty("pig.logfile"), null);
  }
       
        if(logFileName != null) {
            log.info("Logging error messages to: " + logFileName);
        }
       
        pigContext.getProperties().setProperty("pig.logfile", (logFileName == null? "": logFileName));
       
        if(optimizerRules.size() > 0) {
          pigContext.getProperties().setProperty("pig.optimizer.rules", ObjectSerializer.serialize(optimizerRules));
        }
       
        if (properties.get("udf.import.list")!=null)
            PigContext.initializeImportList((String)properties.get("udf.import.list"));

        LogicalPlanBuilder.classloader = pigContext.createCl(null);

        // construct the parameter substitution preprocessor
        Grunt grunt = null;
        BufferedReader in;
        String substFile = null;
        switch (mode) {
        case FILE: {
            // Run, using the provided file as a pig file
            in = new BufferedReader(new FileReader(file));

            // run parameter substitution preprocessor first
            substFile = file + ".substituted";
            pin = runParamPreprocessor(in, params, paramFiles, substFile, debug || dryrun);
            if (dryrun) {
                log.info("Dry run completed. Substituted pig script is at " + substFile);
                return;
            }

            logFileName = validateLogFile(logFileName, file);
            pigContext.getProperties().setProperty("pig.logfile", logFileName);

            // Set job name based on name of the script
            pigContext.getProperties().setProperty(PigContext.JOB_NAME,
                                                   "PigLatin:" +new File(file).getName()
            );
           
            if (!debug) {
                new File(substFile).deleteOnExit();
            }
           
            grunt = new Grunt(pin, pigContext);
            gruntCalled = true;
            int results[] = grunt.exec();
            rc = getReturnCodeForStats(results);
            return;
        }

        case STRING: {
            // Gather up all the remaining arguments into a string and pass them into
            // grunt.
            StringBuffer sb = new StringBuffer();
            String remainders[] = opts.getRemainingArgs();
            for (int i = 0; i < remainders.length; i++) {
                if (i != 0) sb.append(' ');
                sb.append(remainders[i]);
            }
            in = new BufferedReader(new StringReader(sb.toString()));
            grunt = new Grunt(in, pigContext);
            gruntCalled = true;
            int results[] = grunt.exec();
            rc = getReturnCodeForStats(results);
            return;
            }

        default:
            break;
        }

        // If we're here, we don't know yet what they want.  They may have just
        // given us a jar to execute, they might have given us a pig script to
        // execute, or they might have given us a dash (or nothing) which means to
        // run grunt interactive.
        String remainders[] = opts.getRemainingArgs();
        if (remainders == null) {
            // Interactive
            mode = ExecMode.SHELL;
            ConsoleReader reader = new ConsoleReader(System.in, new OutputStreamWriter(System.out));
            reader.setDefaultPrompt("grunt> ");
View Full Code Here

Examples of org.apache.pig.tools.cmdline.CmdLineParser

        boolean dryrun = false;
        ArrayList<String> params = new ArrayList<String>();
        ArrayList<String> paramFiles = new ArrayList<String>();
        HashSet<String> optimizerRules = new HashSet<String>();

        CmdLineParser opts = new CmdLineParser(pigArgs);
        opts.registerOpt('4', "log4jconf", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('b', "brief", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('c', "check", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('d', "debug", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('e', "execute", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('f', "file", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('h', "help", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('i', "version", CmdLineParser.ValueExpected.OPTIONAL);       
        opts.registerOpt('l', "logfile", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('m', "param_file", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('p', "param", CmdLineParser.ValueExpected.OPTIONAL);
        opts.registerOpt('r', "dryrun", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('t', "optimizer_off", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('v', "verbose", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('w', "warning", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('x', "exectype", CmdLineParser.ValueExpected.REQUIRED);
        opts.registerOpt('F', "stop_on_failure", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('M', "no_multiquery", CmdLineParser.ValueExpected.NOT_ACCEPTED);
        opts.registerOpt('P', "propertyFile", CmdLineParser.ValueExpected.REQUIRED);

        ExecMode mode = ExecMode.UNKNOWN;
        String file = null;
        ExecType execType = ExecType.MAPREDUCE ;
        String execTypeString = properties.getProperty("exectype");
        if(execTypeString!=null && execTypeString.length()>0){
            execType = PigServer.parseExecType(execTypeString);
        }
               
        if (properties.getProperty("aggregate.warning") == null) {
            //by default warning aggregation is on
            properties.setProperty("aggregate.warning", ""+true);
        }
               
        if (properties.getProperty("opt.multiquery") == null) {
            //by default multiquery optimization is on
            properties.setProperty("opt.multiquery", ""+true);
        }

        if (properties.getProperty("stop.on.failure") == null) {
            //by default we keep going on error on the backend
            properties.setProperty("stop.on.failure", ""+false);
        }
       
        if( "true".equals( properties.getProperty( "mapred.output.compress" ) ) ) {
            properties.setProperty( "output.compression.enabled""true" );
            String codec = properties.getProperty( "mapred.output.compression.codec" );
            if( codec == null ) {
                throw new RuntimeException( "'mapred.output.compress' is set but no value is specified for 'mapred.output.compression.codec'." );
            } else {
                properties.setProperty( "output.compression.codec", codec );
            }
        }
       
        // set up client side system properties in UDF context
        UDFContext.getUDFContext().setClientSystemProps();

        char opt;
        while ((opt = opts.getNextOpt()) != CmdLineParser.EndOfOpts) {
            switch (opt) {
            case '4':
                String log4jconf = opts.getValStr();
                if(log4jconf != null){
                    properties.setProperty(LOG4J_CONF, log4jconf);
                }
                break;

            case 'b':
                properties.setProperty(BRIEF, "true");
                break;

            case 'c':
                checkScriptOnly = true;               
                break;

            case 'd':
                String logLevel = opts.getValStr();
                if (logLevel != null) {
                    properties.setProperty(DEBUG, logLevel);
                }
                debug = true;
                break;
               
            case 'e':
                mode = ExecMode.STRING;
                break;

            case 'f':
                mode = ExecMode.FILE;
                file = opts.getValStr();
                break;

            case 'F':
                properties.setProperty("stop.on.failure", ""+true);
                break;

            case 'h':
    String topic = opts.getValStr();
    if (topic != null)
        if (topic.equalsIgnoreCase("properties"))
            printProperties();
                    else{
                        System.out.println("Invalide help topic - " + topic);
      usage();
                    }
    else
                    usage();
                return ReturnCode.SUCCESS;

            case 'i':
              System.out.println(getVersionString());
              return ReturnCode.SUCCESS;

            case 'l':
                //call to method that validates the path to the log file
                //and sets up the file to store the client side log file               
                String logFileParameter = opts.getValStr();
                if (logFileParameter != null && logFileParameter.length() > 0) {
                    logFileName = validateLogFile(logFileParameter, null);
                } else {
                    logFileName = validateLogFile(logFileName, null);
                }
                userSpecifiedLog = true;
                properties.setProperty("pig.logfile", (logFileName == null? "": logFileName));
                break;

            case 'm':
                paramFiles.add(opts.getValStr());
                break;

            case 'M':
                // turns off multiquery optimization
                properties.setProperty("opt.multiquery",""+false);
                break;
                           
            case 'p':
                params.add(opts.getValStr());
                break;
                           
            case 'r':
                // currently only used for parameter substitution
                // will be extended in the future
                dryrun = true;
                break;

            case 't':
              optimizerRules.add(opts.getValStr());
                break;
                           
            case 'v':
                properties.setProperty(VERBOSE, ""+true);
                verbose = true;
                break;

            case 'w':
                properties.setProperty("aggregate.warning", ""+false);
                break;

            case 'x':
                try {
                    execType = PigServer.parseExecType(opts.getValStr());
                    } catch (IOException e) {
                        throw new RuntimeException("ERROR: Unrecognized exectype.", e);
                    }
                break;
               
            case 'P':
            {
                InputStream inputStream = null;
                try {
                    FileLocalizer.FetchFileRet localFileRet = FileLocalizer.fetchFile(properties, opts.getValStr());
                    inputStream = new BufferedInputStream(new FileInputStream(localFileRet.file));
                    properties.load(inputStream) ;
                } catch (IOException e) {
                    throw new RuntimeException("Unable to parse properties file '" + opts.getValStr() + "'");
                } finally {
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                        }
                    }
                }
            }
            break;
           
            default: {
                Character cc = Character.valueOf(opt);
                throw new AssertionError("Unhandled option " + cc.toString());
                     }
            }
        }
        // create the context with the parameter
        PigContext pigContext = new PigContext(execType, properties);
       
        // create the static script state object
        String commandLine = LoadFunc.join((AbstractList<String>)Arrays.asList(args), " ");
        ScriptState scriptState = ScriptState.start(commandLine);
        if (listener != null) {
            scriptState.registerListener(listener);
        }
       

        if(logFileName == null && !userSpecifiedLog) {
            logFileName = validateLogFile(properties.getProperty("pig.logfile"), null);
        }
       
        pigContext.getProperties().setProperty("pig.logfile", (logFileName == null? "": logFileName));
    
        // configure logging
        configureLog4J(properties, pigContext);
       
        if(logFileName != null) {
            log.info("Logging error messages to: " + logFileName);
        }
       
        if(optimizerRules.size() > 0) {
          pigContext.getProperties().setProperty("pig.optimizer.rules", ObjectSerializer.serialize(optimizerRules));
        }
       
        if (properties.get("udf.import.list")!=null)
            PigContext.initializeImportList((String)properties.get("udf.import.list"));

        LogicalPlanBuilder.classloader = pigContext.createCl(null);

        // construct the parameter substitution preprocessor
        Grunt grunt = null;
        BufferedReader in;
        String substFile = null;
        switch (mode) {
        case FILE: {
            FileLocalizer.FetchFileRet localFileRet = FileLocalizer.fetchFile(properties, file);
            if (localFileRet.didFetch) {
                properties.setProperty("pig.jars.relative.to.dfs", "true");
            }
            in = new BufferedReader(new FileReader(localFileRet.file));

            // run parameter substitution preprocessor first
            substFile = file + ".substituted";
            pin = runParamPreprocessor(properties, in, params, paramFiles, substFile, debug || dryrun || checkScriptOnly);
            if (dryrun) {
                log.info("Dry run completed. Substituted pig script is at " + substFile);
                return ReturnCode.SUCCESS;
            }

            logFileName = validateLogFile(logFileName, file);
            pigContext.getProperties().setProperty("pig.logfile", logFileName);

            // Set job name based on name of the script
            pigContext.getProperties().setProperty(PigContext.JOB_NAME,
                                                   "PigLatin:" +new File(file).getName()
            );
           
            if (!debug) {
                new File(substFile).deleteOnExit();
            }
           
            scriptState.setScript(new File(file));
           
            grunt = new Grunt(pin, pigContext);
            gruntCalled = true;
           
            if(checkScriptOnly) {
                grunt.checkScript(substFile);
                System.err.println(file + " syntax OK");
                rc = ReturnCode.SUCCESS;
            } else {
                int results[] = grunt.exec();
                rc = getReturnCodeForStats(results);
            }
           
            return rc;
        }

        case STRING: {
            if(checkScriptOnly) {
                System.err.println("ERROR:" +
                        "-c (-check) option is only valid " +
                        "when executing pig with a pig script file)");
                return ReturnCode.ILLEGAL_ARGS;
            }
            // Gather up all the remaining arguments into a string and pass them into
            // grunt.
            StringBuffer sb = new StringBuffer();
            String remainders[] = opts.getRemainingArgs();
            for (int i = 0; i < remainders.length; i++) {
                if (i != 0) sb.append(' ');
                sb.append(remainders[i]);
            }
           
            scriptState.setScript(sb.toString());
           
            in = new BufferedReader(new StringReader(sb.toString()));
            grunt = new Grunt(in, pigContext);
            gruntCalled = true;
            int results[] = grunt.exec();
            return getReturnCodeForStats(results);
        }

        default:
            break;
        }

        // If we're here, we don't know yet what they want.  They may have just
        // given us a jar to execute, they might have given us a pig script to
        // execute, or they might have given us a dash (or nothing) which means to
        // run grunt interactive.
        String remainders[] = opts.getRemainingArgs();
        if (remainders == null) {
            if(checkScriptOnly) {
                System.err.println("ERROR:" +
                        "-c (-check) option is only valid " +
                        "when executing pig with a pig script file)");
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.