Mercurial > jhg
comparison hg4j-cli/src/main/java/org/tmatesoft/hg/console/Options.java @ 213:6ec4af642ba8 gradle
Project uses Gradle for build - actual changes
| author | Alexander Kitaev <kitaev@gmail.com> |
|---|---|
| date | Tue, 10 May 2011 10:52:53 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 212:edb2e2829352 | 213:6ec4af642ba8 |
|---|---|
| 1 /* | |
| 2 * Copyright (c) 2011 TMate Software Ltd | |
| 3 * | |
| 4 * This program is free software; you can redistribute it and/or modify | |
| 5 * it under the terms of the GNU General Public License as published by | |
| 6 * the Free Software Foundation; version 2 of the License. | |
| 7 * | |
| 8 * This program is distributed in the hope that it will be useful, | |
| 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 11 * GNU General Public License for more details. | |
| 12 * | |
| 13 * For information on how to redistribute this software under | |
| 14 * the terms of a license other than GNU General Public License | |
| 15 * contact TMate Software at support@hg4j.com | |
| 16 */ | |
| 17 package org.tmatesoft.hg.console; | |
| 18 | |
| 19 import java.util.HashMap; | |
| 20 import java.util.LinkedList; | |
| 21 import java.util.List; | |
| 22 import java.util.Map; | |
| 23 | |
| 24 import org.tmatesoft.hg.repo.HgLookup; | |
| 25 import org.tmatesoft.hg.repo.HgRepository; | |
| 26 | |
| 27 /** | |
| 28 * Parse command-line options. Primitive implementation that recognizes options with 0 or 1 argument. | |
| 29 * | |
| 30 * @author Artem Tikhomirov | |
| 31 * @author TMate Software Ltd. | |
| 32 */ | |
| 33 class Options { | |
| 34 | |
| 35 public final Map<String,List<String>> opt2values = new HashMap<String, List<String>>(); | |
| 36 | |
| 37 public boolean getBoolean(String... aliases) { | |
| 38 return getBoolean(false, aliases); | |
| 39 } | |
| 40 | |
| 41 public boolean getBoolean(boolean def, String... aliases) { | |
| 42 for (String s : aliases) { | |
| 43 if (opt2values.containsKey(s)) { | |
| 44 return true; | |
| 45 } | |
| 46 } | |
| 47 return def; | |
| 48 } | |
| 49 | |
| 50 public String getSingle(String... aliases) { | |
| 51 String rv = null; | |
| 52 for (String s : aliases) { | |
| 53 List<String> values = opt2values.get(s); | |
| 54 if (values != null && values.size() > 0) { | |
| 55 rv = values.get(values.size() - 1); // take last one, most recent | |
| 56 } | |
| 57 } | |
| 58 return rv; | |
| 59 } | |
| 60 | |
| 61 public int getSingleInt(int def, String... aliases) { | |
| 62 String r = getSingle(aliases); | |
| 63 if (r == null) { | |
| 64 return def; | |
| 65 } | |
| 66 return Integer.parseInt(r); | |
| 67 } | |
| 68 | |
| 69 public List<String> getList(String... aliases) { | |
| 70 LinkedList<String> rv = new LinkedList<String>(); | |
| 71 for (String s : aliases) { | |
| 72 List<String> values = opt2values.get(s); | |
| 73 if (values != null) { | |
| 74 rv.addAll(values); | |
| 75 } | |
| 76 } | |
| 77 return rv; | |
| 78 } | |
| 79 | |
| 80 public HgRepository findRepository() throws Exception { | |
| 81 String repoLocation = getSingle("-R", "--repository"); | |
| 82 if (repoLocation != null) { | |
| 83 return new HgLookup().detect(repoLocation); | |
| 84 } | |
| 85 return new HgLookup().detectFromWorkingDir(); | |
| 86 } | |
| 87 | |
| 88 | |
| 89 public static Options parse(String[] commandLineArgs) { | |
| 90 Options rv = new Options(); | |
| 91 List<String> values = new LinkedList<String>(); | |
| 92 rv.opt2values.put("", values); // values with no options | |
| 93 for (String arg : commandLineArgs) { | |
| 94 if (arg.charAt(0) == '-') { | |
| 95 // option | |
| 96 if (arg.length() == 1) { | |
| 97 throw new IllegalArgumentException("Bad option: -"); | |
| 98 } | |
| 99 values = rv.opt2values.get(arg); | |
| 100 if (values == null) { | |
| 101 rv.opt2values.put(arg, values = new LinkedList<String>()); | |
| 102 } | |
| 103 // next value, if any, gets into the values list for arg option. | |
| 104 } else { | |
| 105 values.add(arg); | |
| 106 values = rv.opt2values.get(""); | |
| 107 } | |
| 108 } | |
| 109 return rv; | |
| 110 } | |
| 111 } |
