Skip to content

Commit

Permalink
Add: rust tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Hayao0819 committed Jun 22, 2024
1 parent a1d4451 commit d930b7f
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 1 deletion.
2 changes: 1 addition & 1 deletion posts/20240620/why-shell/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: ""
date: 2024-06-20T11:36:57.022489+09:00
categories:
- 技術系
draft: true
draft: false
publish: true
---

Expand Down
1 change: 1 addition & 0 deletions tools-rs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
237 changes: 237 additions & 0 deletions tools-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tools-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "blogtool-rs"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.5.7", features = ["derive"] }
69 changes: 69 additions & 0 deletions tools-rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::path::PathBuf;

use clap::{Parser, Subcommand};
// mod clap;

#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
/// Optional name to operate on
name: Option<String>,

/// Sets a custom config file
#[arg(short, long, value_name = "FILE")]
config: Option<PathBuf>,

/// Turn debugging information on
#[arg(short, long, action = clap::ArgAction::Count)]
debug: u8,

#[command(subcommand)]
command: Option<Commands>,
}

#[derive(Subcommand)]
enum Commands {
/// does testing things
Test {
/// lists test values
#[arg(short, long)]
list: bool,
},
}

fn main() {
let cli = Cli::parse();

// You can check the value provided by positional arguments, or option arguments
if let Some(name) = cli.name.as_deref() {
println!("Value for name: {name}");
}

if let Some(config_path) = cli.config.as_deref() {
println!("Value for config: {}", config_path.display());
}

// You can see how many times a particular flag or argument occurred
// Note, only flags can have multiple occurrences
match cli.debug {
0 => println!("Debug mode is off"),
1 => println!("Debug mode is kind of on"),
2 => println!("Debug mode is on"),
_ => println!("Don't be crazy"),
}

// You can check for the existence of subcommands, and if found use their
// matches just as you would the top level cmd
match &cli.command {
Some(Commands::Test { list }) => {
if *list {
println!("Printing testing lists...");
} else {
println!("Not printing testing lists...");
}
}
None => {}
}

// Continued program logic goes here...
}

0 comments on commit d930b7f

Please sign in to comment.