From f779283a95f93bc9d3ed6f46a7f4abe064d3aa6d Mon Sep 17 00:00:00 2001 From: Eric Seidel Date: Sat, 30 Sep 2023 17:18:09 -0700 Subject: [PATCH] refactor: teach runOffline about ArgParser --- packages/cli/bin/calc_price_ranges.dart | 2 +- .../cli/bin/closest_system_to_explore.dart | 2 +- packages/cli/bin/db_stats.dart | 2 +- packages/cli/bin/debug_mount.dart | 2 +- packages/cli/bin/debug_surveys.dart | 2 +- packages/cli/bin/earning_per_ship.dart | 4 +-- packages/cli/bin/earning_rate.dart | 2 +- packages/cli/bin/find_mounts.dart | 2 +- packages/cli/bin/list_fleet.dart | 6 ++-- packages/cli/bin/list_inventory_value.dart | 2 +- packages/cli/bin/list_market_prices.dart | 2 +- packages/cli/bin/list_market_scores.dart | 2 +- packages/cli/bin/list_nearby_buys_for.dart | 2 +- .../cli/bin/list_repeated_transactions.dart | 2 +- packages/cli/bin/list_ship_prices.dart | 2 +- packages/cli/bin/list_trade_volumes.dart | 2 +- packages/cli/bin/list_transactions.dart | 3 +- packages/cli/bin/network_execute.dart | 2 +- packages/cli/bin/network_fill.dart | 2 +- packages/cli/bin/plan_route.dart | 3 +- packages/cli/bin/recent_deals.dart | 4 +-- packages/cli/bin/route_perf.dart | 2 +- packages/cli/bin/show_contracts.dart | 21 +++++++------ .../bin/systems_reachable_from_factions.dart | 2 +- packages/cli/lib/cli.dart | 31 ++----------------- 25 files changed, 44 insertions(+), 64 deletions(-) diff --git a/packages/cli/bin/calc_price_ranges.dart b/packages/cli/bin/calc_price_ranges.dart index 93d7bba9..284ba80c 100644 --- a/packages/cli/bin/calc_price_ranges.dart +++ b/packages/cli/bin/calc_price_ranges.dart @@ -20,7 +20,7 @@ void printPriceRanges(List gameStats) { } } -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { const fs = LocalFileSystem(); final prices = MarketPrices.load(fs); diff --git a/packages/cli/bin/closest_system_to_explore.dart b/packages/cli/bin/closest_system_to_explore.dart index 907f8c70..1af05c5f 100644 --- a/packages/cli/bin/closest_system_to_explore.dart +++ b/packages/cli/bin/closest_system_to_explore.dart @@ -3,7 +3,7 @@ import 'package:cli/cache/caches.dart'; import 'package:cli/cli.dart'; import 'package:types/types.dart'; -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final shipCache = ShipCache.loadCached(fs)!; final ship = shipCache.ships.first; final systemsCache = SystemsCache.loadCached(fs)!; diff --git a/packages/cli/bin/db_stats.dart b/packages/cli/bin/db_stats.dart index 61913644..b2ec80e9 100644 --- a/packages/cli/bin/db_stats.dart +++ b/packages/cli/bin/db_stats.dart @@ -1,7 +1,7 @@ import 'package:cli/cli.dart'; import 'package:db/db.dart'; -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final db = await defaultDatabase(); final connection = db.connection; diff --git a/packages/cli/bin/debug_mount.dart b/packages/cli/bin/debug_mount.dart index fbffedf2..9121a7f6 100644 --- a/packages/cli/bin/debug_mount.dart +++ b/packages/cli/bin/debug_mount.dart @@ -5,7 +5,7 @@ import 'package:cli/cache/caches.dart'; import 'package:cli/cli.dart'; import 'package:types/types.dart'; -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final shipCache = ShipCache.loadCached(fs)!; final behaviorCache = BehaviorCache.load(fs); final marketPrices = MarketPrices.load(fs); diff --git a/packages/cli/bin/debug_surveys.dart b/packages/cli/bin/debug_surveys.dart index cad9488f..cfd06009 100644 --- a/packages/cli/bin/debug_surveys.dart +++ b/packages/cli/bin/debug_surveys.dart @@ -4,7 +4,7 @@ import 'package:cli/cli.dart'; import 'package:db/db.dart'; import 'package:types/types.dart'; -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final db = await defaultDatabase(); final marketPrices = MarketPrices.load(fs); final agentCache = AgentCache.loadCached(fs)!; diff --git a/packages/cli/bin/earning_per_ship.dart b/packages/cli/bin/earning_per_ship.dart index 4af55a3a..532ff5f9 100644 --- a/packages/cli/bin/earning_per_ship.dart +++ b/packages/cli/bin/earning_per_ship.dart @@ -52,10 +52,10 @@ Behavior? behaviorFromFrame(Ship ship) { }[ship.frame.symbol]; } -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { // For a given ship, show the credits per minute averaged over the // last hour. - final lookbackMinutesString = args.firstOrNull; + final lookbackMinutesString = argResults.rest.firstOrNull; final lookbackMinutes = lookbackMinutesString != null ? int.parse(lookbackMinutesString) : 180; final lookback = Duration(minutes: lookbackMinutes); diff --git a/packages/cli/bin/earning_rate.dart b/packages/cli/bin/earning_rate.dart index 29326a32..f30de1dd 100644 --- a/packages/cli/bin/earning_rate.dart +++ b/packages/cli/bin/earning_rate.dart @@ -11,7 +11,7 @@ int hoursAgo(DateTime time) { return DateTime.now().difference(time).inHours; } -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final db = await defaultDatabase(); // Credits per hour. final oneDayAgoAsHour = diff --git a/packages/cli/bin/find_mounts.dart b/packages/cli/bin/find_mounts.dart index 4ecb9c13..c7aa322b 100644 --- a/packages/cli/bin/find_mounts.dart +++ b/packages/cli/bin/find_mounts.dart @@ -4,7 +4,7 @@ import 'package:cli/nav/route.dart'; import 'package:cli/printing.dart'; import 'package:cli/trading.dart'; -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final marketPrices = MarketPrices.load(fs); final systemsCache = SystemsCache.loadCached(fs)!; final routePlanner = RoutePlanner.fromSystemsCache(systemsCache); diff --git a/packages/cli/bin/list_fleet.dart b/packages/cli/bin/list_fleet.dart index 8626ecf9..ba2c652b 100644 --- a/packages/cli/bin/list_fleet.dart +++ b/packages/cli/bin/list_fleet.dart @@ -104,8 +104,8 @@ bool Function(Ship) filterFromArgs(List args) { return (ship) => ship.symbol == symbol; } -Future command(FileSystem fs, List args) async { - final filter = filterFromArgs(args); +Future command(FileSystem fs, ArgResults argResults) async { + final filter = filterFromArgs(argResults.rest); final behaviorCache = BehaviorCache.load(fs); final shipCache = ShipCache.loadCached(fs)!; @@ -114,7 +114,7 @@ Future command(FileSystem fs, List args) async { final matchingShips = ships.where(filter).toList(); if (matchingShips.isEmpty) { logger - ..info('No ships matching ${args.firstOrNull}.') + ..info('No ships matching ${argResults.rest.firstOrNull}.') ..info('Usage: list_fleet [ship_symbol]') ..info('Example: list_fleet ${shipCache.ships.first.symbol}'); return; diff --git a/packages/cli/bin/list_inventory_value.dart b/packages/cli/bin/list_inventory_value.dart index 6f20fde1..630b19c4 100644 --- a/packages/cli/bin/list_inventory_value.dart +++ b/packages/cli/bin/list_inventory_value.dart @@ -7,7 +7,7 @@ Future main(List args) async { await runOffline(args, command); } -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final shipCache = ShipCache.loadCached(fs)!; final marketPrices = MarketPrices.load(fs); final countByTradeSymbol = {}; diff --git a/packages/cli/bin/list_market_prices.dart b/packages/cli/bin/list_market_prices.dart index ea241634..6555e816 100644 --- a/packages/cli/bin/list_market_prices.dart +++ b/packages/cli/bin/list_market_prices.dart @@ -5,7 +5,7 @@ import 'package:cli/cache/market_prices.dart'; import 'package:cli/cli.dart'; import 'package:cli/printing.dart'; -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final marketPrices = MarketPrices.load(fs); logger.info( diff --git a/packages/cli/bin/list_market_scores.dart b/packages/cli/bin/list_market_scores.dart index ec4974c4..d5766471 100644 --- a/packages/cli/bin/list_market_scores.dart +++ b/packages/cli/bin/list_market_scores.dart @@ -2,7 +2,7 @@ import 'package:cli/cache/market_prices.dart'; import 'package:cli/cli.dart'; import 'package:cli/market_scores.dart'; -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final marketPrices = MarketPrices.load(fs); final topTen = scoreMarketSystems(marketPrices, limit: 10); for (final entry in topTen.entries) { diff --git a/packages/cli/bin/list_nearby_buys_for.dart b/packages/cli/bin/list_nearby_buys_for.dart index a6b7aada..ba024778 100644 --- a/packages/cli/bin/list_nearby_buys_for.dart +++ b/packages/cli/bin/list_nearby_buys_for.dart @@ -4,7 +4,7 @@ import 'package:cli/nav/route.dart'; import 'package:cli/printing.dart'; import 'package:cli/trading.dart'; -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final marketPrices = MarketPrices.load(fs); final systemsCache = SystemsCache.loadCached(fs)!; final routePlanner = RoutePlanner.fromSystemsCache(systemsCache); diff --git a/packages/cli/bin/list_repeated_transactions.dart b/packages/cli/bin/list_repeated_transactions.dart index de1666c9..066cd5d7 100644 --- a/packages/cli/bin/list_repeated_transactions.dart +++ b/packages/cli/bin/list_repeated_transactions.dart @@ -11,7 +11,7 @@ void printDiffs(List data) { logger.info(diffs.toString()); } -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final db = await defaultDatabase(); final transactions = await allTransactions(db); // final marketPrices = MarketPrices.load(fs); diff --git a/packages/cli/bin/list_ship_prices.dart b/packages/cli/bin/list_ship_prices.dart index 72fd4fa8..4c71426e 100644 --- a/packages/cli/bin/list_ship_prices.dart +++ b/packages/cli/bin/list_ship_prices.dart @@ -5,7 +5,7 @@ import 'package:cli/cache/shipyard_prices.dart'; import 'package:cli/cli.dart'; import 'package:cli/printing.dart'; -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final shipyardPrices = ShipyardPrices.load(fs); logger.info( diff --git a/packages/cli/bin/list_trade_volumes.dart b/packages/cli/bin/list_trade_volumes.dart index dbff1e2f..e6709a81 100644 --- a/packages/cli/bin/list_trade_volumes.dart +++ b/packages/cli/bin/list_trade_volumes.dart @@ -5,7 +5,7 @@ void main(List args) async { await runOffline(args, command); } -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final marketPrices = MarketPrices.load(fs); final tradeVolumesBySymbol = >{}; for (final price in marketPrices.prices) { diff --git a/packages/cli/bin/list_transactions.dart b/packages/cli/bin/list_transactions.dart index dc3e3bfc..80228914 100644 --- a/packages/cli/bin/list_transactions.dart +++ b/packages/cli/bin/list_transactions.dart @@ -8,7 +8,8 @@ String describeTransaction(Transaction t) { '${t.shipSymbol} ${t.waypointSymbol} ${t.creditChange} ${t.accounting}'; } -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { + final args = argResults.rest; final shipNumber = args.firstOrNull; final shipSymbol = ShipSymbol.fromString('ESEIDEL-$shipNumber'); diff --git a/packages/cli/bin/network_execute.dart b/packages/cli/bin/network_execute.dart index 4ab24adc..200435d8 100644 --- a/packages/cli/bin/network_execute.dart +++ b/packages/cli/bin/network_execute.dart @@ -160,7 +160,7 @@ class NetExecutor { } } -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final connection = await defaultDatabase(); await NetExecutor(connection).run(); } diff --git a/packages/cli/bin/network_fill.dart b/packages/cli/bin/network_fill.dart index 3eba5619..3752e082 100644 --- a/packages/cli/bin/network_fill.dart +++ b/packages/cli/bin/network_fill.dart @@ -4,7 +4,7 @@ import 'package:cli/cli.dart'; import 'package:cli/net/queue.dart'; import 'package:db/db.dart'; -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final db = await defaultDatabase(); final queue = NetQueue(db, QueueRole.requestor); diff --git a/packages/cli/bin/plan_route.dart b/packages/cli/bin/plan_route.dart index 1793f5af..cdb31e91 100644 --- a/packages/cli/bin/plan_route.dart +++ b/packages/cli/bin/plan_route.dart @@ -54,7 +54,8 @@ void planRouteAndLog( } } -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { + final args = argResults.rest; if (args.length != 2) { logger.err('Usage: plan_route START END'); return; diff --git a/packages/cli/bin/recent_deals.dart b/packages/cli/bin/recent_deals.dart index cf947c4e..c395d4d8 100644 --- a/packages/cli/bin/recent_deals.dart +++ b/packages/cli/bin/recent_deals.dart @@ -101,9 +101,9 @@ bool Function(Transaction t) filterFromArgs(List args) { return (Transaction t) => t.shipSymbol == shipSymbol; } -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final db = await defaultDatabase(); - final filter = filterFromArgs(args); + final filter = filterFromArgs(argResults.rest); final deals = []; final openDeals = >{}; final ignoredTransactions = []; diff --git a/packages/cli/bin/route_perf.dart b/packages/cli/bin/route_perf.dart index 025d21db..db2146e8 100644 --- a/packages/cli/bin/route_perf.dart +++ b/packages/cli/bin/route_perf.dart @@ -22,7 +22,7 @@ class Result { final Duration duration; } -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { const count = 5000; final db = await defaultDatabase(); diff --git a/packages/cli/bin/show_contracts.dart b/packages/cli/bin/show_contracts.dart index b5b5a5d6..e2165f96 100644 --- a/packages/cli/bin/show_contracts.dart +++ b/packages/cli/bin/show_contracts.dart @@ -1,4 +1,3 @@ -import 'package:args/args.dart'; import 'package:cli/behavior/trader.dart'; import 'package:cli/cache/contract_cache.dart'; import 'package:cli/cache/market_prices.dart'; @@ -58,12 +57,16 @@ Future command(FileSystem fs, ArgResults argResults) async { } void main(List args) async { - await runOfflineArgs(args, command, (parser) { - parser.addFlag( - 'all', - abbr: 'a', - help: 'Print all contracts, not just active ones.', - negatable: false, - ); - }); + await runOffline( + args, + command, + addArgs: (parser) { + parser.addFlag( + 'all', + abbr: 'a', + help: 'Print all contracts, not just active ones.', + negatable: false, + ); + }, + ); } diff --git a/packages/cli/bin/systems_reachable_from_factions.dart b/packages/cli/bin/systems_reachable_from_factions.dart index b686e3c2..fd21df79 100644 --- a/packages/cli/bin/systems_reachable_from_factions.dart +++ b/packages/cli/bin/systems_reachable_from_factions.dart @@ -3,7 +3,7 @@ import 'package:cli/cli.dart'; import 'package:db/db.dart'; import 'package:types/types.dart'; -Future command(FileSystem fs, List args) async { +Future command(FileSystem fs, ArgResults argResults) async { final db = await defaultDatabase(); final systemsCache = await SystemsCache.load(fs); final factionsApi = FactionsApi(); diff --git a/packages/cli/lib/cli.dart b/packages/cli/lib/cli.dart index cd72859a..8f19a0bf 100644 --- a/packages/cli/lib/cli.dart +++ b/packages/cli/lib/cli.dart @@ -4,41 +4,16 @@ import 'package:cli/logger.dart'; import 'package:file/local.dart'; import 'package:scoped/scoped.dart'; +export 'package:args/args.dart'; export 'package:cli/logger.dart'; export 'package:file/file.dart'; /// Run command with a logger, but without an Api. Future runOffline( List args, - Future Function(FileSystem fs, List args) fn, -) async { - final parser = ArgParser() - ..addFlag( - 'verbose', - abbr: 'v', - help: 'Verbose logging', - negatable: false, - ); - final results = parser.parse(args); - const fs = LocalFileSystem(); - return runScoped( - () async { - if (results['verbose'] as bool) { - setVerboseLogging(); - } - return fn(fs, results.rest); - }, - values: {loggerRef}, - ); -} - -/// Run command with a logger, and ArgParser. -// This should replace runOffline for all callers. -Future runOfflineArgs( - List args, - Future Function(FileSystem fs, ArgResults argResults) fn, + Future Function(FileSystem fs, ArgResults argResults) fn, { void Function(ArgParser parser)? addArgs, -) async { +}) async { final parser = ArgParser() ..addFlag( 'verbose',