cli.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use std::path::PathBuf;
  5. const HELP: &str = "\
  6. USAGE: tauri-driver [FLAGS] [OPTIONS]
  7. FLAGS:
  8. -h, --help Prints help information
  9. OPTIONS:
  10. --port NUMBER Sets the tauri-driver intermediary port
  11. --native-port NUMBER Sets the port of the underlying WebDriver
  12. --native-driver PATH Sets the path to the native WebDriver binary
  13. ";
  14. #[derive(Debug, Clone)]
  15. pub struct Args {
  16. pub port: u16,
  17. pub native_port: u16,
  18. pub native_driver: Option<PathBuf>,
  19. }
  20. impl From<pico_args::Arguments> for Args {
  21. fn from(mut args: pico_args::Arguments) -> Self {
  22. // if the user wanted help, we don't care about parsing the rest of the args
  23. if args.contains(["-h", "--help"]) {
  24. println!("{}", HELP);
  25. std::process::exit(0);
  26. }
  27. let native_driver = match args.opt_value_from_str("--native-driver") {
  28. Ok(native_driver) => native_driver,
  29. Err(e) => {
  30. eprintln!("Error while parsing option --native-driver: {}", e);
  31. std::process::exit(1);
  32. }
  33. };
  34. let parsed = Args {
  35. port: args.value_from_str("--port").unwrap_or(4444),
  36. native_port: args.value_from_str("--native-port").unwrap_or(4445),
  37. native_driver,
  38. };
  39. // be strict about accepting args, error for anything extraneous
  40. let rest = args.finish();
  41. if !rest.is_empty() {
  42. eprintln!("Error: unused arguments left: {:?}", rest);
  43. eprintln!("{}", HELP);
  44. std::process::exit(1);
  45. }
  46. parsed
  47. }
  48. }