From 15c17c4a02a49c9d2a53a3696ffa0d2b7fccbdc1 Mon Sep 17 00:00:00 2001 From: Bearmine Date: Sun, 30 Nov 2025 11:46:59 -0600 Subject: [PATCH] Change over to using command runner for arg parsing --- bin/adventofcode2025.dart | 26 ++++++-------------------- lib/day1/command.dart | 21 +++++++++++++++++++++ lib/day1/main.dart | 3 --- 3 files changed, 27 insertions(+), 23 deletions(-) create mode 100644 lib/day1/command.dart delete mode 100644 lib/day1/main.dart diff --git a/bin/adventofcode2025.dart b/bin/adventofcode2025.dart index d978f77..50dda13 100644 --- a/bin/adventofcode2025.dart +++ b/bin/adventofcode2025.dart @@ -1,29 +1,15 @@ 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 dayNumber = 'day'; void main(List arguments) { - final parser = ArgParser() - ..addFlag(help, negatable: false, abbr: 'h') - ..addOption(dayNumber, mandatory: true, abbr: "d"); - var results = parser.parse(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"); - } + var runner = + CommandRunner("adventofcode2025", "Advent of Code 2025 solutions") + ..addCommand(day1.Day1Command()) + ..run(arguments); } diff --git a/lib/day1/command.dart b/lib/day1/command.dart new file mode 100644 index 0000000..a94e2cd --- /dev/null +++ b/lib/day1/command.dart @@ -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); + } +} diff --git a/lib/day1/main.dart b/lib/day1/main.dart deleted file mode 100644 index b30eda8..0000000 --- a/lib/day1/main.dart +++ /dev/null @@ -1,3 +0,0 @@ -void run() { - print("Hello World Day 1"); -}