// WARN: This file is auto generated by flood-tide-gen
const OPTIONS_TEXT: &str = r"Options:
  -a, --all                 output the all statistics of text, exclude ascii map
  -b, --bytes               output the byte counts
  -c, --chars               output the unicode character counts
  -l, --lines               output the line counts
      --map-ascii           output the ascii map statistics
  -m, --max-line-bytes      output the maximum byte counts of line
  -w, --words               output the word counts
      --locale <loc>        locale of number format: en, fr, ... posix
  -?, --query <q>           display available names of locale and exit

  -H, --help        display this help and exit
  -V, --version     display version information and exit
  -X <x-options>    x options. try -X help
";

#[repr(u8)]
#[derive(Debug, PartialEq)]
enum CmdOp {
    All,
    Bytes,
    Chars,
    Lines,
    MapAscii,
    MaxLineBytes,
    Words,
    Locale,
    Query,
    Help,
    Version,
    UcX,
}

impl std::convert::From<u8> for CmdOp {
    fn from(value: u8) -> Self {
        unsafe { std::mem::transmute(value) }
    }
}
impl CmdOp {
    pub const fn to(self) -> OptNum {
        self as OptNum
    }
}

#[rustfmt::skip]
const OPT_ARY: [Opt;12] = [
    Opt { sho: b'X', lon: "",              has: Arg::Yes, num: CmdOp::UcX.to(), },
    Opt { sho: b'a', lon: "all",           has: Arg::No,  num: CmdOp::All.to(), },
    Opt { sho: b'b', lon: "bytes",         has: Arg::No,  num: CmdOp::Bytes.to(), },
    Opt { sho: b'c', lon: "chars",         has: Arg::No,  num: CmdOp::Chars.to(), },
    Opt { sho: b'H', lon: "help",          has: Arg::No,  num: CmdOp::Help.to(), },
    Opt { sho: b'l', lon: "lines",         has: Arg::No,  num: CmdOp::Lines.to(), },
    Opt { sho: 0u8,  lon: "locale",        has: Arg::Yes, num: CmdOp::Locale.to(), },
    Opt { sho: 0u8,  lon: "map-ascii",     has: Arg::No,  num: CmdOp::MapAscii.to(), },
    Opt { sho: b'm', lon: "max-line-bytes",has: Arg::No,  num: CmdOp::MaxLineBytes.to(), },
    Opt { sho: b'?', lon: "query",         has: Arg::Yes, num: CmdOp::Query.to(), },
    Opt { sho: b'V', lon: "version",       has: Arg::No,  num: CmdOp::Version.to(), },
    Opt { sho: b'w', lon: "words",         has: Arg::No,  num: CmdOp::Words.to(), },
];

#[rustfmt::skip]
const OPT_ARY_SHO_IDX: [(u8,usize);10] = [
(b'?',9),(b'H',4),(b'V',10),(b'X',0),(b'a',1),(b'b',2),(b'c',3),(b'l',5),(b'm',8),(b'w',11),];

#[derive(Debug, Default, PartialEq)]
pub struct CmdOptConf {
    pub prog_name: String,
    //
    pub flg_all: bool,
    pub flg_bytes: bool,
    pub flg_chars: bool,
    pub flg_lines: bool,
    pub flg_map_ascii: bool,
    pub flg_max_line_bytes: bool,
    pub flg_words: bool,
    pub opt_locale: OptLocaleLoc,
    pub opt_query: Option<String>,
    pub flg_help: bool,
    pub flg_version: bool,
    pub opt_uc_x: Vec<OptUcXParam>,
    //
    pub arg_params: Vec<String>,
}

impl flood_tide::HelpVersion for CmdOptConf {
    fn is_help(&self) -> bool {
        self.flg_help
    }
    fn is_version(&self) -> bool {
        self.flg_version
    }
}

fn value_to_string(nv: &NameVal<'_>) -> Result<String, OptParseError> {
    match nv.val {
        Some(x) => Ok(x.to_string()),
        None => Err(OptParseError::missing_option_argument(&nv.opt.lon_or_sho())),
    }
}

fn value_to_opt_locale_loc(nv: &NameVal<'_>) -> Result<OptLocaleLoc, OptParseError> {
    match nv.val {
        Some(s) => match FromStr::from_str(s) {
            Ok(x) => Ok(x),
            Err(err) => Err(OptParseError::invalid_option_argument(
                &nv.opt.lon_or_sho(),
                &err.to_string(),
            )),
        },
        None => Err(OptParseError::missing_option_argument(&nv.opt.lon_or_sho())),
    }
}

fn value_to_opt_uc_x_param(nv: &NameVal<'_>) -> Result<OptUcXParam, OptParseError> {
    match nv.val {
        Some(s) => match FromStr::from_str(s) {
            Ok(x) => Ok(x),
            Err(err) => Err(OptParseError::invalid_option_argument(
                &nv.opt.lon_or_sho(),
                &err.to_string(),
            )),
        },
        None => Err(OptParseError::missing_option_argument(&nv.opt.lon_or_sho())),
    }
}
