seating_cli.rs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //! 换座位命令行入口(开发期对照用;镜像 Python CLI 参数)。
  2. //! 用法:
  3. //! cargo run -p toolbox-core --example seating_cli -- <xlsx> <班级> <样式> [策略] [--mixed] [--count N] [--seed S] [--output O]
  4. use std::path::PathBuf;
  5. use toolbox_core::seating::{run_seating, SeatingInput, Strategy};
  6. fn main() {
  7. let args: Vec<String> = std::env::args().collect();
  8. if args.len() < 4 {
  9. eprintln!("用法: seating_cli <xlsx> <班级> <样式> [good-with-good|good-with-bad|random] [--mixed] [--count N] [--seed S] [--output O]");
  10. std::process::exit(2);
  11. }
  12. let excel = PathBuf::from(&args[1]);
  13. let class = args[2].clone();
  14. let style = args[3].clone();
  15. let mut strategy = Strategy::GoodWithGood;
  16. let mut allow_mixed = false;
  17. let mut count = 1usize;
  18. let mut seed: Option<u64> = None;
  19. let mut output: Option<PathBuf> = None;
  20. let mut i = 4;
  21. while i < args.len() {
  22. match args[i].as_str() {
  23. "--mixed" => allow_mixed = true,
  24. "--count" => {
  25. i += 1;
  26. count = args[i].parse().unwrap_or(1);
  27. }
  28. "--seed" => {
  29. i += 1;
  30. seed = Some(args[i].parse().expect("seed 必须为数字"));
  31. }
  32. "--output" => {
  33. i += 1;
  34. output = Some(PathBuf::from(&args[i]));
  35. }
  36. "good-with-good" => strategy = Strategy::GoodWithGood,
  37. "good-with-bad" => strategy = Strategy::GoodWithBad,
  38. "random" => strategy = Strategy::Random,
  39. _ => {
  40. eprintln!("未知参数: {}", args[i]);
  41. std::process::exit(2);
  42. }
  43. }
  44. i += 1;
  45. }
  46. let inp = SeatingInput {
  47. excel_path: excel,
  48. sheet: None,
  49. class_name: class,
  50. style,
  51. strategy,
  52. allow_mixed,
  53. count,
  54. seed,
  55. output,
  56. };
  57. match run_seating(&inp) {
  58. Ok(res) => {
  59. for l in &res.log {
  60. println!("{}", l);
  61. }
  62. }
  63. Err(e) => {
  64. eprintln!("{}", e);
  65. std::process::exit(1);
  66. }
  67. }
  68. }