diff --git a/bin/dart_kassakuitti_cli.dart b/bin/dart_kassakuitti_cli.dart index 843f83a..8cc6472 100644 --- a/bin/dart_kassakuitti_cli.dart +++ b/bin/dart_kassakuitti_cli.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:args/args.dart'; import 'read_ean_products.dart'; import 'ean_products_2_csv.dart'; import 'specific/s_kaupat/ean_handler.dart'; @@ -16,23 +17,29 @@ void main(List arguments) async { var parser = getParser(); var argResults = parser.parse(arguments); - if (argResults.command?.name == ArgSelector.help.value!) { + await handleArgCommands(argResults, parser); +} + +/// Handles the commands in the arguments. +Future handleArgCommands(ArgResults argResults, ArgParser parser) async { + // Help command + if (argResults.command?.name == ArgSelector.help.value) { print('Help:\n${parser.usage}'); - return; - } else if (argResults.command?.name == ArgSelector.run.value!) { + } + // Run command + else if (argResults.command?.name == ArgSelector.run.value) { print('\nRunning...\n'); - var selectedTextFile = argResults[ArgSelector.textFile.value!] as String?; - var selectedHtmlFile = argResults[ArgSelector.htmlFile.value!] as String; - var selectedStore = - argResults[ArgSelector.foodOnlineStore.value!] as String; - var csvFilesPath = argResults[ArgSelector.csvPath.value!] as String; + var selectedTextFile = argResults[ArgSelector.textFile.value] as String?; + var selectedHtmlFile = argResults[ArgSelector.htmlFile.value] as String; + var selectedStore = argResults[ArgSelector.foodOnlineStore.value] as String; + var csvFilesPath = argResults[ArgSelector.csvPath.value] as String; printSelectedValues( selectedTextFile, selectedHtmlFile, selectedStore, csvFilesPath); try { - if (ShopSelector.sKaupat.isEqual(selectedStore)) { + if (ShopSelector.sKaupat.value == selectedStore) { var receiptProducts = await readReceiptProducts(selectedTextFile!, csvFilesPath); var eanProducts = await readEANProducts( @@ -42,7 +49,7 @@ void main(List arguments) async { receiptProducts2CSV(receiptProducts, csvFilesPath); eanProducts2CSV(eanProducts, csvFilesPath, ShopSelector.sKaupat.name); - } else if (ShopSelector.kRuoka.isEqual(selectedStore)) { + } else if (ShopSelector.kRuoka.value == selectedStore) { var eanProducts = await readEANProducts( selectedHtmlFile, ShopSelector.kRuoka, csvFilesPath); @@ -57,7 +64,9 @@ void main(List arguments) async { } print('\nDone!'); - } else { - await printBasicInfo(); + } + // Empty command (or other commands, e.g. 'moro' / 'hello') + else { + await printBasicInfo(parser); } } diff --git a/bin/ean_products_2_csv.dart b/bin/ean_products_2_csv.dart index 285e492..22ea95b 100644 --- a/bin/ean_products_2_csv.dart +++ b/bin/ean_products_2_csv.dart @@ -6,11 +6,11 @@ void eanProducts2CSV( List eanProductList, String csvFilePath, String shopSelector) { var csv = StringBuffer(); - csv.write('EAN code;Name;Quantity;Total price;Price per unit;More details\n'); + csv.write('Name;Quantity;Price per unit;Total price;EAN code;More details\n'); for (var item in eanProductList) { csv.write( - '${item.ean};${item.name};${item.quantity};${item.totalPrice};${item.pricePerUnit};${item.moreDetails}\n'); + '${item.name};${item.quantity};${item.pricePerUnit};${item.totalPrice};${item.ean};${item.moreDetails}\n'); } var file = File( diff --git a/bin/specific/s_kaupat/strings_to_receipt_products.dart b/bin/specific/s_kaupat/strings_to_receipt_products.dart index f450f5a..c965523 100644 --- a/bin/specific/s_kaupat/strings_to_receipt_products.dart +++ b/bin/specific/s_kaupat/strings_to_receipt_products.dart @@ -57,7 +57,8 @@ List strings2ReceiptProducts(List rows) { } /* A campaign row - (i.e. usually means that there's a mistake in the previous line): + (i.e. usually means that there's a mistake in the previous row + BUT not always -> let's assume that it's a discount row): */ else if (item.contains('kampanja')) { var items = item.split(RegExp(r'\s{12,33}')); @@ -81,6 +82,7 @@ List strings2ReceiptProducts(List rows) { .replaceAll(RegExp(r'\.'), ','); lastProduct.pricePerUnit = fixedPricePerUnit; + lastProduct.discountCounted = 'yes'; } lastProduct.totalPrice = fixedPriceAsString; diff --git a/bin/utils/arg_selector_helper.dart b/bin/utils/arg_selector_helper.dart index 03c88a1..aace76e 100644 --- a/bin/utils/arg_selector_helper.dart +++ b/bin/utils/arg_selector_helper.dart @@ -1,30 +1,14 @@ -/// ArgSelector (textFile, htmlFile, foodOnlineStore, csvPath, help) +/// ArgSelector (textFile, htmlFile, foodOnlineStore, csvPath, help, run) enum ArgSelector { - textFile, - htmlFile, - foodOnlineStore, - csvPath, - help, - run, -} - -extension ArgSelectorExtension on ArgSelector { - static const values = { - ArgSelector.textFile: 'text', - ArgSelector.htmlFile: 'html', - ArgSelector.foodOnlineStore: 'store', - ArgSelector.csvPath: 'csv', - ArgSelector.help: 'help', - ArgSelector.run: 'run', - }; + textFile('text'), + htmlFile('html'), + foodOnlineStore('store'), + csvPath('csv'), + help('help'), + run('run'); - String? get value => values[this]; + final String term; + const ArgSelector(this.term); - bool isEqual(dynamic value) { - if (value is String) { - return toString() == value || this.value == value; - } else { - return false; - } - } + String get value => term; } diff --git a/bin/utils/parse_kassakuitti_arguments.dart b/bin/utils/parse_kassakuitti_arguments.dart index 647f41c..a8b21c2 100644 --- a/bin/utils/parse_kassakuitti_arguments.dart +++ b/bin/utils/parse_kassakuitti_arguments.dart @@ -5,30 +5,30 @@ import 'shop_selector_helper.dart'; ArgParser getParser() { final parser = ArgParser() - ..addCommand(ArgSelector.run.value!) + ..addCommand(ArgSelector.run.value) ..addOption( - ArgSelector.textFile.value!, - abbr: ArgSelector.textFile.value!.substring(0, 1), + ArgSelector.textFile.value, + abbr: ArgSelector.textFile.value.substring(0, 1), help: 'Text file (cash receipt) to read', ) ..addOption( - ArgSelector.htmlFile.value!, - abbr: ArgSelector.htmlFile.value!.substring(0, 1), + ArgSelector.htmlFile.value, + abbr: ArgSelector.htmlFile.value.substring(0, 1), help: 'HTML (EAN products) file to read', ) ..addOption( - ArgSelector.foodOnlineStore.value!, - abbr: ArgSelector.foodOnlineStore.value!.substring(0, 1), + ArgSelector.foodOnlineStore.value, + abbr: ArgSelector.foodOnlineStore.value.substring(0, 1), help: 'Food online store', defaultsTo: ShopSelector.sKaupat.value, - allowed: [ShopSelector.sKaupat.value!, ShopSelector.kRuoka.value!], + allowed: [ShopSelector.sKaupat.value, ShopSelector.kRuoka.value], ) ..addOption( - ArgSelector.csvPath.value!, - abbr: ArgSelector.csvPath.value!.substring(0, 1), + ArgSelector.csvPath.value, + abbr: ArgSelector.csvPath.value.substring(0, 1), help: 'Path for output CSV files', ) - ..addCommand(ArgSelector.help.value!); + ..addCommand(ArgSelector.help.value); return parser; } diff --git a/bin/utils/printing_helper.dart b/bin/utils/printing_helper.dart index d85315b..a24aeca 100644 --- a/bin/utils/printing_helper.dart +++ b/bin/utils/printing_helper.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:args/args.dart'; import 'package:path/path.dart'; import 'package:yaml/yaml.dart'; @@ -12,7 +13,7 @@ void printSelectedValues(String? selectedTextFile, String selectedHtmlFile, '\n- Path where to save CSV files:\t\t$csvFilesPath\n'); } -Future printBasicInfo() async { +Future printBasicInfo(ArgParser parser) async { String pathToYaml = join(dirname(Platform.script.toFilePath()), '../pubspec.yaml'); var file = File(pathToYaml); @@ -24,11 +25,5 @@ Future printBasicInfo() async { print('Version: ${yaml['version']}'); print('Homepage: ${yaml['homepage']}'); - print('''\nTo get help, run: - - dart run bin/dart_kassakuitti_cli.dart help - - or when using alias: - - kassakuitti help\n'''); + print('\nHelp:\n${parser.usage}'); } diff --git a/bin/utils/shop_selector_helper.dart b/bin/utils/shop_selector_helper.dart index 87adec8..0c1c46a 100644 --- a/bin/utils/shop_selector_helper.dart +++ b/bin/utils/shop_selector_helper.dart @@ -1,22 +1,10 @@ /// Shop selector (sKaupat, kRuoka) enum ShopSelector { - sKaupat, - kRuoka, -} - -extension ShopSelectorExtension on ShopSelector { - static const values = { - ShopSelector.sKaupat: 'S-kaupat', - ShopSelector.kRuoka: 'K-ruoka', - }; + sKaupat('S-kaupat'), + kRuoka('K-ruoka'); - String? get value => values[this]; + final String term; + const ShopSelector(this.term); - bool isEqual(dynamic value) { - if (value is String) { - return toString() == value || this.value == value; - } else { - return false; - } - } + String get value => term; } diff --git a/pubspec.lock b/pubspec.lock index a61a8d0..7c87edd 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -259,7 +259,7 @@ packages: name: lints url: "https://pub.dartlang.org" source: hosted - version: "1.0.1" + version: "2.0.0" logging: dependency: transitive description: @@ -429,4 +429,4 @@ packages: source: hosted version: "3.1.0" sdks: - dart: ">=2.16.2 <3.0.0" + dart: ">=2.17.1 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 5021b96..b0f8a2f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,10 +1,10 @@ name: dart_kassakuitti_cli description: A simple Dart CLI app to handle a cash receipt coming from S-kaupat or K-ruoka (two Finnish food online stores). -version: 0.13.0 +version: 0.14.0 homepage: https://github.com/areee/dart_kassakuitti_cli environment: - sdk: '>=2.16.2 <3.0.0' + sdk: '>=2.17.1 <3.0.0' # dependencies: @@ -13,7 +13,7 @@ environment: dev_dependencies: build_runner: ^2.1.10 hive_generator: ^1.1.2 - lints: ^1.0.1 + lints: ^2.0.0 dependencies: ansicolor: ^2.0.1 args: ^2.3.0