I have a CLI program, which uses a custom type to represent possible error conditions:
use std::process::ExitCode;
use bitflags::bitflags;
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ErrorCode(u8);
bitflags! {
impl ErrorCode: u8 {
/// No error.
const NONE = 0b0000_0000;
/// The boot parameters and the `gateway_info.json` contain different serial numbers.
const INCONSISTENT_SERIAL_NUMBER = 0b0000_0001;
/// The boot parameters and the `gateway_info.json` contain different MAC addresses.
const INCONSISTENT_MAC_ADDRESS = 0b0000_0010;
/// No serial number is configured.
const MISSING_SERIAL_NUMBER = 0b0000_0100;
/// No MAC address is configured.
const MISSING_MAC_ADDRESS = 0b0000_1000;
/// No preshared key is configured.
const MISSING_PRESHARED_KEY = 0b0001_0000;
/// Error in the `gateway_info.json`.
const GATEWAY_INFO_ERROR = 0b0010_0000;
/// Error in the boot parameters.
const BOOT_PARAMETERS_ERROR = 0b0100_0000;
}
}
impl From<ErrorCode> for ExitCode {
fn from(error_code: ErrorCode) -> Self {
match error_code.0 {
0 => Self::SUCCESS,
n => Self::from(n),
}
}
}
Is there a simple way to integrate this into clap's help screen, e.g. by means of after_help
?
I'd like this to automagically work with the struct and the docstrings on bitflags' constants, just like cargo doc
would produce the according HTML docs.
Is there something readily available, or do I need to write my own macro for that?