Skip to content

Commit 1df6935

Browse files
feat!: Preserves UsageException instead of replacing it with ExitException (#23)
* feat!: No longer replaces UsageException with ExitException BREAKING CHANGES: Upon argument parse errors the UsageException was replaced by an ExitException. This behavior is now changed to rethrow the orginal UsageException instead. * test: Added specific exit code conds to existing select tests * chore: Reformatted source with Dart 3.6 * ci: Pinned CI Dart version to 3.6
1 parent 75a97a2 commit 1df6935

File tree

19 files changed

+201
-154
lines changed

19 files changed

+201
-154
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,28 @@ jobs:
1313
name: Dart format
1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: actions/checkout@v4
17-
- uses: dart-lang/setup-dart@v1.6.4
18-
- name: Verify formatting
19-
run: dart format --output=none --set-exit-if-changed .
16+
- uses: actions/checkout@v4
17+
- uses: dart-lang/setup-dart@v1.7.1
18+
with:
19+
sdk: 3.6
20+
- name: Verify formatting
21+
run: dart format --output=none --set-exit-if-changed .
2022
dart_analyze:
2123
name: Dart Analyze
2224
runs-on: ubuntu-latest
2325
steps:
2426
- uses: actions/checkout@v4
25-
- uses: dart-lang/setup-dart@v1.6.4
27+
- uses: dart-lang/setup-dart@v1.7.1
28+
with:
29+
sdk: 3.6
2630
- run: dart pub get
2731
- run: dart analyze --fatal-infos
2832
dart_test:
2933
name: Dart Test
3034
runs-on: ubuntu-latest
3135
steps:
3236
- uses: actions/checkout@v4
33-
- uses: dart-lang/setup-dart@v1.6.4
37+
- uses: dart-lang/setup-dart@v1.7.1
38+
with:
39+
sdk: 3.6
3440
- run: dart test

lib/src/analytics/analytics.dart

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class MixPanelAnalytics implements Analytics {
2424
required String uniqueUserId,
2525
required String projectToken,
2626
required String version,
27-
}) : _uniqueUserId = uniqueUserId,
28-
_projectToken = projectToken,
29-
_version = version;
27+
}) : _uniqueUserId = uniqueUserId,
28+
_projectToken = projectToken,
29+
_version = version;
3030

