百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rust-cli-boilerplate

> 开发工具
开源

用于CLI应用的Rust项目锅炉板

184 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于CLI应用的Rust项目锅炉板

Rust CLI Project Template

A base project template for comfortably building small but reliable utilities in the Rust programming language.

Rationale

Historically, I have used Python for quick little scripts (from which my big, long-lived projects tend to unexpectedly develop), but maintaining large things written in Python risks turning into a gumption trap due to the need to either manually test every code path I affect when making changes or pour a lot of effort into writing an automated test suite to do it for me.

Rust's stronger compile-time guarantees remove most of that motivation-sapping busywork, but, since I've spent so many years getting comfortable with Python, working in a new language risks being a chicken-and-egg problem which kills off the will to change tasks. (Once I start something, it's easy to keep going.)

The purpose of this repository is to lower the friction for Rust-based development of such "scripts of unknown potential" as much as possible. (Hence the inclusion of aliases like just run which serve only to remove the need to think about whether to type just or cargo, and the wrappers for cargo-edit commands which regenerate the API documentation I'll have open in a browser tab after adding/removing/updating dependencies.)

In short, this is a "do it properly" answer to the workflow that evolved around the boiler snippet for Python that I keep in my UltiSnips.

License

This repository is licensed under your choice of the MIT or Apache 2.0 licenses with the exception of the license texts themselves. Please replace the LICENSE file in the template folder with your preferred license if you do not habitually start your new projects under the GNU GPLv3.

Features

  • Uses StructOpt (with colourized --help output and "Did you mean...?" suggestions enabled) for argument parsing because gumdrop doesn't support OsString-based parsing for correct handling of non-UTF8 paths.
  • Uses anyhow for unified error handling.
  • Presents an app::main(opts: CliOpts) -> Result function to keep your application logic cleanly separated from argument parsing and handling of fatal errors.
  • Exposes clap's support for generating shell completions by providing a --dump-completions option.
  • Enables almost all rustc and clippy lints without making clippy mandatory.
  • A comprehensive set of just commands, easily customized via variables (eg. for cross-compilation), including install and uninstall, which also take care of shell completions and a manpage.
  • just build-dist for a 100% static x86_64 binary that starts at roughly 272KiB (248KiB with panic="abort") in new projects.
  • just install-deps to install all but two optional dependencies on Debian-family distros.
  • just install-cargo-deps to install all distro-agnostic dependencies.
  • A basic .travis.yml for use with Travis-CI and Nightli.es.
  • The just fmt command always calls the nightly version of rustfmt to ensure access to the excessive number of customization options which are gated away as unstable.

Usage

  1. Clone a copy of this repository (.git must exist, so no archive downloads)
  2. Run apply.py path/to/new/project
  3. Edit src/app.rs to implement your application logic

NOTE: As a safety measure, apply.py generates new projects from the git HEAD of the template repository (not the working tree), so any local changes you make will not be picked up until you commit them.

Supplementary Files

Metadata

LICENSE A copy of the GNU GPLv3 as my "until I've had time to think about it" license of choice. Make a local commit which replaces this with your preferred default for new projects.

CONTRIBUTING A copy of the Developer Certificate of Origin, which is the Linux kernel developers' more ideologically appropriate alternative to CLAs as a means of legally armouring themselves against bad-faith contributions

Configuration

.gitignore Ignore /target and other generated files

clippy.toml Whitelist for CamelCase names which trigger Clippy's "identifier needs backticks" lint

rustfmt.toml A custom rustfmt configuration which shows TODO/FIXME comments and attempts to make it conform to the style I'm willing to enforce at the expense of not using rustfmt if necessary.

Development Automation

apply.py Run this to generate new projects as a workaround for cargo-generate's incompatibility with justfile syntax

justfile Build/development-automation commands via just (a pure-Rust make-alike)

Support Code You May Borrow

gen_justfile_reference.py Code which is used to regenerate the reference charts for justfile variables and commands in this README so it's easy to keep them up to date.

test_justfile.py A test suite for my justfile which you may want to adapt for your own projects.

Justfile Reference

Variables (just --evaluate)

VariableDefault ValueDescription

CARGO_BUILD_TARGET x86_64-unknown-linux-musl The target for cargo commands to use and install-rustup-deps to install

build_flags

An easy place to modify the build flags used

channel stable An easy way to override the cargo channel for just this project

features

Extra cargo features to enable

build-dist

sstrip_bin sstrip Set this if you need to override it for a cross-compiling sstrip

strip_bin strip Set this to the cross-compiler's strip when cross-compiling

strip_flags --strip-unneeded Flags passed to strip_bin

upx_flags --ultra-brute Flags passed to UPX

kcachegrind

callgrind_args

Extra arguments to pass to callgrind.

callgrind_out_file callgrind.out.justfile Temporary file used by just kcachegrind

kcachegrind kcachegrind Set this to override how kcachegrind is called

install and uninstall

bash_completion_dir ~/.bash_completion.d Where to install bash completions. You'll need to manually add some lines to source these files in .bashrc

fish_completion_dir ~/.config/fish/completions Where to install fish completions. You'll probably never need to change this.

manpage_dir ~/.cargo/share/man/man1 Where to install manpages. As long as ~/.cargo/bin is in your PATH, man should automatically pick up this location.

zsh_completion_dir ~/.zsh/functions Where to install zsh completions. You'll need to add this to your fpath manually

Commands (just --list)

CommandArgumentsDescription

DEFAULT

Shorthand for just fulltest

Development

add args (optional) Alias for cargo-edit's cargo add which regenerates local API docs afterwards

bloat args (optional) Alias for cargo bloat

check args (optional) Alias for cargo check

clean args (optional) Superset of cargo clean -v which deletes other stuff this justfile builds

clippy args (optional) Alias for cargo clippy which touches src/* to work around clippy bug

doc args (optional) Run rustdoc with --document-private-items and then run cargo-deadlinks

fmt args (optional) Alias for cargo +nightly fmt -- {{args}}

fmt-check args (optional) Alias for cargo +nightly fmt -- --check {{args}} which un-bloats TODO/FIXME warnings

fulltest

Run all installed static analysis, plus cargo test

geiger args (optional) Alias for cargo geiger

kcachegrind args (optional) Run a debug build under callgrind, then open the profile in KCachegrind

kcov

Generate a statement coverage report in target/cov/

rm args (optional) Alias for cargo-edit's cargo rm which regenerates local API docs afterwards

search args (optional) Convenience alias for opening a crate search on lib.rs in the browser

test args (optional) Alias for cargo test

update args (optional) Alias for cargo-edit's cargo update which regenerates local API docs afterwards

Local Builds

build

Alias for cargo build

install

Install the un-packed binary, shell completions, and a manpage

run args (optional) Alias for cargo run -- {{args}}

uninstall

Remove any files installed by the install task (but leave any parent directories created)

Release Builds

build-dist

Make a release build and then strip and compress the resulting binary

dist

Call dist-supplemental and build-dist and copy the packed binary to dist/

dist-supplemental

Build the shell completions and a manpage, and put them in dist/

Dependencies

install-apt-deps

Use apt-get to install dependencies cargo can't (except kcov and sstrip)

install-cargo-deps

install-rustup-deps and then cargo install tools

install-deps

Run install-apt-deps and install-cargo-deps. List what remains.

install-rustup-deps

Install (don't update) nightly and channel toolchains, plus CARGO_BUILD_TARGET, clippy, and rustfmt

Tips

  • Edit the DEFAULT command. That's what it's there for.

  • You can use just from any subdirectory in your project. It's like git that way.

  • just path/to/project/ (note the trailing slash) is equivalent to (cd path/to/project; just)

  • just path/to/project/command is equivalent to (cd path/to/project; just command)

  • The simplest way to activate the bash completion installed by just install is to add this to your .bashrc:

    bash
    for script in ~/.bash_completion.d/*; do
      if [ -e "$script" ]; then
        . "$script"
      fi
    done
    foo
    
  • The simplest way to activate the zsh completion installed by just install is to add this to your .zshrc:

    bash
    fpath=(~/.zsh/functions(:A) $fpath)
  • When using clap/StructOpt validators for inputs such as filesystem paths, only use them to bail out early on bad input, not as your only check. They're conceptually similar to raw pointers and can be invalidated between when you check them and when you try to use them because Rust can't control what the OS and other programs do in the interim. See this blog post for more on this idea of references versus values in command-line arguments.

Build Behaviour

In order to be as suitable as possible for building compact, easy-to-distribute, high-reliability replacements for shell scripts, the following build options are defined:

If built via just build:

  1. The default CARGO_BUILD_TARGET defined in the justfile will specify a 32-bit x86 build, statically linked against musl-libc for portability comparable to a Go binary. (Unless musl-gcc is installed, this will cause build failures if you depend on any crates which link to C or C++ code.)

If built via cargo build --release:

  1. Full LTO (Link-Time Optimization) will be enabled. (lto = true)
  2. The binary will be built with opt-level = "z" to further reduce file size.
  3. If panic="abort" is uncommented in Cargo.toml, LTO will prune away the unwinding machinery to save even more space, but panics will not cause Drop implementations to be run and will be uncatchable.

If built via just build-dist:

  1. Unless otherwise noted, all optimizations listed above. [[1]](https://lifthrasiir.github.io/rustlog/why-is-a-rust-exec

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rustboilerplateboilerplate-applicationboilerplate-templatecli

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类开发工具
定价开源

> 相关工具

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具