args.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. use clap::{arg, command, Parser};
  2. use rand::random;
  3. use crate::config::{Config, IpAddress};
  4. /// # Args结构体
  5. /// 使用clap库对命令行输入进行pasing,产生参数配置
  6. #[derive(Parser, Debug, Clone)]
  7. #[command(author, version, about, long_about = None)]
  8. pub struct Args {
  9. // Count of ping times
  10. #[arg(short, default_value_t = 4)]
  11. count: u16,
  12. // Ping packet size
  13. #[arg(short = 's', default_value_t = 64)]
  14. packet_size: usize,
  15. // Ping ttl
  16. #[arg(short = 't', default_value_t = 64)]
  17. ttl: u32,
  18. // Ping timeout seconds
  19. #[arg(short = 'w', default_value_t = 1)]
  20. timeout: u64,
  21. // Ping interval duration milliseconds
  22. #[arg(short = 'i', default_value_t = 1000)]
  23. interval: u64,
  24. // Ping destination, ip or domain
  25. #[arg(value_parser=IpAddress::parse)]
  26. destination: IpAddress,
  27. }
  28. impl Args {
  29. /// # 将Args结构体转换为config结构体
  30. pub fn as_config(&self) -> Config {
  31. Config {
  32. count: self.count,
  33. packet_size: self.packet_size,
  34. ttl: self.ttl,
  35. timeout: self.timeout,
  36. interval: self.interval,
  37. id: random::<u16>(),
  38. sequence: 1,
  39. address: self.destination.clone(),
  40. }
  41. }
  42. }