comparison cmdline/org/tmatesoft/hg/console/Options.java @ 143:b9700740553a

Command line tools parse and respect most of command-line arguments
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 17 Feb 2011 22:16:25 +0100
parents a3a2e5deb320
children 45dc79e545f5
comparison
equal deleted inserted replaced
142:37a34044e6bd 143:b9700740553a
14 * the terms of a license other than GNU General Public License 14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.console; 17 package org.tmatesoft.hg.console;
18 18
19 import java.util.ArrayList; 19 import java.util.HashMap;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.LinkedHashSet;
24 import java.util.LinkedList; 20 import java.util.LinkedList;
25 import java.util.List; 21 import java.util.List;
26 import java.util.Set; 22 import java.util.Map;
27 23
24 import org.tmatesoft.hg.repo.HgLookup;
28 import org.tmatesoft.hg.repo.HgRepository; 25 import org.tmatesoft.hg.repo.HgRepository;
29 import org.tmatesoft.hg.repo.HgLookup;
30 26
31 /** 27 /**
32 * Parse command-line options 28 * Parse command-line options. Primitive implementation that recognizes options with 0 or 1 argument.
33 * 29 *
34 * @author Artem Tikhomirov 30 * @author Artem Tikhomirov
35 * @author TMate Software Ltd. 31 * @author TMate Software Ltd.
36 */ 32 */
37 class Options { 33 class Options {
38 34
39 public String repoLocation; 35 public final Map<String,List<String>> opt2values = new HashMap<String, List<String>>();
40 public List<String> files; 36
41 public int limit = -1; 37 public boolean getBoolean(String... aliases) {
42 public Set<String> users; 38 return getBoolean(false, aliases);
43 public Set<String> branches; 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 }
44 79
45 public HgRepository findRepository() throws Exception { 80 public HgRepository findRepository() throws Exception {
81 String repoLocation = getSingle("-R", "--repository");
46 if (repoLocation != null) { 82 if (repoLocation != null) {
47 return new HgLookup().detect(repoLocation); 83 return new HgLookup().detect(repoLocation);
48 } 84 }
49 return new HgLookup().detectFromWorkingDir(); 85 return new HgLookup().detectFromWorkingDir();
50 } 86 }
51 87
52 88
53 public static Options parse(String[] commandLineArgs) { 89 public static Options parse(String[] commandLineArgs) {
54 Options rv = new Options(); 90 Options rv = new Options();
55 List<String> args = Arrays.asList(commandLineArgs); 91 List<String> values = new LinkedList<String>();
56 LinkedList<String> files = new LinkedList<String>(); 92 rv.opt2values.put("", values); // values with no options
57 for (Iterator<String> it = args.iterator(); it.hasNext(); ) { 93 for (String arg : commandLineArgs) {
58 String arg = it.next();
59 if (arg.charAt(0) == '-') { 94 if (arg.charAt(0) == '-') {
60 // option 95 // option
61 if (arg.length() == 1) { 96 if (arg.length() == 1) {
62 throw new IllegalArgumentException("Bad option: -"); 97 throw new IllegalArgumentException("Bad option: -");
63 } 98 }
64 switch ((int) arg.charAt(1)) { 99 values = rv.opt2values.get(arg);
65 case (int) 'R' : { 100 if (values == null) {
66 if (! it.hasNext()) { 101 rv.opt2values.put(arg, values = new LinkedList<String>());
67 throw new IllegalArgumentException("Need repo location");
68 }
69 rv.repoLocation = it.next();
70 break;
71 }
72 case (int) 'l' : {
73 if (!it.hasNext()) {
74 throw new IllegalArgumentException();
75 }
76 rv.limit = Integer.parseInt(it.next());
77 break;
78 }
79 case (int) 'u' : {
80 if (rv.users == null) {
81 rv.users = new LinkedHashSet<String>();
82 }
83 rv.users.add(it.next());
84 break;
85 }
86 case (int) 'b' : {
87 if (rv.branches == null) {
88 rv.branches = new LinkedHashSet<String>();
89 }
90 rv.branches.add(it.next());
91 break;
92 }
93 } 102 }
103 // next value, if any, gets into the values list for arg option.
94 } else { 104 } else {
95 // filename 105 values.add(arg);
96 files.add(arg); 106 values = rv.opt2values.get("");
97 } 107 }
98 }
99 if (!files.isEmpty()) {
100 rv.files = new ArrayList<String>(files);
101 } else {
102 rv.files = Collections.emptyList();
103 } 108 }
104 return rv; 109 return rv;
105 } 110 }
106 } 111 }