3131
@override
3232
void cleanUp() {}
@@ -62,16 +62,14 @@ class MixPanelAnalytics implements Analytics {
6262

6363
Future<void> _quietPost(String payload) async {
6464
try {
65-
await http
66-
.post(
67-
Uri.parse(_endpoint),
68-
body: 'data=$payload',
69-
headers: {
70-
'Accept': 'text/plain',
71-
'Content-Type': 'application/x-www-form-urlencoded',
72-
},
73-
)
74-
.timeout(const Duration(seconds: 2));
65+
await http.post(
66+
Uri.parse(_endpoint),
67+
body: 'data=$payload',
68+
headers: {
69+
'Accept': 'text/plain',
70+
'Content-Type': 'application/x-www-form-urlencoded',
71+
},
72+
).timeout(const Duration(seconds: 2));
7573
} catch (e) {
7674
return;
7775
}

lib/src/better_command_runner/better_command.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ abstract class BetterCommand extends Command {
77
final ArgParser _argParser;
88

99
BetterCommand({PassMessage? logInfo, int? wrapTextColumn})
10-
: _logInfo = logInfo,
11-
_argParser = ArgParser(usageLineLength: wrapTextColumn);
10+
: _logInfo = logInfo,
11+
_argParser = ArgParser(usageLineLength: wrapTextColumn);
1212

1313
@override
1414
ArgParser get argParser => _argParser;

lib/src/better_command_runner/better_command_runner.dart

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'dart:async';
22

33
import 'package:args/args.dart';
44
import 'package:args/command_runner.dart';
5-
import 'package:cli_tools/src/better_command_runner/exit_exception.dart';
65

76
/// A function type for executing code before running a command.
87
typedef OnBeforeRunCommand = Future<void> Function(BetterCommandRunner runner);
@@ -14,11 +13,10 @@ typedef PassMessage = void Function(String message);
1413
/// The [logLevel] is the log level to set.
1514
/// The [commandName] is the name of the command if custom rules for log
1615
/// levels are needed.
17-
typedef SetLogLevel =
18-
void Function({
19-
required CommandRunnerLogLevel parsedLogLevel,
20-
String? commandName,
21-
});
16+
typedef SetLogLevel = void Function({
17+
required CommandRunnerLogLevel parsedLogLevel,
18+
String? commandName,
19+
});
2220

2321
/// A function type for tracking events.
2422
typedef OnAnalyticsEvent = void Function(String event);
@@ -63,19 +61,18 @@ class BetterCommandRunner extends CommandRunner {
6361
OnBeforeRunCommand? onBeforeRunCommand,
6462
OnAnalyticsEvent? onAnalyticsEvent,
6563
int? wrapTextColumn,
66-
}) : _logError = logError,
67-
_logInfo = logInfo,
68-
_onBeforeRunCommand = onBeforeRunCommand,
69-
_setLogLevel = setLogLevel,
70-
_onAnalyticsEvent = onAnalyticsEvent,
71-
_argParser = ArgParser(usageLineLength: wrapTextColumn) {
64+
}) : _logError = logError,
65+
_logInfo = logInfo,
66+
_onBeforeRunCommand = onBeforeRunCommand,
67+
_setLogLevel = setLogLevel,
68+
_onAnalyticsEvent = onAnalyticsEvent,
69+
_argParser = ArgParser(usageLineLength: wrapTextColumn) {
7270
argParser.addFlag(
7371
BetterCommandRunnerFlags.quiet,
7472
abbr: BetterCommandRunnerFlags.quietAbbr,
7573
defaultsTo: false,
7674
negatable: false,
77-
help:
78-
'Suppress all cli output. Is overridden by '
75+
help: 'Suppress all cli output. Is overridden by '
7976
' -${BetterCommandRunnerFlags.verboseAbbr}, --${BetterCommandRunnerFlags.verbose}.',
8077
);
8178

@@ -84,8 +81,7 @@ class BetterCommandRunner extends CommandRunner {
8481
abbr: BetterCommandRunnerFlags.verboseAbbr,
8582
defaultsTo: false,
8683
negatable: false,
87-
help:
88-
'Prints additional information useful for development. '
84+
help: 'Prints additional information useful for development. '
8985
'Overrides --${BetterCommandRunnerFlags.quietAbbr}, --${BetterCommandRunnerFlags.quiet}.',
9086
);
9187

@@ -120,7 +116,7 @@ class BetterCommandRunner extends CommandRunner {
120116
} on UsageException catch (e) {
121117
_onAnalyticsEvent?.call(BetterCommandRunnerAnalyticsEvents.invalid);
122118
_logError?.call(e.toString());
123-
throw ExitException(exitCodeCommandNotFound);
119+
rethrow;
124120
}
125121
}
126122

@@ -176,7 +172,7 @@ class BetterCommandRunner extends CommandRunner {
176172
} on UsageException catch (e) {
177173
_logError?.call(e.toString());
178174
_onAnalyticsEvent?.call(BetterCommandRunnerAnalyticsEvents.invalid);
179-
throw ExitException(exitCodeCommandNotFound);
175+
rethrow;
180176
}
181177
}
182178

