clap_fmt 0.1.1

Serialize a clap arg parser into the command-line arguments.
Documentation
use clap::{CommandFactory, Parser};
use serde::Serialize;

mod serializer;
use serializer::to_args;

/// Trait for converting clap Parser structs back to command-line arguments
pub trait FmtArgs {
    /// Convert the struct instance to command-line arguments
    fn to_args(&self) -> Vec<String>;

    /// Convert to args with a program name prefix
    fn to_args_with_program(&self, program: &str) -> Vec<String>;
}

// Blanket implementation for any type that has the required traits
impl<T> FmtArgs for T
where
    T: Parser + CommandFactory + Serialize,
{
    fn to_args(&self) -> Vec<String> {
        let cmd = Self::command();
        to_args(self, &cmd).unwrap_or_else(|e| {
            panic!("Failed to convert to args: {e}");
        })
    }

    fn to_args_with_program(&self, program: &str) -> Vec<String> {
        let mut result = vec![program.to_string()];
        result.extend(self.to_args());
        result
    }
}