This repository has been archived on 2025-04-22. You can view files and clone it, but cannot push or open issues or pull requests.
rust-test/file_manager_cli/src/main.rs
2024-11-15 21:16:51 +08:00

56 lines
1.7 KiB
Rust

use clap::{Arg, Command};
use std::fs;
use std::io;
use std::path;
use std::path::Path;
fn create_file(path: &str) -> io::Result<()> {
fs::File::create(path)?;
println!("File '{}' created successfully.", path);
Ok(())
}
fn main() {
println!("Hello, world!");
let matches = Command::new("File Manager CLI")
.version("1.0")
.author("Yu LinLing")
.about("A simple file management tool")
.subcommand(
Command::new("create")
.about("Create a file or dircetory")
.arg(
Arg::new("path")
.help("Path to the file or directory")
.required(true)
.index(1),
)
.arg(
Arg::new("type")
.help("Type of entity to create file or dic")
.required(true)
.long("type")
.value_parser(clap::value_parser!(String))
.num_args(1),
),
)
.get_matches();
if let Some(matches) = matches.subcommand_matches("create") {
let path = matches.get_one::<String>("path").unwrap();
let entity_type = matches.get_one::<String>("type").unwrap();
if entity_type == "file" {
if let Err(e) = create_file(path) {
eprintln!("Error creating file: {}", e);
}
} else if entity_type == "dir" {
if let Err(e) = create_file(path) {
eprintln!("Error creating directory: {}", e);
}
} else {
println!("Invalid type. Use 'file' or 'dir' ");
}
}
}