kitaev@213: /* kitaev@213: * Copyright (c) 2011 TMate Software Ltd kitaev@213: * kitaev@213: * This program is free software; you can redistribute it and/or modify kitaev@213: * it under the terms of the GNU General Public License as published by kitaev@213: * the Free Software Foundation; version 2 of the License. kitaev@213: * kitaev@213: * This program is distributed in the hope that it will be useful, kitaev@213: * but WITHOUT ANY WARRANTY; without even the implied warranty of kitaev@213: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the kitaev@213: * GNU General Public License for more details. kitaev@213: * kitaev@213: * For information on how to redistribute this software under kitaev@213: * the terms of a license other than GNU General Public License kitaev@213: * contact TMate Software at support@hg4j.com kitaev@213: */ kitaev@213: package org.tmatesoft.hg.console; kitaev@213: kitaev@213: import java.util.HashMap; kitaev@213: import java.util.LinkedList; kitaev@213: import java.util.List; kitaev@213: import java.util.Map; kitaev@213: kitaev@213: import org.tmatesoft.hg.repo.HgLookup; kitaev@213: import org.tmatesoft.hg.repo.HgRepository; kitaev@213: kitaev@213: /** kitaev@213: * Parse command-line options. Primitive implementation that recognizes options with 0 or 1 argument. kitaev@213: * kitaev@213: * @author Artem Tikhomirov kitaev@213: * @author TMate Software Ltd. kitaev@213: */ kitaev@213: class Options { kitaev@213: kitaev@213: public final Map> opt2values = new HashMap>(); kitaev@213: kitaev@213: public boolean getBoolean(String... aliases) { kitaev@213: return getBoolean(false, aliases); kitaev@213: } kitaev@213: kitaev@213: public boolean getBoolean(boolean def, String... aliases) { kitaev@213: for (String s : aliases) { kitaev@213: if (opt2values.containsKey(s)) { kitaev@213: return true; kitaev@213: } kitaev@213: } kitaev@213: return def; kitaev@213: } kitaev@213: kitaev@213: public String getSingle(String... aliases) { kitaev@213: String rv = null; kitaev@213: for (String s : aliases) { kitaev@213: List values = opt2values.get(s); kitaev@213: if (values != null && values.size() > 0) { kitaev@213: rv = values.get(values.size() - 1); // take last one, most recent kitaev@213: } kitaev@213: } kitaev@213: return rv; kitaev@213: } kitaev@213: kitaev@213: public int getSingleInt(int def, String... aliases) { kitaev@213: String r = getSingle(aliases); kitaev@213: if (r == null) { kitaev@213: return def; kitaev@213: } kitaev@213: return Integer.parseInt(r); kitaev@213: } kitaev@213: kitaev@213: public List getList(String... aliases) { kitaev@213: LinkedList rv = new LinkedList(); kitaev@213: for (String s : aliases) { kitaev@213: List values = opt2values.get(s); kitaev@213: if (values != null) { kitaev@213: rv.addAll(values); kitaev@213: } kitaev@213: } kitaev@213: return rv; kitaev@213: } kitaev@213: kitaev@213: public HgRepository findRepository() throws Exception { kitaev@213: String repoLocation = getSingle("-R", "--repository"); kitaev@213: if (repoLocation != null) { kitaev@213: return new HgLookup().detect(repoLocation); kitaev@213: } kitaev@213: return new HgLookup().detectFromWorkingDir(); kitaev@213: } kitaev@213: kitaev@213: kitaev@213: public static Options parse(String[] commandLineArgs) { kitaev@213: Options rv = new Options(); kitaev@213: List values = new LinkedList(); kitaev@213: rv.opt2values.put("", values); // values with no options kitaev@213: for (String arg : commandLineArgs) { kitaev@213: if (arg.charAt(0) == '-') { kitaev@213: // option kitaev@213: if (arg.length() == 1) { kitaev@213: throw new IllegalArgumentException("Bad option: -"); kitaev@213: } kitaev@213: values = rv.opt2values.get(arg); kitaev@213: if (values == null) { kitaev@213: rv.opt2values.put(arg, values = new LinkedList()); kitaev@213: } kitaev@213: // next value, if any, gets into the values list for arg option. kitaev@213: } else { kitaev@213: values.add(arg); kitaev@213: values = rv.opt2values.get(""); kitaev@213: } kitaev@213: } kitaev@213: return rv; kitaev@213: } kitaev@213: }