Skip to content

Commit 452d1dd

Browse files
authored
feat: add example app (#9)
1 parent bde9d17 commit 452d1dd

16 files changed

+467
-2
lines changed

.github/dependabot.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
version: 2
2-
enable-beta-ecosystems: true
32
updates:
43
- package-ecosystem: "github-actions"
54
directory: "/"
65
schedule:
76
interval: "daily"
87
- package-ecosystem: "pub"
9-
directory: "/"
8+
directory: "/example"
109
schedule:
1110
interval: "daily"
11+
- package-ecosystem: "pub"
12+
directory: "/"
13+
schedule:
14+
interval: "daily"

.github/workflows/example.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: example
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- ".github/workflows/example.yaml"
7+
- "example/lib/**"
8+
- "example/test/**"
9+
- "example/pubspec.yaml"
10+
push:
11+
branches:
12+
- main
13+
paths:
14+
- ".github/workflows/example.yaml"
15+
- "example/lib/**"
16+
- "example/test/**"
17+
- "example/pubspec.yaml"
18+
19+
jobs:
20+
semantic-pull-request:
21+
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/semantic_pull_request.yml@v1
22+
23+
build:
24+
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/dart_package.yml@v1

.idea/cli_completion.iml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# See https://www.dartlang.org/guides/libraries/private-files
2+
3+
# Files and directories created by pub
4+
.dart_tool/
5+
.packages
6+
build/
7+
pubspec.lock
8+
9+
# Files generated during tests
10+
.test_coverage.dart
11+
coverage/
12+
.test_runner.dart
13+
14+
# Android studio and IntelliJ
15+
.idea
16+
17+
/coverage

example/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## example
2+
3+
Generated by the [Very Good CLI][very_good_cli_link] 🤖
4+
5+
Example for cli_completion with the most common use cases for command completion.
6+
7+
[very_good_cli_link]: https://github.com/VeryGoodOpenSource/very_good_cli

example/analysis_options.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include: package:very_good_analysis/analysis_options.3.1.0.yaml
2+
linter:
3+
rules:
4+
public_member_api_docs: false

example/bin/example_cli.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'dart:io';
2+
3+
import 'package:example/src/command_runner.dart';
4+
5+
Future<void> main(List<String> args) async {
6+
await _flushThenExit(await ExampleCommandRunner().run(args));
7+
}
8+
9+
/// Flushes the stdout and stderr streams, then exits the program with the given
10+
/// status code.
11+
///
12+
/// This returns a Future that will never complete, since the program will have
13+
/// exited already. This is useful to prevent Future chains from proceeding
14+
/// after you've decided to exit.
15+
Future<void> _flushThenExit(int status) {
16+
return Future.wait<void>([stdout.close(), stderr.close()])
17+
.then<void>((_) => exit(status));
18+
}

example/lib/example.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// Example for cli_completion
2+
///
3+
/// ```sh
4+
/// # activate example
5+
/// dart pub global activate example
6+
///
7+
/// # see usage
8+
/// example_cli --help
9+
/// ```
10+
library example;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import 'package:args/command_runner.dart';
2+
import 'package:example/src/commands/commands.dart';
3+
import 'package:mason_logger/mason_logger.dart';
4+
5+
const executableName = 'example_cli';
6+
const packageName = 'example';
7+
const description = 'Example for cli_completion';
8+
9+
/// {@template example_command_runner}
10+
/// A [CommandRunner] for the CLI.
11+
///
12+
/// ```
13+
/// $ example_cli --version
14+
/// ```
15+
/// {@endtemplate}
16+
class ExampleCommandRunner extends CommandRunner<int> {
17+
/// {@macro example_command_runner}
18+
ExampleCommandRunner({
19+
Logger? logger,
20+
}) : _logger = logger ?? Logger(),
21+
super(executableName, description) {
22+
// Add root options and flags
23+
argParser.addFlag(
24+
'rootFlag',
25+
help: 'A flag in the root command',
26+
);
27+
28+
// Add sub commands
29+
addCommand(SomeCommand(_logger));
30+
addCommand(SomeOtherCommand(_logger));
31+
}
32+
33+
@override
34+
void printUsage() => _logger.info(usage);
35+
36+
final Logger _logger;
37+
38+
@override
39+
Future<int> run(Iterable<String> args) async {
40+
try {
41+
final topLevelResults = parse(args);
42+
43+
if (topLevelResults['rootFlag'] == true) {
44+
_logger.info('You used the root flag, it does nothing :)');
45+
return ExitCode.success.code;
46+
}
47+
48+
return await runCommand(topLevelResults) ?? ExitCode.success.code;
49+
} on FormatException catch (e, stackTrace) {
50+
// On format errors, show the commands error message, root usage and
51+
// exit with an error code
52+
_logger
53+
..err(e.message)
54+
..err('$stackTrace')
55+
..info('')
56+
..info(usage);
57+
return ExitCode.usage.code;
58+
} on UsageException catch (e) {
59+
// On usage errors, show the commands usage message and
60+
// exit with an error code
61+
_logger
62+
..err(e.message)
63+
..info('')
64+
..info(e.usage);
65+
return ExitCode.usage.code;
66+
}
67+
}
68+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export 'some_commmand.dart';
2+
export 'some_other_commmand.dart';

0 commit comments

Comments
 (0)