clif-util: Switch to using structopt for CLI arguments
This commit is contained in:
@@ -1,9 +1,22 @@
|
||||
use anyhow::{Context, Result};
|
||||
use std::io::{Read, Write};
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
use structopt::StructOpt;
|
||||
|
||||
pub fn run(input: &Path, output: &Path) -> Result<()> {
|
||||
let peepmatic_dsl = if input == Path::new("-") {
|
||||
/// Convert Souper optimizations into Peepmatic DSL.
|
||||
#[derive(StructOpt)]
|
||||
pub struct Options {
|
||||
/// Specify an input file to be used. Use '-' for stdin.
|
||||
#[structopt(parse(from_os_str))]
|
||||
input: PathBuf,
|
||||
|
||||
/// Specify the output file to be used. Use '-' for stdout.
|
||||
#[structopt(short("o"), long("output"), default_value("-"), parse(from_os_str))]
|
||||
output: PathBuf,
|
||||
}
|
||||
|
||||
pub fn run(options: &Options) -> Result<()> {
|
||||
let peepmatic_dsl = if options.input == Path::new("-") {
|
||||
let stdin = std::io::stdin();
|
||||
let mut stdin = stdin.lock();
|
||||
let mut souper_dsl = vec![];
|
||||
@@ -13,18 +26,18 @@ pub fn run(input: &Path, output: &Path) -> Result<()> {
|
||||
let souper_dsl = String::from_utf8(souper_dsl).context("stdin is not UTF-8: {}")?;
|
||||
peepmatic_souper::convert_str(&souper_dsl, Some(Path::new("stdin")))?
|
||||
} else {
|
||||
peepmatic_souper::convert_file(input)?
|
||||
peepmatic_souper::convert_file(&options.input)?
|
||||
};
|
||||
|
||||
if output == Path::new("-") {
|
||||
if options.output == Path::new("-") {
|
||||
let stdout = std::io::stdout();
|
||||
let mut stdout = stdout.lock();
|
||||
stdout
|
||||
.write_all(peepmatic_dsl.as_bytes())
|
||||
.context("error writing to stdout")?;
|
||||
} else {
|
||||
std::fs::write(output, peepmatic_dsl.as_bytes())
|
||||
.with_context(|| format!("error writing to {}", output.display()))?;
|
||||
std::fs::write(&options.output, peepmatic_dsl.as_bytes())
|
||||
.with_context(|| format!("error writing to {}", options.output.display()))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user