|
| 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 | +'''; |
0 commit comments