|
34 | 34 | @ApiStatus.Internal |
35 | 35 | public final class OptionParser { |
36 | 36 |
|
37 | | - private final Flag[] flags; |
38 | | - private final Option[] options; |
39 | | - private final Set<String> arguments; |
40 | | - private final Set<String> parsedFlags; |
41 | | - private final Map<String, List<String>> parsedOptions; |
42 | | - |
43 | | - public OptionParser(String[] arguments, Method method) { |
44 | | - this.flags = method.getAnnotationsByType(Flag.class); |
45 | | - this.options = method.getAnnotationsByType(Option.class); |
46 | | - this.arguments = new HashSet<>(Arrays.asList(arguments)); |
47 | | - this.parsedFlags = new HashSet<>(); |
48 | | - this.parsedOptions = new HashMap<>(); |
49 | | - } |
50 | | - |
51 | | - public Map<String, List<String>> parseOptions() { |
52 | | - for (Option option : options) { |
53 | | - this.parseOption(option); |
54 | | - } |
55 | | - |
56 | | - return this.parsedOptions; |
57 | | - } |
58 | | - |
59 | | - public Set<String> parseFlags() { |
60 | | - for (Flag flag : flags) { |
61 | | - this.parseFlag(flag); |
62 | | - } |
63 | | - |
64 | | - return this.parsedFlags; |
65 | | - } |
66 | | - |
67 | | - private void parseOption(Option option) { |
68 | | - String prefix = option.prefix(); |
69 | | - String keySeparator = Pattern.quote(option.keySeparator()); |
70 | | - String valueSeparator = Pattern.quote(option.valueSeparator()); |
71 | | - Iterator<String> iterator = arguments.iterator(); |
72 | | - |
73 | | - while (iterator.hasNext()) { |
74 | | - String argument = iterator.next(); |
75 | | - |
76 | | - if (!argument.startsWith(prefix)) { |
77 | | - continue; |
78 | | - } |
79 | | - |
80 | | - if (option.allowSeparating() && !argument.contains(option.keySeparator())) { |
81 | | - continue; |
82 | | - } |
83 | | - |
84 | | - String[] options = argument.substring(prefix.length()).split(keySeparator); |
85 | | - |
86 | | - if (!option.allowSeparating()) { |
87 | | - if (options.length < 2) { |
88 | | - continue; |
89 | | - } |
90 | | - |
91 | | - String value = options[1]; |
92 | | - this.parsedOptions.put(option.value(), Collections.singletonList(value)); |
93 | | - |
94 | | - iterator.remove(); |
95 | | - continue; |
96 | | - } |
97 | | - |
98 | | - String[] values = options[1].split(valueSeparator); |
99 | | - this.parsedOptions.put(option.value(), Arrays.asList(values)); |
100 | | - |
101 | | - iterator.remove(); |
102 | | - } |
103 | | - } |
104 | | - |
105 | | - private void parseFlag(Flag flag) { |
106 | | - String prefix = flag.prefix(); |
107 | | - |
108 | | - outer: |
109 | | - for (String argument : this.arguments) { |
110 | | - if (!argument.startsWith(prefix)) { |
111 | | - continue; |
112 | | - } |
113 | | - |
114 | | - String foundFlag = argument.substring(prefix.length()); |
115 | | - |
116 | | - for (String flagName : flag.value()) { |
117 | | - if (!flagName.equals(foundFlag)) { |
118 | | - continue; |
119 | | - } |
120 | | - |
121 | | - this.parsedFlags.add(flagName); |
122 | | - continue outer; |
123 | | - } |
124 | | - } |
125 | | - } |
| 37 | + private final Flag[] flags; |
| 38 | + private final Option[] options; |
| 39 | + private final Set<String> arguments; |
| 40 | + private final Set<String> parsedFlags; |
| 41 | + private final Map<String, List<String>> parsedOptions; |
| 42 | + |
| 43 | + public OptionParser(String[] arguments, Method method) { |
| 44 | + this.flags = method.getAnnotationsByType(Flag.class); |
| 45 | + this.options = method.getAnnotationsByType(Option.class); |
| 46 | + this.arguments = new HashSet<>(Arrays.asList(arguments)); |
| 47 | + this.parsedFlags = new HashSet<>(); |
| 48 | + this.parsedOptions = new HashMap<>(); |
| 49 | + } |
| 50 | + |
| 51 | + public Map<String, List<String>> parseOptions() { |
| 52 | + for (Option option : options) { |
| 53 | + this.parseOption(option); |
| 54 | + } |
| 55 | + |
| 56 | + return this.parsedOptions; |
| 57 | + } |
| 58 | + |
| 59 | + public Set<String> parseFlags() { |
| 60 | + for (Flag flag : flags) { |
| 61 | + this.parseFlag(flag); |
| 62 | + } |
| 63 | + |
| 64 | + return this.parsedFlags; |
| 65 | + } |
| 66 | + |
| 67 | + private void parseOption(Option option) { |
| 68 | + String prefix = option.prefix(); |
| 69 | + String keySeparator = option.keySeparator(); |
| 70 | + String valueSeparator = option.valueSeparator(); |
| 71 | + |
| 72 | + Iterator<String> iterator = arguments.iterator(); |
| 73 | + |
| 74 | + while (iterator.hasNext()) { |
| 75 | + String argument = iterator.next(); |
| 76 | + |
| 77 | + if (!argument.startsWith(prefix)) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + String raw = argument.substring(prefix.length()); |
| 82 | + |
| 83 | + if (!raw.startsWith(option.value() + keySeparator)) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + String valuePart = raw.substring((option.value() + keySeparator).length()); |
| 88 | + |
| 89 | + if (!option.allowSeparating()) { |
| 90 | + this.parsedOptions.put(option.value(), Collections.singletonList(valuePart)); |
| 91 | + } else { |
| 92 | + this.parsedOptions.put( |
| 93 | + option.value(), |
| 94 | + Arrays.asList(valuePart.split(Pattern.quote(valueSeparator))) |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + iterator.remove(); |
| 99 | + return; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private void parseFlag(Flag flag) { |
| 104 | + String prefix = flag.prefix(); |
| 105 | + Iterator<String> iterator = this.arguments.iterator(); |
| 106 | + |
| 107 | + while (iterator.hasNext()) { |
| 108 | + String argument = iterator.next(); |
| 109 | + |
| 110 | + if (!argument.startsWith(prefix)) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + String foundFlag = argument.substring(prefix.length()); |
| 115 | + |
| 116 | + for (String flagName : flag.value()) { |
| 117 | + if (flagName.equals(foundFlag)) { |
| 118 | + this.parsedFlags.add(flagName); |
| 119 | + iterator.remove(); |
| 120 | + return; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
126 | 125 | } |
0 commit comments