Skip to content

Commit dc6731b

Browse files
committed
[ CLI ] Add new cli template to dart create
This new template will create a simple project with basic command line argument processing using `package:args`. Fixes #53126 Change-Id: I70f370bc348bb85547e4fcf2131c007416a48262 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/321800 Reviewed-by: Michael Thomsen <[email protected]> Commit-Queue: Ben Konyi <[email protected]>
1 parent 4d4a922 commit dc6731b

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

pkg/dartdev/lib/src/templates.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:collection/collection.dart' show IterableExtension;
88
import 'package:meta/meta.dart';
99
import 'package:path/path.dart' as p;
1010

11+
import 'templates/cli.dart';
1112
import 'templates/console.dart';
1213
import 'templates/console_simple.dart';
1314
import 'templates/package.dart';
@@ -18,6 +19,7 @@ final _substituteRegExp = RegExp(r'__([a-zA-Z]+)__');
1819
final _nonValidSubstituteRegExp = RegExp(r'[^a-zA-Z]');
1920

2021
final List<Generator> generators = [
22+
CliGenerator(),
2123
ConsoleGenerator(),
2224
PackageGenerator(),
2325
ServerShelfGenerator(),
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import '../templates.dart';
6+
import 'common.dart' as common;
7+
8+
/// A generator for a simple command-line application with argument parsing.
9+
class CliGenerator extends DefaultGenerator {
10+
CliGenerator()
11+
: super(
12+
'cli',
13+
'CLI Application',
14+
'A command-line application with basic argument parsing.',
15+
alternateId: 'console-cli',
16+
categories: const ['dart', 'cli'],
17+
) {
18+
addFile('.gitignore', common.gitignore);
19+
addFile('analysis_options.yaml', common.analysisOptions);
20+
addFile('CHANGELOG.md', common.changelog);
21+
addFile('pubspec.yaml', _pubspec);
22+
addFile('README.md', _readme);
23+
setEntrypoint(
24+
addFile('bin/__projectName__.dart', _mainDart),
25+
);
26+
}
27+
28+
@override
29+
String getInstallInstructions(
30+
String directory, {
31+
String? scriptPath,
32+
}) =>
33+
super.getInstallInstructions(directory, scriptPath: 'bin/$scriptPath');
34+
}
35+
36+
final String _pubspec = '''
37+
name: __projectName__
38+
description: A sample command-line application with basic argument parsing.
39+
version: 0.0.1
40+
# repository: https://github.com/my_org/my_repo
41+
42+
environment:
43+
${common.sdkConstraint}
44+
45+
# Add regular dependencies here.
46+
dependencies:
47+
args: ^2.4.2
48+
49+
dev_dependencies:
50+
lints: ^2.1.0
51+
test: ^1.24.0
52+
''';
53+
54+
final String _readme = '''
55+
A sample command-line application providing basic argument parsing with an entrypoint in `bin/`.
56+
''';
57+
58+
final String _mainDart = r'''
59+
import 'package:args/args.dart';
60+
61+
const String version = '0.0.1';
62+
63+
ArgParser buildParser() {
64+
return ArgParser()
65+
..addFlag(
66+
'help',
67+
abbr: 'h',
68+
negatable: false,
69+
help: 'Print this usage information.',
70+
)
71+
..addFlag(
72+
'verbose',
73+
abbr: 'v',
74+
negatable: false,
75+
help: 'Show additional command output.',
76+
)
77+
..addFlag(
78+
'version',
79+
negatable: false,
80+
help: 'Print the tool version.',
81+
);
82+
}
83+
84+
void printUsage(ArgParser argParser) {
85+
print('Usage: dart __projectName__.dart <flags> [arguments]');
86+
print(argParser.usage);
87+
}
88+
89+
void main(List<String> arguments) {
90+
final ArgParser argParser = buildParser();
91+
try {
92+
final ArgResults results = argParser.parse(arguments);
93+
bool verbose = false;
94+
95+
// Process the parsed arguments.
96+
if (results.wasParsed('help')) {
97+
printUsage(argParser);
98+
return;
99+
}
100+
if (results.wasParsed('version')) {
101+
print('__projectName__ version: $version');
102+
return;
103+
}
104+
if (results.wasParsed('verbose')) {
105+
verbose = true;
106+
}
107+
108+
// Act on the arguments provided.
109+
print('Positional arguments: ${results.rest}');
110+
if (verbose) {
111+
print('[VERBOSE] All arguments: ${results.arguments}');
112+
}
113+
} on FormatException catch (e) {
114+
// Print usage information if an invalid argument was provided.
115+
print(e.message);
116+
print('');
117+
printUsage(argParser);
118+
}
119+
}
120+
''';

pkg/dartdev/test/commands/create_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ void defineCreateTests() {
193193
expect(lines.length, 2);
194194
expect(lines[0], 'cd $dir');
195195
expect(lines[1], 'dart run bin/server.dart');
196+
} else if (generator.categories.contains('cli')) {
197+
expect(lines.length, 2);
198+
expect(lines[0], 'cd $dir');
199+
expect(lines[1], 'dart run bin/$projectName.dart');
196200
} else {
197201
expect(lines.length, 2);
198202
expect(lines[0], 'cd $dir');

0 commit comments

Comments
 (0)