001package org.editorconfig;
002
003import org.editorconfig.core.EditorConfig;
004
005import java.io.PrintStream;
006import java.util.ArrayList;
007import java.util.List;
008
009/**
010 * @author Dennis.Ushakov
011 */
012public class EditorConfigCLI {
013  public static void main(String[] args) throws Exception {
014    List<String> filePaths = new ArrayList<String>();
015    String configFilename = null;
016    String version = EditorConfig.VERSION;
017    for (int i = 0; i < args.length; i++) {
018      String arg = args[i];
019      if ("-v".equals(arg) || "--version".equals(arg)) {
020        System.out.println("EditorConfig Java Version " + version);
021        System.exit(0);
022      }
023      if ("-h".equals(arg) || "--help".equals("arg")) {
024        printUsage(false);
025      }
026      if ("-b".equals(arg)) {
027        if (i + 1 < args.length) {
028          version = args[++i];
029          continue;
030        } else {
031          printUsage(true);
032        }
033      }
034      if ("-f".equals(arg)) {
035        if (i + 1 < args.length) {
036          configFilename = args[++i];
037          continue;
038        } else {
039          printUsage(true);
040        }
041      }
042      filePaths.add(arg);
043    }
044    if (filePaths.isEmpty()) {
045      printUsage(true);
046    }
047    for (String filePath : filePaths) {
048      List<EditorConfig.OutPair> properties = new EditorConfig(configFilename, version).getProperties(filePath);
049      if (filePaths.size() > 1) {
050        System.out.println("[" + filePath + "]");
051      }
052      for (EditorConfig.OutPair property : properties) {
053        System.out.println(property.getKey() + "=" + property.getVal());
054      }
055    }
056  }
057
058  private static void printUsage(boolean error) {
059    PrintStream out = error ? System.err : System.out;
060    out.println("[OPTIONS] filename");
061    out.println("-f                 Specify conf filename other than \".editorconfig\".");
062    out.println("-b                 Specify version (used by devs to test compatibility).");
063    out.println("-h OR --help       Print this help message.");
064    out.println("-v OR --version    Display version information.");
065    System.exit(error ? 2 : 0);
066  }
067}