preftool-clap-derive 0.1.0

Configuration library for CLI tools/servers.
Documentation
use clap::App;
use preftool_clap::ClapConfigExt;
use preftool_clap_derive::ClapConfig;

#[derive(ClapConfig)]
pub enum Binding {
  Unencrypted {
    #[preftool_clap(help = "Server host")]
    host: String,

    #[preftool_clap(help = "Server port")]
    port: String,
  },

  Encrypted {
    #[preftool_clap(help = "Server host")]
    host: String,

    #[preftool_clap(help = "Server port")]
    port: String,

    #[preftool_clap(help = "Server certificate (enables TLS)")]
    cert: String,
  },
}

#[derive(ClapConfig)]
pub struct Server {
  binding: Binding,
}

#[test]
fn sets_host() {
  let args = vec!["test", "--binding-host=localhost"];
  let result = Server::from_cli_args(App::new("test"), args).unwrap();

  assert_eq!(result.len(), 1);
  assert_eq!(result.get("binding:host").unwrap(), "localhost");
  assert_eq!(result.get("binding:port"), None);
}

#[test]
fn sets_port() {
  let args = vec!["test", "--binding-port", "8080"];
  let result = Server::from_cli_args(App::new("test"), args).unwrap();

  assert_eq!(result.len(), 1);
  assert_eq!(result.get("binding:port").unwrap(), "8080");
  assert_eq!(result.get("binding:host"), None);
}

#[test]
fn sets_all() {
  let args = vec![
    "test",
    "--binding-port",
    "8080",
    "--binding-host=otherhost",
    "--binding-cert=somecert",
  ];
  let result = Server::from_cli_args(App::new("test"), args).unwrap();

  assert_eq!(result.len(), 3);
  assert_eq!(result.get("binding:port").unwrap(), "8080");
  assert_eq!(result.get("binding:host").unwrap(), "otherhost");
  assert_eq!(result.get("binding:cert").unwrap(), "somecert");
}