lib/src/logger/helpers/progress.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ class Progress {
6464
this._message,
6565
this._stdout, {
6666
ProgressOptions options = const ProgressOptions(),
67-
}) : _stopwatch = Stopwatch(),
68-
_options = options {
67+
}) : _stopwatch = Stopwatch(),
68+
_options = options {
6969
_stopwatch
7070
..reset()
7171
..start();

lib/src/logger/loggers/std_out_logger.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class StdOutLogger extends Logger {
1717
final Map<String, String>? _replacements;
1818

1919
StdOutLogger(super.logLevel, {Map<String, String>? replacements})
20-
: _replacements = replacements;
20+
: _replacements = replacements;
2121

2222
@override
2323
int? get wrapTextColumn => stdout.hasTerminal ? stdout.terminalColumns : null;
@@ -189,9 +189,9 @@ class StdOutLogger extends Logger {
189189
message = switch (_replacements) {
190190
null => message,
191191
Map<String, String> replacements => replacements.entries.fold(
192-
message,
193-
(String acc, entry) => acc.replaceAll(entry.key, entry.value),
194-
),
192+
message,
193+
(String acc, entry) => acc.replaceAll(entry.key, entry.value),
194+
),
195195
};
196196

197197
_stopAnimationInProgress();

lib/src/package_version/package_version.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ abstract class PackageVersion {
2020
/// attempting to fetch the latest version again.
2121
static Future<Version?> fetchLatestPackageVersion({
2222
required Future<void> Function(PackageVersionData versionArtefact)
23-
storePackageVersionData,
23+
storePackageVersionData,
2424
required Future<PackageVersionData?> Function() loadPackageVersionData,
2525
required Future<Version?> Function() fetchLatestPackageVersion,
2626
}) async {
@@ -47,7 +47,7 @@ abstract class PackageVersion {
4747
static Future<void> _storePubDevVersion(
4848
Version? version, {
4949
required Future<void> Function(PackageVersionData versionArtefact)
50-
storePackageVersionData,
50+
storePackageVersionData,
5151
}) async {
5252
PackageVersionData versionArtefact;
5353
if (version != null) {
@@ -81,7 +81,7 @@ class PackageVersionData {
8181
);
8282

8383
Map<String, dynamic> toJson() => {
84-
'version': version.toString(),
85-
'valid_until': validUntil.millisecondsSinceEpoch,
86-
};
84+
'version': version.toString(),
85+
'valid_until': validUntil.millisecondsSinceEpoch,
86+
};
8787
}

lib/src/package_version/pub_api_client.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class PubApiClient {
1313
PubApiClient({
1414
http.Client? httpClient,
1515
requestTimeout = const Duration(seconds: 2),
16-
}) : _pubClient = PubClient(client: httpClient),
17-
_requestTimeout = requestTimeout;
16+
}) : _pubClient = PubClient(client: httpClient),
17+
_requestTimeout = requestTimeout;
1818

1919
/// Tries to fetch the latest stable version, version does not include '-' or '+',
2020
/// for the package named [packageName].

lib/src/prompts/confirm.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ Future<bool> confirm(
1010
bool? defaultValue,
1111
required Logger logger,
1212
}) async {
13-
var prompt =
14-
defaultValue == null
15-
? '[y/n]'
16-
: defaultValue
13+
var prompt = defaultValue == null
14+
? '[y/n]'
15+
: defaultValue
1716
? '[Y/n]'
1817
: '[y/N]';
1918

lib/src/prompts/select.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Future<Option> select(
2222
prompt,
2323
options: options,
2424
logger: logger,
25-
)).first;
25+
))
26+
.first;
2627
}
2728

2829
/// Prompts the user to select multiple options from a list of [options].
@@ -166,10 +167,9 @@ class _SelectState {
166167
return _SelectState(
167168
options: options,
168169
selectedIndex: selectedIndex,
169-
selectedOptions:
170-
selectedOptions.contains(selectedIndex)
171-
? (selectedOptions..remove(selectedIndex))
172-
: (selectedOptions..add(selectedIndex)),
170+
selectedOptions: selectedOptions.contains(selectedIndex)
171+
? (selectedOptions..remove(selectedIndex))
172+
: (selectedOptions..add(selectedIndex)),
173173
multiple: multiple,
174174
);
175175
}
@@ -208,7 +208,7 @@ void _clearTerminal() {
208208
const _underlineSelectGraphicRenditionControlSequence = '\x1B[4m';
209209
const _resetSelectGraphicRenditionControlSequence = '\x1B[0m';
210210
String underline(String text) => [
211-
_underlineSelectGraphicRenditionControlSequence,
212-
text,
213-
_resetSelectGraphicRenditionControlSequence,
214-
].join('');
211+
_underlineSelectGraphicRenditionControlSequence,
212+
text,
213+
_resetSelectGraphicRenditionControlSequence,
214+
].join('');

0 commit comments

Comments
 (0)