From e3ca83670a9c1c55ed3ac1bc5b3b18987afef499 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Sun, 18 Jun 2023 21:16:47 +0200 Subject: [PATCH] feat(example): Implement common argument parsing --- examples_dart/bin/omemo_client.dart | 40 ++++---------- examples_dart/lib/arguments.dart | 84 +++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 31 deletions(-) create mode 100644 examples_dart/lib/arguments.dart diff --git a/examples_dart/bin/omemo_client.dart b/examples_dart/bin/omemo_client.dart index 53bf7e3..5241d03 100644 --- a/examples_dart/bin/omemo_client.dart +++ b/examples_dart/bin/omemo_client.dart @@ -1,10 +1,9 @@ -import 'package:args/args.dart'; import 'package:chalkdart/chalk.dart'; import 'package:cli_repl/cli_repl.dart'; +import 'package:example_dart/arguments.dart'; import 'package:example_dart/socket.dart'; import 'package:logging/logging.dart'; import 'package:moxxmpp/moxxmpp.dart'; -import 'package:moxxmpp_socket_tcp/moxxmpp_socket_tcp.dart'; import 'package:omemo_dart/omemo_dart.dart' as omemo; void main(List args) async { @@ -17,43 +16,22 @@ void main(List args) async { ); }); - final parser = ArgParser() - ..addOption('jid') - ..addOption('password') - ..addOption('host') - ..addOption('port') - ..addOption('to') - ..addOption('xmpps-srv'); - final options = parser.parse(args); - - // Parse a potential xmpps-client SRV record here. - // Format: --xmpps-srv ,,, - MoxSrvRecord? srvRecord; - if (options['xmpps-srv'] != null) { - final parts = (options['xmpps-srv']! as String).split(','); - srvRecord = MoxSrvRecord( - int.parse(parts[0]), - int.parse(parts[1]), - parts[2], - int.parse(parts[3]), - ); + final parser = ArgumentParser() + ..parser.addOption('to', help: 'The JID to send messages to'); + final options = parser.handleArguments(args); + if (options == null) { + return; } // Connect - final jid = JID.fromString(options['jid']! as String); + final jid = parser.jid; final to = JID.fromString(options['to']! as String).toBare(); - final portString = options['port'] as String?; final connection = XmppConnection( TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), ClientToServerNegotiator(), - ExampleTCPSocketWrapper(srvRecord), - )..connectionSettings = ConnectionSettings( - jid: jid, - password: options['password']! as String, - host: options['host'] as String?, - port: portString != null ? int.parse(portString) : null, - ); + ExampleTCPSocketWrapper(parser.srvRecord), + )..connectionSettings = parser.connectionSettings; // Generate OMEMO data omemo.OmemoManager? oom; diff --git a/examples_dart/lib/arguments.dart b/examples_dart/lib/arguments.dart new file mode 100644 index 0000000..94a8f10 --- /dev/null +++ b/examples_dart/lib/arguments.dart @@ -0,0 +1,84 @@ +import 'package:args/args.dart'; +import 'package:chalkdart/chalk.dart'; +import 'package:moxxmpp/moxxmpp.dart'; +import 'package:moxxmpp_socket_tcp/moxxmpp_socket_tcp.dart'; + +extension StringToInt on String { + int toInt() => int.parse(this); +} + +/// A wrapper around [ArgParser] for providing convenience functions and standard parameters +/// to the examples. +class ArgumentParser { + ArgumentParser() { + parser + ..addOption('jid', help: 'The JID to connect as') + ..addOption('password', help: 'The password to use for authenticating') + ..addOption('host', + help: + 'The host address to connect to (By default uses the domain part of the JID)') + ..addOption('port', help: 'The port to connect to') + ..addOption('xmpps-srv', + help: + 'Inject a SRV record for _xmpps-client._tcp. Format: ,,,') + ..addFlag('help', + abbr: 'h', + negatable: false, + defaultsTo: false, + help: 'Show this help text'); + } + + /// The [ArgParser] that handles parsing the arguments. + final ArgParser parser = ArgParser(); + + /// The parsed options. Only valid after calling [handleArguments]. + late ArgResults options; + + ArgResults? handleArguments(List args) { + options = parser.parse(args); + if (options['help']!) { + print(parser.usage); + return null; + } + + if (options['jid'] == null) { + print(chalk.red('No JID specified')); + print(parser.usage); + return null; + } + + if (options['password'] == null) { + print(chalk.red('No password specified')); + print(parser.usage); + return null; + } + + return options; + } + + /// The JID to connect as. + JID get jid => JID.fromString(options['jid']!).toBare(); + + /// Construct connection settings from the parsed options. + ConnectionSettings get connectionSettings => ConnectionSettings( + jid: jid, + password: options['password']!, + host: options['host'], + port: (options['port'] as String?)?.toInt(), + ); + + /// Construct an xmpps-client SRV record for injection, if specified. + MoxSrvRecord? get srvRecord { + if (options['xmpps-srv'] == null) { + return null; + } + + final parts = options['xmpps-srv']!.split(','); + return MoxSrvRecord( + int.parse(parts[0]), + int.parse(parts[1]), + parts[2], + int.parse(parts[3]), + ); + } +}