Skip to content

Commit 49aacb2

Browse files
committed
fix: make CLI return 0 for --version or --help options
Fix #1520
1 parent 8575a6b commit 49aacb2

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

src/main/java/com/adobe/epubcheck/tool/EpubChecker.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public class EpubChecker
9292
File listChecksOut;
9393
File customMessageFile;
9494
boolean listChecks = false;
95+
boolean displayHelpOrVersion = false;
9596
boolean useCustomMessageFile = false;
9697
boolean failOnWarnings = false;
9798
private Messages messages = Messages.getInstance();
@@ -138,6 +139,10 @@ public int run(String[] args)
138139
dumpMessageDictionary(report);
139140
return 0;
140141
}
142+
if (displayHelpOrVersion)
143+
{
144+
return 0;
145+
}
141146
if (useCustomMessageFile)
142147
{
143148
report.setCustomMessageFile(customMessageFile.getAbsolutePath());
@@ -487,7 +492,7 @@ private boolean processArguments(String[] args)
487492
setCustomMessageFileFromEnvironment();
488493

489494
Pattern argPattern = Pattern.compile("--?(.*)");
490-
495+
491496
for (int i = 0; i < args.length; i++)
492497
{
493498
Matcher argMatch = argPattern.matcher(args[i]);
@@ -738,9 +743,11 @@ else if (!fileName.startsWith("-"))
738743
case "?":
739744
case "help":
740745
displayHelp(); // display help message
746+
displayHelpOrVersion = true;
741747
break;
742748
case "version":
743749
displayVersion();
750+
displayHelpOrVersion = true;
744751
break;
745752
default:
746753
System.err.println(String.format(messages.get("unrecognized_argument"), args[i]));
@@ -789,7 +796,7 @@ else if (!fileName.startsWith("-"))
789796

790797
if (path == null)
791798
{
792-
if (listChecks)
799+
if (listChecks || displayHelpOrVersion)
793800
{
794801
return true;
795802
}

src/test/java/com/adobe/epubcheck/tools/CommandLineTest.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public void notfoundTest()
180180
* Validate that the -out parameter will generate a well formed xml output.
181181
*
182182
* @throws Exception
183-
* Any parsing errors will be thrown as an exception.
183+
* Any parsing errors will be thrown as an exception.
184184
*/
185185
@Test
186186
public void outputXMLReportTest()
@@ -206,7 +206,7 @@ public void outputXMLReportTest()
206206
* unpacked epubs.
207207
*
208208
* @throws Exception
209-
* Any parsing errors will be thrown as an exception.
209+
* Any parsing errors will be thrown as an exception.
210210
*/
211211
@Test
212212
public void outputXMLModeExpandedReportTest()
@@ -244,7 +244,7 @@ public void quietTest()
244244
* get a correct xml output report with the output flag.
245245
*
246246
* @throws Exception
247-
* Any parsing errors will be thrown as an exception.
247+
* Any parsing errors will be thrown as an exception.
248248
*/
249249
@Test
250250
public void quietRunWithOutputTest()
@@ -378,9 +378,8 @@ public void missingLocaleShouldFailTest()
378378
@Test
379379
public void helpMessageTest1()
380380
{
381-
runCommandLineTest(1, "-?");
382-
assertEquals("Command output not as expected", messages.get("no_file_specified"),
383-
errContent.toString().trim());
381+
runCommandLineTest(0, "-?");
382+
assertEquals("", errContent.toString().trim());
384383
String expected = String.format(messages.get("help_text").replaceAll("[\\s]+", " "),
385384
EpubCheck.version());
386385
String actual = outContent.toString();
@@ -395,9 +394,8 @@ public void helpMessageTest1()
395394
@Test
396395
public void helpMessageTest2()
397396
{
398-
runCommandLineTest(1, "-help");
399-
assertEquals("Command output not as expected", messages.get("no_file_specified"),
400-
errContent.toString().trim());
397+
runCommandLineTest(0, "-help");
398+
assertEquals("", errContent.toString().trim());
401399
String expected = String.format(messages.get("help_text").replaceAll("[\\s]+", " "),
402400
EpubCheck.version());
403401
String actual = outContent.toString();
@@ -412,9 +410,8 @@ public void helpMessageTest2()
412410
@Test
413411
public void helpMessageTest3()
414412
{
415-
runCommandLineTest(1, "--help");
416-
assertEquals("Command output not as expected", messages.get("no_file_specified"),
417-
errContent.toString().trim());
413+
runCommandLineTest(0, "--help");
414+
assertEquals("", errContent.toString().trim());
418415
String expected = String.format(messages.get("help_text").replaceAll("[\\s]+", " "),
419416
EpubCheck.version());
420417
String actual = outContent.toString();
@@ -428,9 +425,8 @@ public void helpMessageTest3()
428425
@Test
429426
public void versionDisplayTest1()
430427
{
431-
runCommandLineTest(1, "--version");
432-
assertEquals("Command output not as expected", messages.get("no_file_specified"),
433-
errContent.toString().trim());
428+
runCommandLineTest(0, "--version");
429+
assertEquals("", errContent.toString().trim());
434430
String expected = String.format(
435431
messages.get("epubcheck_version_text").replaceAll("[\\s]+", " "), EpubCheck.version());
436432
String actual = outContent.toString();
@@ -444,9 +440,8 @@ public void versionDisplayTest1()
444440
@Test
445441
public void versionDisplayTest2()
446442
{
447-
runCommandLineTest(1, "-version");
448-
assertEquals("Command output not as expected", messages.get("no_file_specified"),
449-
errContent.toString().trim());
443+
runCommandLineTest(0, "-version");
444+
assertEquals("", errContent.toString().trim());
450445
String expected = String.format(
451446
messages.get("epubcheck_version_text").replaceAll("[\\s]+", " "), EpubCheck.version());
452447
String actual = outContent.toString();
@@ -689,7 +684,7 @@ public void failOnWarningsTest()
689684
* document.
690685
*
691686
* @throws Exception
692-
* Throws an exception if the temp file can't be created.
687+
* Throws an exception if the temp file can't be created.
693688
*/
694689
@Test
695690
public void jsonFileTest()
@@ -714,7 +709,7 @@ public void jsonFileTest()
714709
* document.
715710
*
716711
* @throws Exception
717-
* Any parsing errors will be thrown as an exception.
712+
* Any parsing errors will be thrown as an exception.
718713
*/
719714
@Test
720715
public void xmlFileTest()
@@ -740,7 +735,7 @@ public void xmlFileTest()
740735
* document.
741736
*
742737
* @throws Exception
743-
* Any parsing errors will be thrown as an exception.
738+
* Any parsing errors will be thrown as an exception.
744739
*/
745740
@Test
746741
public void xmpFileTest()

0 commit comments

Comments
 (0)