mirror of
https://github.com/HenkKalkwater/aoc-2020
synced 2024-11-22 11:05:18 +00:00
Allow reading input from other files than stdin
This commit is contained in:
parent
8ca791d3d0
commit
9161a33154
|
@ -6,6 +6,6 @@ Run "dub".
|
|||
|
||||
|
||||
HOW TO RUN:
|
||||
./oac-2020 [day] [part]
|
||||
./oac-2020 [DAY] [PART] <INPUT_FILE> <DAY_SPECIFIC_ARGUMENTS...>
|
||||
|
||||
The program reads it input from stdin.
|
||||
The program reads it input from stdin if INPUT_FILE is not specified or if INPUT_FILE is equal to "-".
|
||||
|
|
19
source/app.d
19
source/app.d
|
@ -1,6 +1,7 @@
|
|||
import core.stdc.stdlib;
|
||||
|
||||
import std.conv;
|
||||
import std.exception;
|
||||
import std.format;
|
||||
import std.stdio;
|
||||
import std.getopt;
|
||||
|
@ -28,7 +29,10 @@ void printUsage(string name) {
|
|||
}
|
||||
|
||||
void printUsage(string name, string message) {
|
||||
stderr.writeln("USAGE: %s [day] [part]".format(name));
|
||||
stderr.writeln("USAGE: %s [DAY] [PART] <INPUT_FILE> ".format(name));
|
||||
stderr.writeln(" DAY = int between 1 and 25 (inclusive), specifiying the day to run");
|
||||
stderr.writeln(" PART = int between 1 and 2 (inclusive), specifiying the part to run");
|
||||
stderr.writeln(" INPUT_FILE = file to read from. '-' or missing implies stdin");
|
||||
if (message != null) {
|
||||
stderr.writeln(message);
|
||||
}
|
||||
|
@ -57,8 +61,19 @@ void main(string[] args) {
|
|||
printUsage(args[0], "[part] is not an integer");
|
||||
}
|
||||
|
||||
File file;
|
||||
if (args.length < 4 || args[3] == "-") {
|
||||
file = stdin;
|
||||
} else {
|
||||
try {
|
||||
file = File(args[3], "rb");
|
||||
} catch (ErrnoException e) {
|
||||
printUsage(args[0], "Error %d while opening input file: %s".format(e.errno, e.message));
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Variant result = programs[day - 1](part, stdin, args[3..$]);
|
||||
Variant result = programs[day - 1](part, file, args[3..$]);
|
||||
writeln(result);
|
||||
} catch(ArgumentException e) {
|
||||
printUsage(args[0], e.msg);
|
||||
|
|
Loading…
Reference in a new issue