Skip to content

Commit

Permalink
refactor: teach runOffline about ArgParser
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Oct 1, 2023
1 parent d9d3791 commit f779283
Show file tree
Hide file tree
Showing 25 changed files with 44 additions and 64 deletions.
2 changes: 1 addition & 1 deletion packages/cli/bin/calc_price_ranges.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void printPriceRanges(List<MarketPrice> gameStats) {
}
}

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
const fs = LocalFileSystem();
final prices = MarketPrices.load(fs);

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/closest_system_to_explore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:cli/cache/caches.dart';
import 'package:cli/cli.dart';
import 'package:types/types.dart';

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final shipCache = ShipCache.loadCached(fs)!;
final ship = shipCache.ships.first;
final systemsCache = SystemsCache.loadCached(fs)!;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/db_stats.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:cli/cli.dart';
import 'package:db/db.dart';

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();

final connection = db.connection;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/debug_mount.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:cli/cache/caches.dart';
import 'package:cli/cli.dart';
import 'package:types/types.dart';

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final shipCache = ShipCache.loadCached(fs)!;
final behaviorCache = BehaviorCache.load(fs);
final marketPrices = MarketPrices.load(fs);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/debug_surveys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:cli/cli.dart';
import 'package:db/db.dart';
import 'package:types/types.dart';

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();
final marketPrices = MarketPrices.load(fs);
final agentCache = AgentCache.loadCached(fs)!;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/bin/earning_per_ship.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ Behavior? behaviorFromFrame(Ship ship) {
}[ship.frame.symbol];
}

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> 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);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/earning_rate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int hoursAgo(DateTime time) {
return DateTime.now().difference(time).inHours;
}

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();
// Credits per hour.
final oneDayAgoAsHour =
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/find_mounts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:cli/nav/route.dart';
import 'package:cli/printing.dart';
import 'package:cli/trading.dart';

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final marketPrices = MarketPrices.load(fs);
final systemsCache = SystemsCache.loadCached(fs)!;
final routePlanner = RoutePlanner.fromSystemsCache(systemsCache);
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/bin/list_fleet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ bool Function(Ship) filterFromArgs(List<String> args) {
return (ship) => ship.symbol == symbol;
}

Future<void> command(FileSystem fs, List<String> args) async {
final filter = filterFromArgs(args);
Future<void> command(FileSystem fs, ArgResults argResults) async {
final filter = filterFromArgs(argResults.rest);
final behaviorCache = BehaviorCache.load(fs);
final shipCache = ShipCache.loadCached(fs)!;

Expand All @@ -114,7 +114,7 @@ Future<void> command(FileSystem fs, List<String> 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;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/list_inventory_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Future<void> main(List<String> args) async {
await runOffline(args, command);
}

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final shipCache = ShipCache.loadCached(fs)!;
final marketPrices = MarketPrices.load(fs);
final countByTradeSymbol = <TradeSymbol, int>{};
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/list_market_prices.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:cli/cache/market_prices.dart';
import 'package:cli/cli.dart';
import 'package:cli/printing.dart';

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final marketPrices = MarketPrices.load(fs);

logger.info(
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/list_market_scores.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:cli/cache/market_prices.dart';
import 'package:cli/cli.dart';
import 'package:cli/market_scores.dart';

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final marketPrices = MarketPrices.load(fs);
final topTen = scoreMarketSystems(marketPrices, limit: 10);
for (final entry in topTen.entries) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/list_nearby_buys_for.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:cli/nav/route.dart';
import 'package:cli/printing.dart';
import 'package:cli/trading.dart';

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final marketPrices = MarketPrices.load(fs);
final systemsCache = SystemsCache.loadCached(fs)!;
final routePlanner = RoutePlanner.fromSystemsCache(systemsCache);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/list_repeated_transactions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void printDiffs(List<int> data) {
logger.info(diffs.toString());
}

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();
final transactions = await allTransactions(db);
// final marketPrices = MarketPrices.load(fs);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/list_ship_prices.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:cli/cache/shipyard_prices.dart';
import 'package:cli/cli.dart';
import 'package:cli/printing.dart';

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final shipyardPrices = ShipyardPrices.load(fs);

logger.info(
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/list_trade_volumes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ void main(List<String> args) async {
await runOffline(args, command);
}

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final marketPrices = MarketPrices.load(fs);
final tradeVolumesBySymbol = <TradeSymbol, Set<int>>{};
for (final price in marketPrices.prices) {
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/bin/list_transactions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ String describeTransaction(Transaction t) {
'${t.shipSymbol} ${t.waypointSymbol} ${t.creditChange} ${t.accounting}';
}

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final args = argResults.rest;
final shipNumber = args.firstOrNull;
final shipSymbol = ShipSymbol.fromString('ESEIDEL-$shipNumber');

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/network_execute.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class NetExecutor {
}
}

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final connection = await defaultDatabase();
await NetExecutor(connection).run();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/network_fill.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:cli/cli.dart';
import 'package:cli/net/queue.dart';
import 'package:db/db.dart';

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();
final queue = NetQueue(db, QueueRole.requestor);

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/bin/plan_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ void planRouteAndLog(
}
}

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final args = argResults.rest;
if (args.length != 2) {
logger.err('Usage: plan_route START END');
return;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/bin/recent_deals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ bool Function(Transaction t) filterFromArgs(List<String> args) {
return (Transaction t) => t.shipSymbol == shipSymbol;
}

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();
final filter = filterFromArgs(args);
final filter = filterFromArgs(argResults.rest);
final deals = <SyntheticDeal>[];
final openDeals = <ShipSymbol, List<Transaction>>{};
final ignoredTransactions = <Transaction>[];
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/route_perf.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Result {
final Duration duration;
}

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
const count = 5000;

final db = await defaultDatabase();
Expand Down
21 changes: 12 additions & 9 deletions packages/cli/bin/show_contracts.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -58,12 +57,16 @@ Future<void> command(FileSystem fs, ArgResults argResults) async {
}

void main(List<String> 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,
);
},
);
}
2 changes: 1 addition & 1 deletion packages/cli/bin/systems_reachable_from_factions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:cli/cli.dart';
import 'package:db/db.dart';
import 'package:types/types.dart';

Future<void> command(FileSystem fs, List<String> args) async {
Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();
final systemsCache = await SystemsCache.load(fs);
final factionsApi = FactionsApi();
Expand Down
31 changes: 3 additions & 28 deletions packages/cli/lib/cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<R> runOffline<R>(
List<String> args,
Future<R> Function(FileSystem fs, List<String> 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<R> runOfflineArgs<R>(
List<String> args,
Future<R> Function(FileSystem fs, ArgResults argResults) fn,
Future<R> Function(FileSystem fs, ArgResults argResults) fn, {
void Function(ArgParser parser)? addArgs,
) async {
}) async {
final parser = ArgParser()
..addFlag(
'verbose',
Expand Down

0 comments on commit f779283

Please sign in to comment.