Mercurial > hg4j
comparison cmdline/org/tmatesoft/hg/console/Options.java @ 299:45dc79e545f5
Recognize flag options (options with no arguments) in command line sample apps
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Sat, 17 Sep 2011 13:26:52 +0200 |
| parents | b9700740553a |
| children |
comparison
equal
deleted
inserted
replaced
| 298:aac0c3fab6ce | 299:45dc79e545f5 |
|---|---|
| 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.Collections; | |
| 19 import java.util.HashMap; | 20 import java.util.HashMap; |
| 20 import java.util.LinkedList; | 21 import java.util.LinkedList; |
| 21 import java.util.List; | 22 import java.util.List; |
| 22 import java.util.Map; | 23 import java.util.Map; |
| 24 import java.util.Set; | |
| 25 import java.util.TreeSet; | |
| 23 | 26 |
| 24 import org.tmatesoft.hg.repo.HgLookup; | 27 import org.tmatesoft.hg.repo.HgLookup; |
| 25 import org.tmatesoft.hg.repo.HgRepository; | 28 import org.tmatesoft.hg.repo.HgRepository; |
| 26 | 29 |
| 27 /** | 30 /** |
| 84 } | 87 } |
| 85 return new HgLookup().detectFromWorkingDir(); | 88 return new HgLookup().detectFromWorkingDir(); |
| 86 } | 89 } |
| 87 | 90 |
| 88 | 91 |
| 89 public static Options parse(String[] commandLineArgs) { | 92 public static Options parse(String[] commandLineArgs, Set<String> flagOptions) { |
| 90 Options rv = new Options(); | 93 Options rv = new Options(); |
| 91 List<String> values = new LinkedList<String>(); | 94 List<String> values = new LinkedList<String>(); |
| 92 rv.opt2values.put("", values); // values with no options | 95 rv.opt2values.put("", values); // values with no options |
| 93 for (String arg : commandLineArgs) { | 96 for (String arg : commandLineArgs) { |
| 94 if (arg.charAt(0) == '-') { | 97 if (arg.charAt(0) == '-') { |
| 95 // option | 98 // option |
| 96 if (arg.length() == 1) { | 99 if (arg.length() == 1) { |
| 97 throw new IllegalArgumentException("Bad option: -"); | 100 throw new IllegalArgumentException("Bad option: -"); |
| 98 } | 101 } |
| 99 values = rv.opt2values.get(arg); | 102 if (flagOptions.contains(arg)) { |
| 100 if (values == null) { | 103 rv.opt2values.put(arg, Collections.<String>emptyList()); |
| 101 rv.opt2values.put(arg, values = new LinkedList<String>()); | 104 values = rv.opt2values.get(""); |
| 105 } else { | |
| 106 values = rv.opt2values.get(arg); | |
| 107 if (values == null) { | |
| 108 rv.opt2values.put(arg, values = new LinkedList<String>()); | |
| 109 } | |
| 102 } | 110 } |
| 103 // next value, if any, gets into the values list for arg option. | 111 // next value, if any, gets into the values list for arg option. |
| 104 } else { | 112 } else { |
| 105 values.add(arg); | 113 values.add(arg); |
| 106 values = rv.opt2values.get(""); | 114 values = rv.opt2values.get(""); |
| 107 } | 115 } |
| 108 } | 116 } |
| 109 return rv; | 117 return rv; |
| 110 } | 118 } |
| 119 | |
| 120 public static Set<String> asSet(String... ss) { | |
| 121 TreeSet<String> rv = new TreeSet<String>(); | |
| 122 for (String s : ss) { | |
| 123 rv.add(s); | |
| 124 } | |
| 125 return rv; | |
| 126 } | |
| 111 } | 127 } |
