|
1 | 1 | extern crate avr_mcu;
|
2 | 2 |
|
3 |
| -use std::path::Path; |
| 3 | +use clap::{App, Arg}; |
4 | 4 |
|
5 |
| -fn main() { |
6 |
| - let crate_root = Path::new(env!("CARGO_MANIFEST_DIR")); |
7 |
| - |
8 |
| - let mcus = if cfg!(feature = "all_mcus") { |
9 |
| - avr_mcu::microcontrollers().to_owned() |
10 |
| - } else { |
11 |
| - // By default, when compiling for AVR we should hard error if |
12 |
| - // microcontroller is not specified. |
13 |
| - if cfg!(arch = "avr") { |
14 |
| - let current_mcu = avr_mcu::current::mcu() |
15 |
| - .expect("no target cpu set"); |
16 |
| - vec![current_mcu] |
17 |
| - } else { |
18 |
| - // On non-avr architectures, support all microcontrollers. |
19 |
| - avr_mcu::microcontrollers().to_owned() |
20 |
| - } |
21 |
| - }; |
| 5 | +use std::path::{Path, PathBuf}; |
| 6 | + |
| 7 | +#[derive(Clone, Debug)] |
| 8 | +struct Config { |
| 9 | + output_directory: PathBuf, |
| 10 | + mcus: Vec<avr_mcu::Mcu>, |
| 11 | +} |
22 | 12 |
|
23 |
| - // Useful for test |
24 |
| - // let mcus = vec![ |
25 |
| - // avr_mcu::microcontroller("ata5795").clone(), |
26 |
| - // avr_mcu::microcontroller("atmega328").clone(), // required when compiling for PC |
27 |
| - // ]; |
| 13 | +fn get_cli_config() -> Config { |
| 14 | + let matches = App::new(env!("CARGO_PKG_NAME")) |
| 15 | + .version("1.0") |
| 16 | + .author(env!("CARGO_PKG_AUTHORS")) |
| 17 | + .about(env!("CARGO_PKG_DESCRIPTION")) |
| 18 | + .arg(Arg::with_name("out-dir") |
| 19 | + .short("o") |
| 20 | + .long("out-dir") |
| 21 | + .value_name("DIRECTORY") |
| 22 | + .help("Sets the directory path that will contain the newly generated Rust source code files") |
| 23 | + .takes_value(true) |
| 24 | + .required(true)) |
| 25 | + .arg(Arg::with_name("mcus") |
| 26 | + .short("m") |
| 27 | + .long("mcus") |
| 28 | + .value_name("COMMA SEPARATED MCU LIST") |
| 29 | + .help("Generate outputs for a specific microcontroller or microcontrollers rather than the default of all") |
| 30 | + .takes_value(true)) |
| 31 | + .get_matches(); |
| 32 | + let output_directory = Path::new(matches.value_of("out-dir").unwrap()).to_owned(); |
| 33 | + |
| 34 | + let mcus = matches.values_of("mcus") |
| 35 | + .map(|m| m.flat_map(|a| a.split(",").map(|s| avr_mcu::microcontroller(s).clone())).collect()) |
| 36 | + .unwrap_or_else(|| avr_mcu::microcontrollers().to_owned() ); |
| 37 | + |
| 38 | + Config { output_directory, mcus } |
| 39 | +} |
| 40 | + |
| 41 | +fn main() { |
| 42 | + let config = get_cli_config(); |
28 | 43 |
|
29 |
| - gen::all(&crate_root.join("src").join("gen"), &mcus).unwrap(); |
| 44 | + gen::all(&config.output_directory, &config.mcus).unwrap(); |
30 | 45 | }
|
31 | 46 |
|
32 | 47 | mod gen {
|
|
0 commit comments