|
| 1 | +extern crate clap; |
| 2 | + |
| 3 | +use clap::{Arg, App}; |
| 4 | +use std::fs; |
| 5 | +use std::process; |
| 6 | + |
| 7 | +static LICENSE: &'static str = "zrtstr licensed under GNU General Public License version 2 or later; |
| 8 | +Full License available at <http://gnu.org/licenses/gpl.html>. |
| 9 | +This is free software: you are free to change and redistribute it. |
| 10 | +There is NO WARRANTY, to the extent permitted by law."; |
| 11 | + |
| 12 | +pub struct Conf { |
| 13 | + pub dither: u32, |
| 14 | + pub dry_run: bool, |
| 15 | + pub no_overwrites: bool, |
| 16 | +} |
| 17 | + |
| 18 | +pub fn get() -> (Option<String>, Conf) { |
| 19 | + let matches = App::new("zrtstr") |
| 20 | + .author(crate_authors!()) |
| 21 | + .version(crate_version!()) |
| 22 | + .about("Check stereo WAV-file for identical channels, detecting \ |
| 23 | + faux-stereo files generated by some audio-editing software and \ |
| 24 | + DAWs.\nOutputs a true-mono WAV file on detection of \ |
| 25 | + faux-stereo. Takes left channel of the input file, writes in the \ |
| 26 | + same location with -MONO suffix in file name.") |
| 27 | + .arg(Arg::with_name("INPUT") |
| 28 | + .help("Path to the input file to process. If no input given, \ |
| 29 | + process all WAV files in current directory.") |
| 30 | + .index(1) |
| 31 | + .validator(validate_path)) |
| 32 | + .arg(Arg::with_name("dither") |
| 33 | + .short("d") |
| 34 | + .long("dither") |
| 35 | + .value_name("THRESHOLD") |
| 36 | + .required(false) |
| 37 | + .help("Set threshold for sample level difference to process \ |
| 38 | + dithered audio.{n}Positive number (amplitude delta).") |
| 39 | + .next_line_help(true) |
| 40 | + .takes_value(true) |
| 41 | + .default_value("10") |
| 42 | + .validator(validate_u32)) |
| 43 | + .arg(Arg::with_name("license") |
| 44 | + .short("l") |
| 45 | + .long("license") |
| 46 | + .help("Display the license information.")) |
| 47 | + .arg(Arg::with_name("dry_run") |
| 48 | + .short("n") |
| 49 | + .long("nowrite") |
| 50 | + .help("Disable saving converted mono files. Analyze pass only.") |
| 51 | + .required(false)) |
| 52 | + .arg(Arg::with_name("no_overwrites") |
| 53 | + .short("p") |
| 54 | + .long("protect") |
| 55 | + .help("Disable overwriting when saving converted mono files.") |
| 56 | + .required(false)) |
| 57 | + .get_matches(); |
| 58 | + |
| 59 | + if matches.is_present("license") { |
| 60 | + println!("{}", LICENSE); |
| 61 | + println!("\nclap (Command Line Argument Parser) License:"); |
| 62 | + println!(include_str!("../LICENSE-CLAP")); |
| 63 | + process::exit(0); |
| 64 | + } |
| 65 | + |
| 66 | + let conf = Conf { |
| 67 | + dither: if matches.is_present("dither") { |
| 68 | + value_t!(matches, "dither", u32).unwrap_or(10) |
| 69 | + } else { |
| 70 | + 0 |
| 71 | + }, |
| 72 | + dry_run: matches.is_present("dry_run"), |
| 73 | + no_overwrites: matches.is_present("no_overwrites"), |
| 74 | + }; |
| 75 | + let input_fname = if matches.is_present("INPUT") { |
| 76 | + Some(matches.value_of("INPUT").unwrap().to_string()) |
| 77 | + } else { |
| 78 | + None |
| 79 | + }; |
| 80 | + (input_fname, conf) |
| 81 | +} |
| 82 | + |
| 83 | +pub fn validate_u32(value: String) -> Result<(), String> { |
| 84 | + value.parse::<u32>() |
| 85 | + .map(|_| ()) |
| 86 | + .map_err(|_| "Supplied parameter is not a valid number!".to_owned()) |
| 87 | +} |
| 88 | + |
| 89 | +pub fn validate_path(path: String) -> Result<(), String> { |
| 90 | + if fs::metadata(&path).is_ok() { |
| 91 | + if path.to_lowercase().ends_with(".wav") { |
| 92 | + Ok(()) |
| 93 | + } else { |
| 94 | + // clap automatically adds "error: " to the beginning of the message. |
| 95 | + Err(String::from("Input file must have .wav extension!")) |
| 96 | + } |
| 97 | + } else { |
| 98 | + Err(String::from("Input file doesn't exist")) |
| 99 | + } |
| 100 | +} |
0 commit comments