Change over to using command runner for arg parsing
This commit is contained in:
@@ -1,29 +1,15 @@
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:adventofcode2025/day1/main.dart' as day1;
|
import 'package:adventofcode2025/day1/command.dart' as day1;
|
||||||
|
|
||||||
import 'package:args/args.dart';
|
import 'package:args/command_runner.dart';
|
||||||
|
|
||||||
const help = 'help';
|
const help = 'help';
|
||||||
const dayNumber = 'day';
|
const dayNumber = 'day';
|
||||||
|
|
||||||
void main(List<String> arguments) {
|
void main(List<String> arguments) {
|
||||||
final parser = ArgParser()
|
var runner =
|
||||||
..addFlag(help, negatable: false, abbr: 'h')
|
CommandRunner("adventofcode2025", "Advent of Code 2025 solutions")
|
||||||
..addOption(dayNumber, mandatory: true, abbr: "d");
|
..addCommand(day1.Day1Command())
|
||||||
var results = parser.parse(arguments);
|
..run(arguments);
|
||||||
|
|
||||||
if (results.flag(help)) {
|
|
||||||
print(parser.usage);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
var day = results.option(dayNumber);
|
|
||||||
|
|
||||||
switch (day) {
|
|
||||||
case '1':
|
|
||||||
day1.run();
|
|
||||||
default:
|
|
||||||
print("'$day' is not a valid value for option $dayNumber");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
21
lib/day1/command.dart
Normal file
21
lib/day1/command.dart
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import "package:args/command_runner.dart";
|
||||||
|
|
||||||
|
class Day1Command extends Command {
|
||||||
|
// The [name] and [description] properties must be defined by every
|
||||||
|
// subclass.
|
||||||
|
final name = "day1";
|
||||||
|
final description = "Run Advent of Code 2025 Day 1";
|
||||||
|
|
||||||
|
Day1Command() {
|
||||||
|
// we can add command specific arguments here.
|
||||||
|
// [argParser] is automatically created by the parent class.
|
||||||
|
}
|
||||||
|
|
||||||
|
// [run] may also return a Future.
|
||||||
|
@override
|
||||||
|
void run() {
|
||||||
|
// [argResults] is set before [run()] is called and contains the flags/options
|
||||||
|
// passed to this command.
|
||||||
|
print(argResults?.rest);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
void run() {
|
|
||||||
print("Hello World Day 1");
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user