Skip to content

Commit 0bab256

Browse files
dfa1claude
andcommitted
refactor(cli): replace magic exit codes with ExitStatus constants
Introduce public ExitStatus class (OK=0, USAGE_ERROR=1, FILE_NOT_FOUND=2, ERROR=3) and replace bare numeric literals in all CLI command classes and VortexCli entry point. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 0203387 commit 0bab256

10 files changed

Lines changed: 49 additions & 37 deletions

File tree

cli/src/main/java/io/github/dfa1/vortex/cli/CountCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ private CountCommand() {
1414
static int run(String[] args) {
1515
if (args.length != 2) {
1616
System.err.println("usage: count <file.vortex>");
17-
return 1;
17+
return ExitStatus.USAGE_ERROR;
1818
}
1919
Path path = Path.of(args[1]);
2020
if (!Files.exists(path)) {
2121
System.err.println("file not found: " + path);
22-
return 2;
22+
return ExitStatus.FILE_NOT_FOUND;
2323
}
2424
try (VortexReader reader = VortexReader.open(path)) {
2525
System.out.println(reader.layout().rowCount());
26-
return 0;
26+
return ExitStatus.OK;
2727
} catch (IOException e) {
2828
System.err.println("error: " + e.getMessage());
29-
return 3;
29+
return ExitStatus.ERROR;
3030
}
3131
}
3232
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.github.dfa1.vortex.cli;
2+
3+
public final class ExitStatus {
4+
5+
public static final int OK = 0;
6+
public static final int USAGE_ERROR = 1;
7+
public static final int FILE_NOT_FOUND = 2;
8+
public static final int ERROR = 3;
9+
10+
private ExitStatus() {
11+
}
12+
}

cli/src/main/java/io/github/dfa1/vortex/cli/ExportCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ private ExportCommand() {
1818
static int run(String[] args) {
1919
if (args.length != 2) {
2020
System.err.println("usage: export <file.vortex>");
21-
return 1;
21+
return ExitStatus.USAGE_ERROR;
2222
}
2323
Path path = Path.of(args[1]);
2424
if (!Files.exists(path)) {
2525
System.err.println("file not found: " + path);
26-
return 2;
26+
return ExitStatus.FILE_NOT_FOUND;
2727
}
2828
try {
2929
Writer stdout = new OutputStreamWriter(System.out, StandardCharsets.UTF_8);
3030
CsvExporter.exportCsv(path, stdout, ExportOptions.defaults());
3131
stdout.flush();
32-
return 0;
32+
return ExitStatus.OK;
3333
} catch (IOException e) {
3434
System.err.println("error: " + e.getMessage());
35-
return 3;
35+
return ExitStatus.ERROR;
3636
}
3737
}
3838
}

cli/src/main/java/io/github/dfa1/vortex/cli/FilterCommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,31 @@ private record ParsedFilter(String column, Op op, Comparable<?> value) {}
4141
static int run(String[] args) {
4242
if (args.length < 3) {
4343
System.err.println("usage: filter <file.vortex> <expr> (e.g. \"price >= 100\")");
44-
return 1;
44+
return ExitStatus.USAGE_ERROR;
4545
}
4646
Path path = Path.of(args[1]);
4747
if (!Files.exists(path)) {
4848
System.err.println("file not found: " + path);
49-
return 2;
49+
return ExitStatus.FILE_NOT_FOUND;
5050
}
5151
String expr = String.join(" ", Arrays.asList(args).subList(2, args.length));
5252
ParsedFilter pf;
5353
try {
5454
pf = parseFilter(expr);
5555
} catch (IllegalArgumentException e) {
5656
System.err.println("error: " + e.getMessage());
57-
return 1;
57+
return ExitStatus.USAGE_ERROR;
5858
}
5959
ScanOptions scanOptions = new ScanOptions(List.of(), toRowFilter(pf), ScanOptions.NO_LIMIT);
6060
RowPredicate rowPred = toRowPredicate(pf);
6161
try {
6262
Writer stdout = new OutputStreamWriter(System.out, StandardCharsets.UTF_8);
6363
CsvExporter.exportCsvFiltered(path, stdout, ExportOptions.defaults(), scanOptions, rowPred);
6464
stdout.flush();
65-
return 0;
65+
return ExitStatus.OK;
6666
} catch (IOException e) {
6767
System.err.println("error: " + e.getMessage());
68-
return 3;
68+
return ExitStatus.ERROR;
6969
}
7070
}
7171

cli/src/main/java/io/github/dfa1/vortex/cli/ImportCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ private ImportCommand() {
1515
static int run(String[] args) {
1616
if (args.length < 2 || args.length > 3) {
1717
System.err.println("usage: import <file.csv> [out.vortex]");
18-
return 1;
18+
return ExitStatus.USAGE_ERROR;
1919
}
2020
Path csvPath = Path.of(args[1]);
2121
if (!Files.exists(csvPath)) {
2222
System.err.println("file not found: " + csvPath);
23-
return 2;
23+
return ExitStatus.FILE_NOT_FOUND;
2424
}
2525
Path vortexPath = args.length == 3 ? Path.of(args[2]) : deriveOutputPath(csvPath);
2626
ImportOptions options = ImportOptions.defaults()
@@ -35,11 +35,11 @@ static int run(String[] args) {
3535
String cascadingInfo = cascadingDepth > 0 ? String.format(", cascading depth %d", cascadingDepth) : "";
3636
System.out.printf("written: %s (%s → %s, %.1f%% smaller%s)%n",
3737
vortexPath, formatBytes(csvBytes), formatBytes(vortexBytes), savings * 100, cascadingInfo);
38-
return 0;
38+
return ExitStatus.OK;
3939
} catch (IOException e) {
4040
clearProgress();
4141
System.err.println("error: " + e.getMessage());
42-
return 3;
42+
return ExitStatus.ERROR;
4343
}
4444
}
4545

cli/src/main/java/io/github/dfa1/vortex/cli/InspectCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ private InspectCommand() {
1515
static int run(String[] args) {
1616
if (args.length != 2) {
1717
System.err.println("usage: inspect <file.vortex>");
18-
return 1;
18+
return ExitStatus.USAGE_ERROR;
1919
}
2020
Path path = Path.of(args[1]);
2121
if (!Files.exists(path)) {
2222
System.err.println("file not found: " + path);
23-
return 2;
23+
return ExitStatus.FILE_NOT_FOUND;
2424
}
2525
try (VortexReader reader = VortexReader.open(path)) {
2626
System.out.print(VortexInspector.inspect(reader));
27-
return 0;
27+
return ExitStatus.OK;
2828
} catch (IOException e) {
2929
System.err.println("error: " + e.getMessage());
30-
return 3;
30+
return ExitStatus.ERROR;
3131
}
3232
}
3333
}

cli/src/main/java/io/github/dfa1/vortex/cli/SchemaCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ private SchemaCommand() {
1515
static int run(String[] args) {
1616
if (args.length != 2) {
1717
System.err.println("usage: schema <file.vortex>");
18-
return 1;
18+
return ExitStatus.USAGE_ERROR;
1919
}
2020
Path path = Path.of(args[1]);
2121
if (!Files.exists(path)) {
2222
System.err.println("file not found: " + path);
23-
return 2;
23+
return ExitStatus.FILE_NOT_FOUND;
2424
}
2525
try (VortexReader reader = VortexReader.open(path)) {
2626
System.out.println(formatDType(reader.dtype()));
27-
return 0;
27+
return ExitStatus.OK;
2828
} catch (IOException e) {
2929
System.err.println("error: " + e.getMessage());
30-
return 3;
30+
return ExitStatus.ERROR;
3131
}
3232
}
3333

cli/src/main/java/io/github/dfa1/vortex/cli/SelectCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ private SelectCommand() {
2020
static int run(String[] args) {
2121
if (args.length < 3) {
2222
System.err.println("usage: select <file.vortex> <col1> [col2 ...]");
23-
return 1;
23+
return ExitStatus.USAGE_ERROR;
2424
}
2525
Path path = Path.of(args[1]);
2626
if (!Files.exists(path)) {
2727
System.err.println("file not found: " + path);
28-
return 2;
28+
return ExitStatus.FILE_NOT_FOUND;
2929
}
3030
List<String> columns = Arrays.asList(args).subList(2, args.length);
3131
ExportOptions options = ExportOptions.defaults().withColumns(columns);
3232
try {
3333
Writer stdout = new OutputStreamWriter(System.out, StandardCharsets.UTF_8);
3434
CsvExporter.exportCsv(path, stdout, options);
3535
stdout.flush();
36-
return 0;
36+
return ExitStatus.OK;
3737
} catch (IOException e) {
3838
System.err.println("error: " + e.getMessage());
39-
return 3;
39+
return ExitStatus.ERROR;
4040
}
4141
}
4242
}

cli/src/main/java/io/github/dfa1/vortex/cli/StatsCommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ private StatsCommand() {
1818
static int run(String[] args) {
1919
if (args.length != 2) {
2020
System.err.println("usage: stats <file.vortex>");
21-
return 1;
21+
return ExitStatus.USAGE_ERROR;
2222
}
2323
Path path = Path.of(args[1]);
2424
if (!Files.exists(path)) {
2525
System.err.println("file not found: " + path);
26-
return 2;
26+
return ExitStatus.FILE_NOT_FOUND;
2727
}
2828
try (VortexReader reader = VortexReader.open(path)) {
2929
if (!(reader.dtype() instanceof DType.Struct schema)) {
3030
System.err.println("error: root dtype is not a struct");
31-
return 3;
31+
return ExitStatus.ERROR;
3232
}
3333
long totalRows = reader.layout().rowCount();
3434
Map<String, ArrayStats> stats = reader.columnStats();
@@ -47,10 +47,10 @@ static int run(String[] args) {
4747
String max = s.max() != null ? s.max().toString() : "n/a";
4848
System.out.printf("%-20s %-12s %15s %15s%n", name, type, min, max);
4949
}
50-
return 0;
50+
return ExitStatus.OK;
5151
} catch (IOException e) {
5252
System.err.println("error: " + e.getMessage());
53-
return 3;
53+
return ExitStatus.ERROR;
5454
}
5555
}
5656

cli/src/main/java/io/github/dfa1/vortex/cli/VortexCli.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/// Entry point for the Vortex command-line tool.
66
///
7-
/// Exit codes: 0 = success, 1 = usage error, 2 = file not found, 3 = decode error.
7+
/// Exit codes: see {@link ExitStatus}.
88
public final class VortexCli {
99

1010
private VortexCli() {
@@ -13,7 +13,7 @@ private VortexCli() {
1313
public static void main(String[] args) {
1414
if (args.length == 0) {
1515
printUsage(System.err);
16-
System.exit(1);
16+
System.exit(ExitStatus.USAGE_ERROR);
1717
}
1818
int exit = switch (args[0]) {
1919
case "inspect" -> InspectCommand.run(args);
@@ -27,7 +27,7 @@ public static void main(String[] args) {
2727
default -> {
2828
System.err.println("unknown subcommand: " + args[0]);
2929
printUsage(System.err);
30-
yield 1;
30+
yield ExitStatus.USAGE_ERROR;
3131
}
3232
};
3333
System.exit(exit);

0 commit comments

Comments
 (0)