Skip to content

Commit c918303

Browse files
committed
[sfntdiff] Fix exit codes
Fixes #348
1 parent 1bd1066 commit c918303

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

c/sfntdiff/source/Dmain.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ static void printUsage(void) {
5454
/* Show usage information */
5555
static void showUsage(void) {
5656
printUsage();
57-
quit(0);
5857
}
5958

6059
/* Show usage and help information */
@@ -85,7 +84,6 @@ static void showHelp(void) {
8584
" e.g., -i cmap,name\n"
8685
" will inspect/compare ONLY the 'cmap' and 'name' tables\n");
8786
printf(" -i and -x switches are exclusive of each other.\n");
88-
quit(1);
8987
}
9088

9189
/* Main program */
@@ -115,16 +113,20 @@ IntN main(IntN argc, Byte8 *argv[]) {
115113

116114
argi = opt_Scan(argc, argv, opt_NOPTS(opt), opt, NULL, NULL);
117115

116+
if (opt_Present("-u") || opt_Present("-h")) return 0;
117+
118118
if (opt_Present("-x") && opt_Present("-i")) {
119119
printf("ERROR: '-x' switch and '-i' switch are exclusive of each other.\n");
120120
showUsage();
121+
return 1;
121122
}
122123

123124
if (level > 4) level = 4;
124125

125126
if ((argc - argi) < 2) {
126127
printf("ERROR: not enough files/directories specified.\n");
127128
showUsage();
129+
return 1;
128130
}
129131

130132
filename1 = argv[argi];
@@ -303,8 +305,7 @@ IntN main(IntN argc, Byte8 *argv[]) {
303305
} else {
304306
printf("ERROR: Incorrect/insufficient files/directories specified.\n");
305307
showUsage();
308+
return 1;
306309
}
307-
308-
/*printf( "\nDone.\n");*/
309310
return 0;
310311
}

c/sfntdiff/source/Dopt.c

+15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <stdio.h>
1010
#include <string.h>
11+
#include <stdbool.h>
1112
#include <stdlib.h>
1213
#include "Dopt.h"
1314

@@ -99,6 +100,16 @@ void opt_Error(int error, opt_Option *opt, char *arg) {
99100
global.error += global.handler(error, opt, arg, global.client);
100101
}
101102

103+
bool is_known_option(char *arg) {
104+
const char *known_options[] = {"-u", "-h", "-T", "-d", "-x", "-i", "-X"};
105+
int num_opts = sizeof(known_options) / sizeof(known_options[0]);
106+
int i;
107+
for (i = 0; i < num_opts; i++)
108+
if (strcmp(arg, known_options[i]) == 0)
109+
return true;
110+
return false;
111+
}
112+
102113
/* Process argument list */
103114
int opt_Scan(int argc, char *argv[],
104115
int nOpts, opt_Option *opts, opt_Handler handler, void *client) {
@@ -138,6 +149,10 @@ int opt_Scan(int argc, char *argv[],
138149
argi = 1;
139150
while (argi < argc) {
140151
char *arg = argv[argi];
152+
if (arg[0] == '-' && !is_known_option(arg)) {
153+
opt_Error(opt_Unknown, NULL, arg);
154+
exit(1);
155+
}
141156
opt_Option *opt = lookup(arg, matchWhole);
142157
if (opt != NULL)
143158
/* Whole argument matched option */

tests/sfntdiff_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import print_function, division, absolute_import
22

33
import os
4+
import subprocess32 as subprocess
45
import pytest
56

67
from .runner import main as runner
@@ -19,6 +20,21 @@ def _get_expected_path(file_name):
1920
# Tests
2021
# -----
2122

23+
def test_exit_no_option():
24+
# When ran by itself, 'sfntdiff' prints the usage
25+
assert subprocess.call([TOOL]) == 1
26+
27+
28+
@pytest.mark.parametrize('arg', ['-h', '-u'])
29+
def test_exit_known_option(arg):
30+
assert subprocess.call([TOOL, arg]) == 0
31+
32+
33+
@pytest.mark.parametrize('arg', ['-j', '--bogus'])
34+
def test_exit_unknown_option(arg):
35+
assert subprocess.call([TOOL, arg]) == 1
36+
37+
2238
@pytest.mark.parametrize('args, txt_filename', [
2339
([], 'dflt.txt'),
2440
(['T'], 'dflt.txt'), # default diff with timestamp

0 commit comments

Comments
 (0)