cli.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2019-2023 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-host HOST Sets the host of the underlying WebDriver (Linux only)
  13. --native-driver PATH Sets the path to the native WebDriver binary
  14. ";
  15. #[derive(Debug, Clone)]
  16. pub struct Args {
  17. pub port: u16,
  18. pub native_port: u16,
  19. pub native_host: String,
  20. pub native_driver: Option<PathBuf>,
  21. }
  22. impl From<pico_args::Arguments> for Args {
  23. fn from(mut args: pico_args::Arguments) -> Self {
  24. // if the user wanted help, we don't care about parsing the rest of the args
  25. if args.contains(["-h", "--help"]) {
  26. println!("{}", HELP);
  27. std::process::exit(0);
  28. }
  29. let native_driver = match args.opt_value_from_str("--native-driver") {
  30. Ok(native_driver) => native_driver,
  31. Err(e) => {
  32. eprintln!("Error while parsing option --native-driver: {}", e);
  33. std::process::exit(1);
  34. }
  35. };
  36. let parsed = Args {
  37. port: args.value_from_str("--port").unwrap_or(4444),
  38. native_port: args.value_from_str("--native-port").unwrap_or(4445),
  39. native_host: args
  40. .value_from_str("--native-host")
  41. .unwrap_or(String::from("127.0.0.1")),
  42. native_driver,
  43. };
  44. // be strict about accepting args, error for anything extraneous
  45. let rest = args.finish();
  46. if !rest.is_empty() {
  47. eprintln!("Error: unused arguments left: {:?}", rest);
  48. eprintln!("{}", HELP);
  49. std::process::exit(1);
  50. }
  51. parsed
  52. }
  53. }