用rust写一个命令行工具

用rust写一个命令行工具

本项目会使用 Rust 和 clap 4.4.0 创建一个命令行工具 my_dev_tool,先实现 urlencode、urldecode 和时间戳转换为本地时间三个功能。如果你也想实现一个自己的命令行工具,可以按照以下步骤进行:

第 1 步:创建项目并添加依赖

  1. 创建新的 Rust 项目:在终端中运行以下命令:

    bash 复制代码
    cargo new my_dev_tool
    cd my_dev_tool
  2. 添加依赖 :在 Cargo.toml 中添加 clapserdeserde_jsonchrono 作为依赖:

    toml 复制代码
    [dependencies]
    clap = "4.4.0"
    chrono = "0.4"
    urlencoding = "2.1"

第 2 步:编写代码

src/main.rs 中,使用 clap 定义命令行参数并实现功能。

rust 复制代码
use clap::{Arg, ArgMatches, Command};
use chrono::{Local, TimeZone};
use urlencoding::{decode, encode};

fn main() {
    let matches = Command::new("my_dev_tool")
        .version("1.0")
        .author("Your Name <your.email@example.com>")
        .about("Developer's tool for urlencode and time format!")
        .subcommand_required(true)
        .arg_required_else_help(true)
        .subcommand(
            Command::new("urlencode")
                .about("URL-encode a string")
                .arg(Arg::new("input").help("String to encode").required(true)),
        )
        .subcommand(
            Command::new("urldecode")
                .about("URL-decode a string")
                .arg(Arg::new("input").help("String to decode").required(true)),
        )
        .subcommand(
            Command::new("timestamp")
                .about("Convert a UNIX timestamp to local datetime")
                .arg(Arg::new("timestamp").help("UNIX timestamp").required(true)),
        )
        .get_matches();

    match matches.subcommand() {
        Some(("urlencode", sub_matches)) => url_encode(sub_matches),
        Some(("urldecode", sub_matches)) => url_decode(sub_matches),
        Some(("timestamp", sub_matches)) => convert_timestamp(sub_matches),
        _ => unreachable!(),
    }
}

fn url_encode(matches: &ArgMatches) {
    if let Some(input) = matches.get_one::<String>("input") {
        println!("{}", encode(input));
    }
}

fn url_decode(matches: &ArgMatches) {
    if let Some(input) = matches.get_one::<String>("input") {
        println!("{}", decode(input).unwrap());
    }
}

fn convert_timestamp(matches: &ArgMatches) {
    if let Some(timestamp_str) = matches.get_one::<String>("timestamp") {
        let timestamp = timestamp_str.parse::<i64>().unwrap();
        let datetime = Local.timestamp_opt(timestamp, 0).unwrap();
        println!("{}", datetime.to_rfc3339());
    }
}

第 3 步:编译和安装

  1. 编译项目

    bash 复制代码
    cargo build --release
  2. 安装到系统

    • 在 Linux 或 macOS 上,您可以将编译后的可执行文件复制到 /usr/local/bin 或其他 PATH 包含的目录:

      bash 复制代码
      sudo cp target/release/my_dev_tool /usr/local/bin/
    • 在 Windows 上,您可以将可执行文件复制到任何 PATH 包含的目录,或者手动添加其所在目录到系统 PATH。

第 4 步:使用工具

一旦安装,您就可以直接在命令行中使用 my_dev_tool,例如:

bash 复制代码
my_dev_tool urlencode "https://example.com"
my_dev_tool urldecode "https%3A%2F%2Fexample.com"
my_dev_tool timestamp 1609459200

正常教程到这里就结束了,但是通过复制的方法安装命令行,实在是low,必须要使用一种装逼的方式来安装。因此,下面的步骤才是命令行装逼的关键,支持cargo安装。

第5步,支持cargo安装

要使您的 my_dev_tool 命令行工具能够通过 cargo install 安装,您需要将其发布到 crates.io,这是 Rust 的包管理仓库。在发布之前,您需要创建一个帐户并获取一个 API 令牌用于身份验证。以下是将您的工具准备并发布到 crates.io 的步骤:

