Skip to content

Commit 992a027

Browse files
committed
added more CLI options. added better documentation for help
1 parent 83c15ad commit 992a027

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

cli/src/main/java/com/github/s4ke/moar/cli/Main.java

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,23 @@ public static void main(String[] args) throws ParseException, IOException {
2929
// create Options object
3030
Options options = new Options();
3131

32-
options.addOption( "rf", true, "regexFile" );
33-
options.addOption( "r", true, "regex" );
32+
options.addOption(
33+
"rf",
34+
true,
35+
"file containint the regexes to test against (multiple regexes are separated by one empty line)"
36+
);
37+
options.addOption( "r", true, "regex to test against" );
3438

35-
options.addOption( "mf", true, "moaFile" );
36-
options.addOption( "mo", true, "moaOutputFolder (overwrites if existent)" );
39+
options.addOption( "mf", true, "file/folder to read the MOA from" );
40+
options.addOption( "mo", true, "folder to export the MOAs to (overwrites if existent)" );
3741

38-
options.addOption( "sf", true, "stringFile" );
39-
options.addOption( "s", true, "string" );
42+
options.addOption( "sf", true, "file to read the input string(s) from" );
43+
options.addOption( "s", true, "string to test the MOA/Regex against" );
4044

41-
options.addOption( "m", false, "multiline" );
45+
options.addOption( "m", false, "multiline matching mode (search in string for regex)" );
46+
47+
options.addOption( "ls", false, "treat every line of the input string file as one string" );
48+
options.addOption( "t", false, "trim lines if -ls is set" );
4249

4350
options.addOption( "help", false, "prints this dialog" );
4451

@@ -76,7 +83,7 @@ public static void main(String[] args) throws ParseException, IOException {
7683
if ( emptyLineCountAfterRegex >= 1 ) {
7784
if ( regexStr.length() > 0 ) {
7885
patterns.add( MoaPattern.compile( regexStr.toString() ) );
79-
patternNames.add(regexStr.toString());
86+
patternNames.add( regexStr.toString() );
8087
}
8188
regexStr.setLength( 0 );
8289
emptyLineCountAfterRegex = 0;
@@ -93,7 +100,7 @@ public static void main(String[] args) throws ParseException, IOException {
93100
if ( regexStr.length() > 0 ) {
94101
try {
95102
patterns.add( MoaPattern.compile( regexStr.toString() ) );
96-
patternNames.add(regexStr.toString());
103+
patternNames.add( regexStr.toString() );
97104
}
98105
catch (Exception e) {
99106
System.out.println( e.getMessage() );
@@ -114,14 +121,14 @@ public static void main(String[] args) throws ParseException, IOException {
114121
for ( File moar : moarFiles ) {
115122
String jsonString = readWholeFile( moar );
116123
patterns.add( MoarJSONSerializer.fromJSON( jsonString ) );
117-
patternNames.add(moar.getAbsolutePath());
124+
patternNames.add( moar.getAbsolutePath() );
118125
}
119126
}
120127
else {
121128
System.out.println( fileName + " is a single file. using it directly (no check for *.moar suffix)" );
122129
String jsonString = readWholeFile( file );
123130
patterns.add( MoarJSONSerializer.fromJSON( jsonString ) );
124-
patternNames.add(fileName);
131+
patternNames.add( fileName );
125132
}
126133
}
127134

@@ -131,23 +138,36 @@ public static void main(String[] args) throws ParseException, IOException {
131138
}
132139

133140
if ( cmd.hasOption( "sf" ) ) {
141+
boolean treatLineAsString = cmd.hasOption( "ls" );
142+
boolean trim = cmd.hasOption( "t" );
134143
String fileName = cmd.getOptionValue( "sf" );
135144
StringBuilder stringBuilder = new StringBuilder();
136145
boolean firstLine = true;
137146
for ( String str : readFileContents( new File( fileName ) ) ) {
138-
if ( !firstLine ) {
139-
stringBuilder.append( "\n" );
147+
if ( treatLineAsString ) {
148+
if ( trim ) {
149+
str = str.trim();
150+
if ( str.length() == 0 ) {
151+
continue;
152+
}
153+
}
154+
stringsToCheck.add( str );
140155
}
141-
if ( firstLine ) {
142-
firstLine = false;
156+
else {
157+
if ( !firstLine ) {
158+
stringBuilder.append( "\n" );
159+
}
160+
if ( firstLine ) {
161+
firstLine = false;
162+
}
163+
stringBuilder.append( str );
143164
}
144-
stringBuilder.append( str );
145165
}
146-
stringsToCheck.add( stringBuilder.toString() );
166+
if ( !treatLineAsString ) {
167+
stringsToCheck.add( stringBuilder.toString() );
168+
}
147169
}
148170

149-
boolean multiLine = cmd.hasOption( "m" );
150-
151171
if ( patterns.size() == 0 ) {
152172
System.out.println( "no patterns to check" );
153173
return;
@@ -184,16 +204,18 @@ public static void main(String[] args) throws ParseException, IOException {
184204
return;
185205
}
186206

207+
boolean multiline = cmd.hasOption( "m" );
208+
187209
for ( String string : stringsToCheck ) {
188210
int curPattern = 0;
189211
for ( MoaPattern pattern : patterns ) {
190212
MoaMatcher matcher = pattern.matcher( string );
191-
if ( !multiLine ) {
213+
if ( !multiline ) {
192214
if ( matcher.matches() ) {
193-
System.out.println( "\"" + patternNames.get(curPattern) + "\" matches \"" + string + "\"" );
215+
System.out.println( "\"" + patternNames.get( curPattern ) + "\" matches \"" + string + "\"" );
194216
}
195217
else {
196-
System.out.println( "\"" + patternNames.get(curPattern) + "\" does not match \"" + string + "\"" );
218+
System.out.println( "\"" + patternNames.get( curPattern ) + "\" does not match \"" + string + "\"" );
197219
}
198220
}
199221
else {

json/src/main/java/com/github/s4ke/moar/json/MoarJSONSerializer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ else if ( state.isBound() ) {
202202
}
203203
else if ( state.isSet() ) {
204204
stateObj.put( "set", ((SetState) state).getStringRepresentation() );
205+
} else {
206+
throw new AssertionError();
205207
}
206208
stateArray.put( stateObj );
207209
}

0 commit comments

Comments
 (0)