MCPcopy Create free account
hub / github.com/cschanck/single-file-java / scanForFlag

Method scanForFlag

src/main/java/org/sfj/DumbCLIParse.java:128–149  ·  view source on GitHub ↗

Scan for the first argument with a param name that matches the argname. Scanning for arg "foo" looks for any of "--foo", "--not--foo", "--not-foo", "-foo", "-not--foo", "-not-foo". @param remaining list of remaining args to scan through @param argname argname to scan for, case sensitive @param foun

(List<String> remaining, String argname, boolean foundVal)

Source from the content-addressed store, hash-verified

126 * or if the flag is not seen, !foundVal will be returned.
127 */
128 public static boolean scanForFlag(List<String> remaining, String argname, boolean foundVal) {
129 // match --not--arg, or single dash versions
130 // case sensitive.
131 Pattern pattern = Pattern.compile(flagPattern(argname));
132 Optional<Matcher> got = new ArrayList<>(remaining).stream().map((s) -> {
133 Matcher ret = match(pattern, s);
134 if (ret != null) {
135 remaining.remove(s);
136 }
137 return ret;
138 }).filter(Objects::nonNull).limit(1).reduce((first, second) -> second);
139
140 if (got.isPresent()) {
141 String not = got.get().group(2);
142 if (not == null) {
143 return foundVal;
144 } else {
145 return !foundVal;
146 }
147 }
148 return !foundVal;
149 }
150
151 static class FlagArg {
152 private final String arg;

Callers 2

testFlagsMethod · 0.80
exampleIncrementalMethod · 0.80

Calls 5

flagPatternMethod · 0.95
matchMethod · 0.95
mapMethod · 0.80
getMethod · 0.65
removeMethod · 0.45

Tested by 2

testFlagsMethod · 0.64
exampleIncrementalMethod · 0.64