feat(example): Implement common argument parsing
This commit is contained in:
parent
fbbe413148
commit
e3ca83670a
@ -1,10 +1,9 @@
|
|||||||
import 'package:args/args.dart';
|
|
||||||
import 'package:chalkdart/chalk.dart';
|
import 'package:chalkdart/chalk.dart';
|
||||||
import 'package:cli_repl/cli_repl.dart';
|
import 'package:cli_repl/cli_repl.dart';
|
||||||
|
import 'package:example_dart/arguments.dart';
|
||||||
import 'package:example_dart/socket.dart';
|
import 'package:example_dart/socket.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:moxxmpp/moxxmpp.dart';
|
import 'package:moxxmpp/moxxmpp.dart';
|
||||||
import 'package:moxxmpp_socket_tcp/moxxmpp_socket_tcp.dart';
|
|
||||||
import 'package:omemo_dart/omemo_dart.dart' as omemo;
|
import 'package:omemo_dart/omemo_dart.dart' as omemo;
|
||||||
|
|
||||||
void main(List<String> args) async {
|
void main(List<String> args) async {
|
||||||
@ -17,43 +16,22 @@ void main(List<String> args) async {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
final parser = ArgParser()
|
final parser = ArgumentParser()
|
||||||
..addOption('jid')
|
..parser.addOption('to', help: 'The JID to send messages to');
|
||||||
..addOption('password')
|
final options = parser.handleArguments(args);
|
||||||
..addOption('host')
|
if (options == null) {
|
||||||
..addOption('port')
|
return;
|
||||||
..addOption('to')
|
|
||||||
..addOption('xmpps-srv');
|
|
||||||
final options = parser.parse(args);
|
|
||||||
|
|
||||||
// Parse a potential xmpps-client SRV record here.
|
|
||||||
// Format: --xmpps-srv <priority>,<weight>,<target>,<port>
|
|
||||||
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]),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect
|
// Connect
|
||||||
final jid = JID.fromString(options['jid']! as String);
|
final jid = parser.jid;
|
||||||
final to = JID.fromString(options['to']! as String).toBare();
|
final to = JID.fromString(options['to']! as String).toBare();
|
||||||
final portString = options['port'] as String?;
|
|
||||||
final connection = XmppConnection(
|
final connection = XmppConnection(
|
||||||
TestingReconnectionPolicy(),
|
TestingReconnectionPolicy(),
|
||||||
AlwaysConnectedConnectivityManager(),
|
AlwaysConnectedConnectivityManager(),
|
||||||
ClientToServerNegotiator(),
|
ClientToServerNegotiator(),
|
||||||
ExampleTCPSocketWrapper(srvRecord),
|
ExampleTCPSocketWrapper(parser.srvRecord),
|
||||||
)..connectionSettings = ConnectionSettings(
|
)..connectionSettings = parser.connectionSettings;
|
||||||
jid: jid,
|
|
||||||
password: options['password']! as String,
|
|
||||||
host: options['host'] as String?,
|
|
||||||
port: portString != null ? int.parse(portString) : null,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Generate OMEMO data
|
// Generate OMEMO data
|
||||||
omemo.OmemoManager? oom;
|
omemo.OmemoManager? oom;
|
||||||
|
84
examples_dart/lib/arguments.dart
Normal file
84
examples_dart/lib/arguments.dart
Normal file
@ -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: <priority>,<weight>,<target>,<port>')
|
||||||
|
..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<String> 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]),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user