第(1)步:注册 crates.io 帐户

  1. 访问 crates.io 并注册一个帐户。
  2. 登录后,在 "Account Settings" 中获取您的 API 令牌。
  3. 验证自己的邮箱,邮箱只有验证成功才可以publish包。

第(2)步:登录 Cargo

在您的终端中,使用以下命令登录 Cargo:

bash 复制代码
cargo login [your_api_token]

[your_api_token] 替换为您在 crates.io 上的 API 令牌。

第(3)步:准备发布

确保您的 Cargo.toml 文件包含所有必要的信息,这对于发布至 crates.io 是必要的。下面是一个示例:

toml 复制代码
[package]
name = "my_dev_tool"
version = "0.1.0"
authors = ["Your Name <youremail@example.com>"]
edition = "2018"

# 以下是描述和文档链接等可选字段
description = "A useful development tool for various tasks"
documentation = "https://example.com/my_dev_tool/docs"
license = "MIT OR Apache-2.0"

[dependencies]
clap = "3.0"
chrono = "0.4"
urlencoding = "2.1"

确保更新 authorsdescriptiondocumentation(如果适用),以及任何其他相关信息。

第(4)步:发布到 crates.io

在您的项目目录中运行以下命令来发布您的包:

bash 复制代码
cargo publish

这将会把您的包上传到 crates.io

第6步:通过 Cargo 安装

一旦您的包被成功发布到 crates.io,其他人就可以通过运行以下命令来安装您的工具:

bash 复制代码
cargo install my_dev_tool

展示成果

shell 复制代码
% cargo install my_dev_tool
    Updating crates.io index
  Downloaded my_dev_tool v0.1.0
  Downloaded 1 crate (10.7 KB) in 2.64s
  Installing my_dev_tool v0.1.0
    Updating crates.io index
   Compiling autocfg v1.1.0
   Compiling utf8parse v0.2.1
   Compiling anstyle-query v1.0.0
   Compiling colorchoice v1.0.0
   Compiling anstyle v1.0.4
   Compiling strsim v0.10.0
   Compiling clap_lex v0.6.0
   Compiling iana-time-zone v0.1.58
   Compiling urlencoding v2.1.3
   Compiling anstyle-parse v0.2.2
   Compiling anstream v0.6.4
   Compiling num-traits v0.2.17
   Compiling clap_builder v4.4.8
   Compiling chrono v0.4.31
   Compiling clap v4.4.8
   Compiling my_dev_tool v0.1.0
    Finished release [optimized] target(s) in 7.84s
  Installing /home/maocg/.cargo/bin/my_dev_tool
   Installed package `my_dev_tool v0.1.0` (executable `my_dev_tool`)

% my_dev_tool timestamp 1609459200
2021-01-01T08:00:00+08:00

注意事项

  • 在发布之前,请确保代码和文档是清晰和完整的,这对于其他人使用你的工具非常重要。
  • 你需要更新版本号(在 Cargo.toml 中的 version 字段)在每次发布新的更改时。
  • 如果你的工具包含敏感或专有信息,请在发布前仔细检查。
相关推荐
小杍随笔1 小时前
【Rust Exercism 练习详解:Anagram + Space Age + Sublist(附完整代码与深度解读)】
开发语言·rust·c#
Rust研习社3 小时前
Rust 字符串与切片实战
rust
朝阳5813 小时前
局域网聊天工具
javascript·rust
朝阳5813 小时前
我做了一个局域网传文件的小工具,记录一下
javascript·rust
Rust语言中文社区17 小时前
【Rust日报】用 Rust 重写的 Turso 是一个更好的 SQLite 吗?
开发语言·数据库·后端·rust·sqlite
小杍随笔1 天前
【Rust 半小时速成(2024 Edition 更新版)】
开发语言·后端·rust
Source.Liu1 天前
【office2pdf】office2pdf 纯 Rust 实现的 Office 转 PDF 库
rust·pdf·office2pdf
洛依尘1 天前
深入浅出 Rust 生命周期:它不是语法负担,而是借用关系的说明书
后端·rust
Rust研习社1 天前
通过示例学习 Rust 模式匹配
rust
PaytonD1 天前
基于 GPUI 实现 WebScoket 服务端之服务篇
后端·rust