Skip to content

Commit

Permalink
命令行部分用 clap 可以提供更人性化的提示
Browse files Browse the repository at this point in the history
  • Loading branch information
zuisong committed Jun 27, 2019
1 parent 5c09276 commit 38725d5
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 14 deletions.
109 changes: 102 additions & 7 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ failure = "0.1.5"
log = "0.4.6"
simple_logger = "1.3.0"
wasm-bindgen = "0.2.47"
clap = "2.33.0"
42 changes: 35 additions & 7 deletions src/bin/chen_lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,47 @@ use log::Level;
use log::*;
use std::fs::OpenOptions;
use std::io::Read;

extern crate clap;

use clap::{App, Arg};

fn main() -> Result<(), failure::Error> {
simple_logger::init_with_level(Level::Warn)?;
let args: Vec<_> = std::env::args().collect();
if args.len() < 2 {
panic!("请指定要执行的源代码文件");
}
let s = std::env::current_dir()?.join(&args[1]);
let matches = App::new("chen_lang")
.version("0.0.1")
.author("zuisong <[email protected]>")
.about("a super tiny and toy language write by rust")
.arg(
Arg::with_name("INPUT_FILE")
.help("要执行的源代码文件")
.required(true)
.index(1),
)
.arg(
Arg::with_name("v")
.short("v")
.required(false)
.multiple(true)
.help("v越多日志级别越低"),
)
.get_matches();
let log_level = match matches.occurrences_of("v") {
0 => Level::Error,
1 => Level::Warn,
2 => Level::Info,
3 => Level::Debug,
4 | _ => Level::Trace,
};

simple_logger::init_with_level(log_level)?;

let code_file = matches.value_of("INPUT_FILE").unwrap();
let s = std::env::current_dir()?.join(code_file);

debug!("{:?}", s);
let mut f = OpenOptions::new().read(true).open(s)?;

let mut v = vec![];

f.read_to_end(&mut v)?;
let code = String::from_utf8(v)?;

Expand Down

0 comments on commit 38725d5

Please sign in to comment.