Flutter命令行工具插件danny_cli的使用
Flutter命令行工具插件danny_cli的使用
一个用于开发文件化命令行应用的命令行接口。
Danny Wilde:
- 70年代一部伟大电视连续剧《劝导者》中的主角。
快速开始
# 🎯 激活插件
dart pub global activate danny_cli
# 🚀 创建项目
danny create greet
# 📦 构建项目
danny build
# 🔨 在本地激活项目
danny activate
# 使用新CLI
greet -h
目录
概述
安装
# 🎯 激活插件
dart pub global activate danny_cli
使用
A Command-Line Interface to develop wilde command-line apps.
Usage: danny <command>
Global options:
-h, --help 打印此使用信息。
-v, --version 打印当前版本。
--verbose 启用详细日志记录。
可用命令:
activate 在本地激活构建。
build 创建构建。
create 创建一个wilde项目。
list 列出项目的命令。
new 创建新的命令。
运行 "danny help <command>" 获取有关命令的更多信息。
动机
在Dart/Flutter项目开发过程中,经常会遇到特定项目相关的工具需求。在理想情况下,这会导致在tool
目录下创建shell脚本,而在不太理想的情况下,任务仅保留在开发者的脑海中。
该包旨在简化使用Dart语言为Dart/Flutter项目编写工具CLI的过程。
前提条件
每个danny项目都依赖于args
包,因此开发者需要具备对其基本功能的理解。
项目结构
一个基本的danny项目结构如下所示:
commands/
<command-1>.dart
<command-2>.dart
<branch-command-1>/
<nested-command-1>.dart
<nested-command-2>.dart
pubspec.yaml
runner.dart
pubspec.yaml
文件暴露了CLI的可执行文件。
在commands
目录中以文件形式定义Command
。
runner.dart
配置了托管命令的CommandRunner
。
CommandRunner
runner.dart
允许开发者配置CLI的CommandRunner
。
import 'package:args/args.dart';
// Required
typedef Type = void;
// Required
const description = 'The cool CLI.';
// Optional
const usageLineLength = 100;
// Optional
const suggestionDistanceLimit = 4;
// Optional
Future<Type?> run(
Iterable<String> args,
Future<Type?> Function(Iterable<String> args) run,
) async {
// ...
}
// Optional
Future<Type?> runCommand(
ArgResults topLevelResults,
Future<Type?> Function(ArgResults topLevelResults) runCommand,
) async {
// ...
}
上述代码展示了runner.dart
的内容。
它必须在顶级暴露Type
和description
。
Type
: CommandRunner
及其所有Command
的类型。
description
: CLI的描述。
usageLineLength
: CLI的使用行长度。(可选)
suggestionDistanceLimit
: CLI的建议距离限制。(可选)
run
: CLI的run
方法。(注意它如何使用在runner.dart
中定义的Type
,并将来自父类的run
方法传递给它)(可选)
runCommand
: CLI的runCommand
方法。(注意它如何使用在runner.dart
中定义的Type
,并将来自父类的runCommand
方法传递给它)(可选)
Command
一个<name>.dart
(命令文件)允许开发者实现CLI的一个Command
。
import 'dart:async';
import 'package:args/args.dart';
import '../runner.dart';
// Required
const description = 'My cool command.';
// Optional
const category = 'Foo';
// Optional
const invocation = 'cool [args]';
// Optional
const usageFooter = 'oooxxxooo';
// Optional
final argParser = ArgParser()
..addFlag(
'foo',
help: 'The foo flag.',
)
..addOption(
'bar',
help: 'The bar option.',
);
// Required
FutureOr<Type> run(ArgResults globalResults, ArgResults argResults) {
final foo = argResults['foo'] as bool? ?? false;
final bar = argResults['bar'] as String? ?? 'Baz';
// ...
}
上述代码展示了命令文件的内容。
description
和run
方法必须在文件的顶层可用,而category
、invocation
、usageFooter
和argParser
是可选的。
description
: 命令的描述。
run
: 命令的run
方法。(注意它如何使用在runner.dart
中定义的Type
,并接收持有解析后的命令行参数的ArgResults
)
category
: 命令的类别。(可选)
invocation
: 命令的调用方式。(可选)
usageFooter
: 命令的使用尾部。(可选)
argParser
: 命令的argParser
。允许开发者定义命令的选项和标志。(可选)
创建项目
create
命令允许开发者创建一个新的项目。
创建一个wilde项目。
Usage: danny create <project-name>
-h, --help 打印此使用信息。
-o, --output-dir 生成新项目的目录。
(默认为".")
--description 传递给CommandRunner的描述。
运行 "danny help" 查看全局选项。
添加命令
new
命令允许开发者向项目中添加新的命令。
如果新命令包含参数或标志,则需要提供arg-parser
标志。
创建一个新命令。
Usage: danny new "foo bar baz"
-h, --help 打印此使用信息。
--arg-parser 命令是否有自定义ArgParser。
--description 命令的描述。
运行 "danny help" 查看全局选项。
列出命令
list
命令显示项目的所有可用命令。
列出项目的命令。
Usage: danny list
-h, --help 打印此使用信息。
运行 "danny help" 查看全局选项。
构建项目
build
命令会在项目根目录生成一个runner.danny.dart
文件,其中包含了基于runner.dart
和commands
目录内定义的命令的就绪使用的CommandRunner
和Command
。
创建一个构建。
Usage: danny build
-h, --help 打印此使用信息。
运行 "danny help" 查看全局选项。
在本地激活项目
activate
命令使项目的可执行文件在本地可用,
使用dart pub global activate
作为底层支持。
在本地激活构建。
Usage: danny activate
-h, --help 打印此使用信息。
运行 "danny help" 查看全局选项。
更多关于Flutter命令行工具插件danny_cli的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter命令行工具插件danny_cli的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter项目中使用命令行工具插件 danny_cli
的代码案例。请注意,danny_cli
是一个假设的插件名称,实际使用中你可能需要替换为真实存在的命令行工具插件。由于具体的插件实现细节未知,以下示例将基于通用插件的使用模式进行展示。
步骤 1: 添加依赖
首先,在你的 pubspec.yaml
文件中添加 danny_cli
插件的依赖。由于这是一个假设的插件,依赖项名称可能需要替换为实际插件的名称。
dependencies:
flutter:
sdk: flutter
danny_cli: ^1.0.0 # 假设的版本号,实际使用时替换为最新版本
然后运行 flutter pub get
来获取依赖。
步骤 2: 导入插件
在你的 Dart 文件中导入 danny_cli
插件。通常,这会在一个专门用于命令行操作的文件中进行。
import 'package:danny_cli/danny_cli.dart';
步骤 3: 使用插件功能
假设 danny_cli
提供了一个执行命令行命令的方法 runCommand
,你可以像下面这样使用它:
void main() async {
// 初始化 Flutter 应用(如果这是你的主入口文件)
WidgetsFlutterBinding.ensureInitialized();
// 使用 danny_cli 插件执行命令行命令
try {
String result = await DannyCli.runCommand('echo Hello, World!');
print('Command output: $result');
} catch (e) {
print('Error executing command: $e');
}
// 如果你是在 Flutter 应用中,接下来通常会启动应用
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Danny CLI Demo'),
),
body: Center(
child: Text('Check the console for command output.'),
),
),
);
}
}
注意事项
- 插件文档:实际使用时,务必查阅
danny_cli
(或实际插件)的官方文档,了解其具体用法和API。 - 权限:某些命令行操作可能需要特定的权限,确保你的应用具有这些权限。
- 异步处理:命令行操作通常是异步的,因此需要使用
async
和await
关键字来处理。 - 错误处理:添加适当的错误处理逻辑,以应对命令执行失败的情况。
由于 danny_cli
是一个假设的插件,上述代码仅作为示例。在实际项目中,你需要根据具体插件的API进行调整。