comparison cmdline/org/tmatesoft/hg/console/Options.java @ 74:6f1b88693d48

Complete refactoring to org.tmatesoft
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 24 Jan 2011 03:14:45 +0100
parents
children bcd31a4c638a
comparison
equal deleted inserted replaced
73:0d279bcc4442 74:6f1b88693d48
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@svnkit.com
16 */
17 package org.tmatesoft.hg.console;
18
19 import java.util.ArrayList;
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;
25 import java.util.List;
26 import java.util.Set;
27
28 import org.tmatesoft.hg.repo.HgRepository;
29 import org.tmatesoft.hg.repo.Lookup;
30
31 /**
32 * Parse command-line options
33 *
34 * @author Artem Tikhomirov
35 * @author TMate Software Ltd.
36 */
37 class Options {
38
39 public String repoLocation;
40 public List<String> files;
41 public int limit = -1;
42 public Set<String> users;
43 public Set<String> branches;
44
45 public HgRepository findRepository() throws Exception {
46 if (repoLocation != null) {
47 return new Lookup().detect(repoLocation);
48 }
49 return new Lookup().detectFromWorkingDir();
50 }
51
52
53 public static Options parse(String[] commandLineArgs) {
54 Options rv = new Options();
55 List<String> args = Arrays.asList(commandLineArgs);
56 LinkedList<String> files = new LinkedList<String>();
57 for (Iterator<String> it = args.iterator(); it.hasNext(); ) {
58 String arg = it.next();
59 if (arg.charAt(0) == '-') {
60 // option
61 if (arg.length() == 1) {
62 throw new IllegalArgumentException("Bad option: -");
63 }
64 switch ((int) arg.charAt(1)) {
65 case (int) 'R' : {
66 if (! it.hasNext()) {
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 }
94 } else {
95 // filename
96 files.add(arg);
97 }
98 }
99 if (!files.isEmpty()) {
100 rv.files = new ArrayList<String>(files);
101 } else {
102 rv.files = Collections.emptyList();
103 }
104 return rv;
105 }
106 }