Search in sources :

Example 51 with CommandLine

use of org.apache.commons.cli.CommandLine in project zm-mailbox by Zimbra.

the class ZoneInfo2iCalendar method main.

// main
public static void main(String[] args) throws Exception {
    // command line handling
    CommandLine cl = null;
    Params params = null;
    try {
        cl = parseArgs(args);
        if (cl.hasOption(OPT_HELP)) {
            usage(null);
            System.exit(0);
        }
        params = initParams(cl);
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
        System.exit(1);
    }
    // parse tzdata source
    ZoneInfoParser parser = new ZoneInfoParser();
    for (File tzdataFile : params.tzdataFiles) {
        Reader r = null;
        try {
            r = new InputStreamReader(new FileInputStream(tzdataFile), "UTF-8");
            parser.readTzdata(r);
        } catch (ParseException e) {
            System.err.println(e.getMessage());
            System.err.println("Line: " + e.getErrorOffset());
            System.err.println("File: " + tzdataFile.getAbsolutePath());
            e.printStackTrace();
            System.exit(1);
        } finally {
            if (r != null)
                r.close();
        }
    }
    parser.analyze();
    // read extra data file containing primary TZ list and zone match scores
    if (params.extraDataFile != null) {
        Reader r = null;
        try {
            r = new InputStreamReader(new FileInputStream(params.extraDataFile), "UTF-8");
            readExtraData(r);
        } catch (ParseException e) {
            System.err.println(e.getMessage());
            System.err.println("Line: " + e.getErrorOffset());
            System.err.println("File: " + params.extraDataFile.getAbsolutePath());
            e.printStackTrace();
            System.exit(1);
        } finally {
            if (r != null)
                r.close();
        }
    }
    Writer out;
    if (params.outputFile != null) {
        out = new PrintWriter(params.outputFile, "UTF-8");
    } else {
        out = new PrintWriter(new OutputStreamWriter(System.out, "UTF-8"));
    }
    try {
        StringBuilder hdr = new StringBuilder("BEGIN:VCALENDAR");
        hdr.append(CRLF);
        hdr.append("PRODID:Zimbra-Calendar-Provider").append(CRLF);
        hdr.append("VERSION:2.0").append(CRLF);
        hdr.append("METHOD:PUBLISH").append(CRLF);
        out.write(hdr.toString());
        Map<String, VTimeZone> oldTimeZones = makeOldTimeZonesMap(params);
        Set<Zone> zones = new TreeSet<Zone>(new ZoneComparatorByGmtOffset());
        zones.addAll(parser.getZones());
        Set<String> zoneIDs = new TreeSet<String>();
        for (Zone zone : zones) {
            zoneIDs.add(zone.getName());
        }
        for (Zone zone : zones) {
            out.write(getTimeZoneForZone(zone, params, zoneIDs, oldTimeZones));
        }
        StringBuilder footer = new StringBuilder("END:VCALENDAR");
        footer.append(CRLF);
        out.write(footer.toString());
    } finally {
        out.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Zone(com.zimbra.common.calendar.ZoneInfoParser.Zone) TimeZone(java.util.TimeZone) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ParseException(java.text.ParseException) ParserException(net.fortuna.ical4j.data.ParserException) FileNotFoundException(java.io.FileNotFoundException) TZDataParseException(com.zimbra.common.calendar.ZoneInfoParser.TZDataParseException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CommandLine(org.apache.commons.cli.CommandLine) TreeSet(java.util.TreeSet) OutputStreamWriter(java.io.OutputStreamWriter) ParseException(java.text.ParseException) TZDataParseException(com.zimbra.common.calendar.ZoneInfoParser.TZDataParseException) File(java.io.File) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Example 52 with CommandLine

use of org.apache.commons.cli.CommandLine in project zm-mailbox by Zimbra.

the class MimeDetect method main.

public static void main(String[] args) {
    int limit = -1;
    MimeDetect md = new MimeDetect();
    Options opts = new Options();
    CommandLineParser parser = new GnuParser();
    int ret = 1;
    opts.addOption("d", "data", false, "data only");
    opts.addOption("g", "globs", true, "globs file");
    opts.addOption("l", "limit", true, "size limit");
    opts.addOption("m", "magic", true, "magic file");
    opts.addOption("n", "name", false, "name only");
    opts.addOption("v", "validate", false, "validate extension and data");
    try {
        CommandLine cl = parser.parse(opts, args);
        String file;
        String globs = LC.shared_mime_info_globs.value();
        String magic = LC.shared_mime_info_magic.value();
        String type;
        if (cl.hasOption('g'))
            globs = cl.getOptionValue('g');
        if (cl.hasOption('l'))
            limit = Integer.parseInt(cl.getOptionValue('l'));
        if (cl.hasOption('m'))
            magic = cl.getOptionValue('m');
        if (cl.getArgs().length != 1)
            usage(opts);
        file = cl.getArgs()[0];
        md.parse(globs, magic);
        if (cl.hasOption('n')) {
            type = md.detect(file);
        } else if (file.equals("-")) {
            type = md.detect(System.in, limit);
        } else if (cl.hasOption('d')) {
            type = md.detect(new FileInputStream(file), limit);
        } else if (cl.hasOption('v')) {
            type = md.validate(new File(file), limit);
        } else {
            type = md.detect(new File(file), limit);
        }
        if (type == null) {
            System.out.println("unknown");
        } else {
            System.out.println(type);
            ret = 0;
        }
    } catch (Exception e) {
        e.printStackTrace(System.out);
        if (e instanceof UnrecognizedOptionException)
            usage(opts);
    }
    System.exit(ret);
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException)

Example 53 with CommandLine

use of org.apache.commons.cli.CommandLine in project zm-mailbox by Zimbra.

the class RandomPassword method main.

public static void main(String[] args) {
    CommandLineParser parser = new GnuParser();
    Options options = new Options();
    options.addOption("l", "localpart", false, "generated string does not contain dot(.)");
    CommandLine cl = null;
    boolean err = false;
    try {
        cl = parser.parse(options, args, true);
    } catch (ParseException pe) {
        System.err.println("error: " + pe.getMessage());
        err = true;
    }
    if (err || cl.hasOption('h')) {
        usage();
    }
    boolean localpart = false;
    int minLength = DEFAULT_MIN_LENGTH;
    int maxLength = DEFAULT_MAX_LENGTH;
    if (cl.hasOption('l'))
        localpart = true;
    args = cl.getArgs();
    if (args.length != 0) {
        if (args.length != 2) {
            usage();
        }
        try {
            minLength = Integer.valueOf(args[0]).intValue();
            maxLength = Integer.valueOf(args[1]).intValue();
        } catch (Exception e) {
            System.err.println(e);
            e.printStackTrace();
        }
    }
    System.out.println(generate(minLength, maxLength, localpart));
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) ParseException(org.apache.commons.cli.ParseException)

Example 54 with CommandLine

use of org.apache.commons.cli.CommandLine in project zm-mailbox by Zimbra.

the class SoapApiChangeLog method main.

/**
     * Main
     */
public static void main(String[] args) throws Exception {
    CommandLineParser parser = new PosixParser();
    Options options = new Options();
    Option opt;
    opt = new Option("d", ARG_OUTPUT_DIR, true, "Output directory for changelog information");
    opt.setRequired(true);
    options.addOption(opt);
    opt = new Option("t", ARG_TEMPLATES_DIR, true, "Directory containing Freemarker templates");
    opt.setRequired(true);
    options.addOption(opt);
    opt = new Option("b", ARG_APIDESC_BASELINE_JSON, true, "JSON file - description of baseline SOAP API");
    opt.setRequired(true);
    options.addOption(opt);
    opt = new Option("c", ARG_APIDESC_CURRENT_JSON, true, "JSON file - description of current SOAP API");
    opt.setRequired(true);
    options.addOption(opt);
    CommandLine cl = null;
    try {
        cl = parser.parse(options, args, true);
    } catch (ParseException pe) {
        System.err.println("error: " + pe.getMessage());
        System.exit(2);
    }
    String baselineApiDescriptionJson = cl.getOptionValue('b');
    String currentApiDescriptionJson = cl.getOptionValue('c');
    SoapApiChangeLog clog = new SoapApiChangeLog(cl.getOptionValue('d'), cl.getOptionValue('t'));
    clog.setBaselineDesc(SoapApiDescription.deserializeFromJson(new File(baselineApiDescriptionJson)));
    clog.setCurrentDesc(SoapApiDescription.deserializeFromJson(new File(currentApiDescriptionJson)));
    clog.makeChangeLogDataModel();
    clog.writeChangelog();
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) JsonParseException(org.codehaus.jackson.JsonParseException) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

Example 55 with CommandLine

use of org.apache.commons.cli.CommandLine in project alluxio by Alluxio.

the class JournalTool method parseInputArgs.

/**
   * Parses the input args with a command line format, using
   * {@link org.apache.commons.cli.CommandLineParser}.
   *
   * @param args the input args
   * @return true if parsing succeeded
   */
private static boolean parseInputArgs(String[] args) {
    CommandLineParser parser = new BasicParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(OPTIONS, args);
    } catch (ParseException e) {
        System.out.println("Failed to parse input args: " + e);
        return false;
    }
    sNoTimeout = cmd.hasOption("noTimeout");
    sHelp = cmd.hasOption("help");
    return true;
}
Also used : BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Aggregations

CommandLine (org.apache.commons.cli.CommandLine)968 Options (org.apache.commons.cli.Options)560 ParseException (org.apache.commons.cli.ParseException)464 CommandLineParser (org.apache.commons.cli.CommandLineParser)459 HelpFormatter (org.apache.commons.cli.HelpFormatter)259 GnuParser (org.apache.commons.cli.GnuParser)230 DefaultParser (org.apache.commons.cli.DefaultParser)213 PosixParser (org.apache.commons.cli.PosixParser)177 File (java.io.File)165 IOException (java.io.IOException)162 Test (org.junit.Test)144 Option (org.apache.commons.cli.Option)131 ArrayList (java.util.ArrayList)63 Path (org.apache.hadoop.fs.Path)60 BasicParser (org.apache.commons.cli.BasicParser)50 Configuration (org.apache.hadoop.conf.Configuration)41 Configuration (org.apache.flink.configuration.Configuration)37 HashMap (java.util.HashMap)34 List (java.util.List)34 Properties (java.util.Properties)33