hextool 0.1.3

A simple command line tool to convert hex to string and string to hex
Documentation
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(_) => {
                // check if stdin is empty
                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)]
    /// Commandline tool to convert hex to string and string to hex
    pub(crate) struct CLI {
        #[command(subcommand)]
        pub(crate) command: Commands,
    }

    #[derive(Subcommand)]
    pub(crate) enum Commands {
        /// Change input to hex
        Hex {
            #[clap(short, long, action)]
            /// Numeric flag, if set, will assume input is a number and convert to hex
            numeric: bool,

            #[clap(short, long, action)]
            /// Split flag, if set, will split the output into bytes
            split: bool,

            /// Input to convert
            input: String,
        },
        /// Change input from hex
        Unhex {
            #[clap(short, long, action)]
            /// Numeric flag, if set, will assume input is a number and convert to hex
            numeric: bool,

            #[clap(short, long, action)]
            /// Split flag, if set, will split the output into bytes
            split: bool,

            /// Input to convert
            input: String,
        },
    }
}