use std::io;
use std::io::Write;
use hextool::{Convert, Hex, UnHex};
use crate::cli::{Commands};
fn main() {
let args = cli::parse();
let result = match &args.command {
Commands::Hex { numeric, split, input } => {
Hex::convert(input, *numeric, *split)
}
Commands::Unhex { numeric, split, input } => {
UnHex::convert(input, *numeric, *split)
}
};
io::stdout().write_all(result.as_bytes()).unwrap();
io::stdout().flush().unwrap();
}
mod cli {
use std::{env, io};
use std::ffi::OsString;
use clap::{Parser, Subcommand};
pub(crate) fn parse() -> CLI {
match CLI::try_parse() {
Ok(c) => { c }
Err(_) => {
if atty::is(atty::Stream::Stdin) {
return CLI::parse();
}
parse_stdin()
}
}
}
fn parse_stdin() -> CLI {
let mut input = String::from("");
io::stdin().lines().for_each(|line| {
input.push_str(&line.unwrap());
input.push_str("\n");
});
let input = input.trim();
let mut args: Vec<OsString> = env::args_os().collect();
args.push(input.into());
match CLI::try_parse_from(args) {
Ok(c) => { c }
Err(e) => { e.exit() }
}
}
#[derive(Parser)]
#[command(author, version, about)]
#[command(propagate_version = true)]
pub(crate) struct CLI {
#[command(subcommand)]
pub(crate) command: Commands,
}
#[derive(Subcommand)]
pub(crate) enum Commands {
Hex {
#[clap(short, long, action)]
numeric: bool,
#[clap(short, long, action)]
split: bool,
input: String,
},
Unhex {
#[clap(short, long, action)]
numeric: bool,
#[clap(short, long, action)]
split: bool,
input: String,
},
}
}