当前位置: 首页 > article >正文

【Rust】6、练习:自己实现 ls

文章目录

  • 一、CLI Utilities

Rust 学完,想找些练手的项目,怎么办?可参考这个文章,列出的 15 个项目。

一、CLI Utilities

github

参考
lsd 项目地址

cargo.toml

[dependencies]
clap = "2"

main.rs

extern crate clap;

use clap::{App, Arg};

fn main() {
    let app = App::new("l")
        .version("1.0.0")
        .author("y")
        .about("this is a l")
        .arg(
            Arg::with_name("all")
                .short("a")
                .help("show all file and dir"),
        )
        .arg(Arg::with_name("long").short("l").help("show file all info"))
        .arg(Arg::with_name("FILE").multiple(true).default_value("."))
        .get_matches();
    let all = app.is_present("all");
    let long = app.is_present("long");
    let files: Vec<&str> = app.values_of("FILE").expect("not get any file").collect();
    println!("all: {}, long: {}, File: {:?}", all, long, files);
}

运行已经可以收到命令行参数了,如下:

# cargo run
all: false, long: false, File: ["."]

# cargo run -- -a -l /tmp /etc
all: true, long: true, File: ["/tmp", "/etc"]

接下来读取某路径下的文件:

extern crate clap;

use std::{fs::read_dir, path::Path};

fn main() {
    let files = vec!["/Users"];
    for file in files {
        let path = Path::new(file);
        if path.is_dir() {
            println!("{} is a directory", file);
            let somes = read_dir(path).expect("not get this path");
            for some in somes {
                if let Ok(ref entry) = some {
                    let name = entry
                        .file_name()
                        .to_str()
                        .expect("file name convert str fail")
                        .to_string();
                    let data = entry.metadata().expect("no metadata");
                    println!("{} {:?}", name, data)
                }
            }
        }
    }
}

接下来把两个程序结合:

extern crate clap;

use clap::{App, Arg};
use std::fs::read_dir;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;

#[derive(Debug)]
pub struct Option {
    display_all: bool,
    display_long: bool,
}

fn main() {
    let app = App::new("l")
        .version("1.0.0")
        .author("lome")
        .about("this is a l")
        .arg(
            Arg::with_name("all")
                .short("a")
                .help("Show all file and dir"),
        )
        .arg(Arg::with_name("long").short("l").help("show file all info"))
        .arg(Arg::with_name("FILE").multiple(true).default_value("."))
        .get_matches();

    let option = Option {
        display_all: app.is_present("all"),
        display_long: app.is_present("long"),
    };

    let files: Vec<&str> = app.values_of("FILE").expect("not get any file").collect();

    for file in files {
        let path = Path::new(file);
        if path.is_dir() {
            println!("{}: ", file);
            let somes = read_dir(path).expect("not get this path");
            for some in somes {
                if let Ok(ref entry) = some {
                    let name = entry
                        .file_name()
                        .to_str()
                        .expect("file name convert str fail")
                        .to_string();
                    let data = entry.metadata().expect("no metadata");
                    if option.display_long {
                        if option.display_all || !name.starts_with(".") {
                            let permission = data.permissions().mode();
                            let systime = data.modified().expect("get system time error");
                            println!(
                                "    {} Permission: {:o}  Systime: {:?}",
                                name, permission, systime
                            );
                        }
                    } else {
                        if option.display_all || !name.starts_with(".") {
                            println!("    {}", name);
                        }
                    }
                }
            }
        }
    }
}

// code result:
Output:
$ ./l  ./test/ ./test/test/
./test/: 
    test.txt
    test
./test/test/: 
    test.txt

$ ./l -a ./test/ ./test/test/
./test/: 
    test.txt
    test
    .test.txt
./test/test/: 
    test.txt

$ ./l -al ./test/ ./test/test/
./test/: 
    test.txt Permission: 100644  Systime: SystemTime { tv_sec: 1573805933, tv_nsec: 470337261 }
    test Permission: 40755  Systime: SystemTime { tv_sec: 1573805940, tv_nsec: 228706396 }
    .test.txt Permission: 100644  Systime: SystemTime { tv_sec: 1573806564, tv_nsec: 333715123 }
./test/test/: 
    test.txt Permission: 100644  Systime: SystemTime { tv_sec: 1573805940, tv_nsec: 228706396 }

http://www.kler.cn/news/133606.html

相关文章:

  • 【STM32】ADC(模拟/数字转换)
  • JVM:字节码文件,类的生命周期,类加载器
  • wpf devexpress 创建布局
  • 蓝桥杯第三周算法竞赛D题E题
  • Android studio访问选程https接口(.crt handshake)
  • 一文了解ChatGPT Plus如何完成论文写作和AI绘图
  • rust内存优化
  • .NET CLR介绍
  • 处理多个axios请求
  • Hive 定义变量 变量赋值 引用变量
  • centos 安装 docker
  • echarts双轴刻度线y轴刻度线对齐
  • 支持4KHz回报还能无线充电,简约不简单的雷柏VT3S游戏鼠标上手
  • Mongodb 副本集名称重命名
  • Vue3 动态设置 ref
  • 2018年五一杯数学建模C题江苏省本科教育质量综合评价解题全过程文档及程序
  • 使用html2canvas转换table为图片时合并单元格rowspan失效,无边框显示问题解决(React实现)
  • python从基础到UnitTest框架-python基础语法
  • iOS_折叠展开 FoldTextView
  • 手机LiDAR-based激光雷达标定板提高无人汽车智能化程度
  • Nas搭建webdav服务器并同步Zotero科研文献
  • 持续集成部署-k8s-配置与存储-配置管理:SubPath
  • 新版JetBrains ToolBox【Windows】修改应用安装位置
  • Node.js之path路径模块
  • ubuntu22.04换源
  • NSSCTF第13页(2)
  • 音视频项目—基于FFmpeg和SDL的音视频播放器解析(十二)
  • golang学习笔记——select 判断语句
  • 动态加载js文件的方法封装
  • 系统安全-常见的几种sql注入攻击的方式