Skip to content

Commit

Permalink
update clap config
Browse files Browse the repository at this point in the history
  • Loading branch information
zuisong committed Sep 5, 2023
1 parent 2b57ae8 commit 760a0d4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ authors = ["zuisong <[email protected]>"]
default-run = "chen_lang"
edition = "2021"
name = "chen_lang"
resolver = "2"
version = "0.1.0"

description = "A tiny programming language written in rust"
[dependencies]
anyhow = "1.0.75"
clap = { version = "4.4.1", optional = true, features = ["derive"] }
Expand Down
60 changes: 31 additions & 29 deletions src/bin/chen_lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@ use std::{
};

use anyhow::{Ok, Result};
use clap::{Command, CommandFactory, Parser};
use clap::{builder::PossibleValuesParser, Command, CommandFactory, Parser};
use clap_complete::{generate, Generator, Shell};
use log::*;
use log::{LevelFilter, *};

use crate::clap::builder::TypedValueParser;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about)]
struct Args {
#[command(subcommand)]
command: Option<SubCommand>,
/// v越多日志级别越低 (-vv is Info, -vvv is Debug
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8,
/// log level
#[arg(short, long)]
#[arg(default_value_t = LevelFilter::Info,)]
#[arg(value_parser=
PossibleValuesParser::new(["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"])
.map(|s| s.parse::<LevelFilter>().unwrap()),
)]
log_level: LevelFilter,
}

#[derive(Parser, Debug, Clone)]
enum SubCommand {
/// Generate tab-completion scripts for your shell
Expand All @@ -31,32 +38,27 @@ enum SubCommand {
code_file: String,
},
}

fn main() -> Result<()> {
let matches = Args::parse();
let log_level = match matches.verbose {
0 => LevelFilter::Error,
1 => LevelFilter::Warn,
2 => LevelFilter::Info,
3 => LevelFilter::Debug,
4 => LevelFilter::Trace,
_ => LevelFilter::Trace,
};
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(log_level.as_str()))
.default_format()
.format(|buf, record| -> Result<(), io::Error> {
let style = buf.style();
let timestamp = buf.timestamp();
writeln!(
buf,
"{} {} [{}:{}]: {}",
record.level(),
timestamp,
record.file().unwrap_or(""),
record.line().unwrap_or(0),
style.value(record.args())
)
})
.init();
env_logger::Builder::from_env(
env_logger::Env::default().default_filter_or(matches.log_level.as_str()),
)
.default_format()
.format(|buf, record| -> Result<(), io::Error> {
let style = buf.style();
let timestamp = buf.timestamp();
writeln!(
buf,
"{} {} [{}:{}]: {}",
record.level(),
timestamp,
record.file().unwrap_or(""),
record.line().unwrap_or(0),
style.value(record.args())
)
})
.init();

match matches.command {
None => Args::command().print_help()?,
Expand Down

0 comments on commit 760a0d4

Please sign in to comment.