#shell #cargo #command #toolchain #command-line #prompt #rustup

bin+lib cargo-shell

简单的 cargo 命令行外壳

2 个版本

使用旧的 Rust 2015

0.1.1 2016 年 12 月 2 日
0.1.0 2016 年 11 月 22 日

#501 in Cargo 插件

MIT/Apache

16KB
254

cargo-shell

:toc

介绍

cargo shell 是一个基于 sbt 启发的 cargo 命令行外壳。它集成了许多现有的工具,并添加了一些额外的功能,以使开发 Rust 应用程序更加方便。

安装

cargo-shell 充分利用了您可能已经安装的工具,因此它假定您已经安装了可工作的 rust 安装(显然)以及 rustup.rs

$ cargo install cargo-shell

用法

安装后,可以在命令行中运行 cargo shell 来启动外壳

$ cargo shell
Welcome to cargo-shell v0.1.0
>> build
>> test
>> bench

...等等

就这样了吗?

到目前为止,我们并没有比仅仅将 cargo 别名为 c 并运行 c build 等等具有更多优势。然而,cargo-shell 提供了一些内置功能,使其更加有用。

cargo-watch 集成

如果您已在系统中安装了 cargo-watch,则可以在源文件更改时重新运行任何命令。这可以通过在命令前加上一个 ~ 来实现。

$ cargo shell
Welcome to cargo-shell v0.1.0
>> ~run
Hello, World!
Waiting for changes... Hit Ctrl-C to stop.

使用不同的工具链运行命令

rustup 允许我们使用不同的 Rust 版本运行命令。然而,执行此操作的命令可能有点冗长。当然,别名可以有所帮助。但是,使用命令行外壳,运行使用不同工具链的命令非常简单。

假设您正在使用 stable 工具链,但想使用 nightly 进行测试

$ cargo shell
Welcome to cargo-shell v0.1.0
>> build
   # builds using `stable`
>> ++nightly test
   # tests using `nightly`

cargo test 命令运行完成后,外壳将再次使用 stable 工具链。要在外壳中永久更改它,只需在命令末尾留下空格即可。

$ cargo shell
Welcome to cargo-shell v0.1.0
>> ++nightly
>> do build, test
   # shell remains in nightly until you change it back manually

在多个工具链下运行命令

假设我们想在 stablebetanightly(虽然默认如此,但这是可配置的)下运行项目的测试。

$ cargo shell
Welcome to cargo-shell v0.1.0
>> +test

...就这么简单!有一个配置选项,cargo-shell.toolchains,可以让你自定义运行时使用的工具链列表。

从文件中运行一系列命令

使用 < 操作符可以从文件中运行一系列命令。例如,这个文件:

.test-file

build
test
bench

可以像这样运行:

$ cargo shell
Welcome to cargo-shell v0.1.0
>> < test-file

然后,buildtestbench 命令将依次运行。

在会话中途更改提示符

可以在配置中永久更改提示符,但如果你想在会话中途更改它,可以使用 p 命令。

$ cargo shell
Welcome to cargo-shell v0.1.0
>> p $
$

一个限制是会自动去除首尾空白字符,所以如果你想在提示符末尾加一个空格,请使用引号。

$ cargo shell
Welcome to cargo-shell v0.1.0
>> p "$ "
$ # now there is a space here

配置

有几个配置选项可以用于自定义 cargo-shell。你可以在 .cargo/config 文件中,在 [cargo-shell] 节标题下添加它们。

提示符

这将自定义外壳提示符的外观。你可以使用一些占位符:{project}{version}{toolchain}。在每条命令之后,cargo-shell 将分别用项目名称、项目版本和当前工具链替换它们。

例如,要得到一个如 "my-project stable>> " 的提示符,你应该将提示符设置为以下内容:

[cargo-shell]
prompt = "{project} {toolchain}>> "

默认情况下,它只是:

[cargo-shell]
prompt = ">> "

默认工具链

这是外壳启动时默认使用的工具链。

注意:一旦我更好地集成了 rustup 的覆盖项,这将不再使用。到那时,cargo-shell 将使用与 rustup 相同的默认工具链,并尊重你使用 rustup 设置的任何覆盖项。

[cargo-shell]
default_toolchain = "stable"

工具链列表

这将自定义在运行 + 外壳命令时,命令在哪个工具链下运行。

[cargo-shell]
toolchains = ["stable", "beta", "nightly"]

待办事项

  • 文档
  • 从 rustup 检测工具链默认值和覆盖项
  • 外壳历史记录
  • 提示符的 git 集成
  • 自动完成

依赖项

~48MB
~895K SLoC