Created
September 21, 2024 06:49
-
-
Save oscarvarto/10a8aa4b6489c0a264f3e23152ae1e42 to your computer and use it in GitHub Desktop.
Simple command line parser with clap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use clap::Parser; | |
use std::fmt::Debug; | |
use std::str::FromStr; | |
#[derive(Debug, PartialEq, Clone)] | |
enum Browser { | |
Chrome, | |
Firefox, | |
Edge, | |
} | |
impl FromStr for Browser { | |
type Err = &'static str; | |
fn from_str(s: &str) -> Result<Self, Self::Err> { | |
match s.to_lowercase().as_str() { | |
"chrome" => Ok(Browser::Chrome), | |
"firefox" => Ok(Browser::Firefox), | |
"edge" => Ok(Browser::Edge), | |
_ => Err("Invalid browser option"), | |
} | |
} | |
} | |
// Simple command-line argument parser | |
#[derive(Parser, Debug)] | |
#[command(version, about, long_about = None)] | |
struct Args { | |
// Name of the environment (and associated properties) | |
#[arg(long, default_value = "v2.0")] | |
env: String, | |
// (Optional) Test suite to execute in src/test/resources/testsuites | |
#[arg(long, default_value = None)] | |
xml: Option<String>, | |
// (Optional) Browser to use for testing | |
#[arg(long, default_value = "Chrome")] | |
browser: Browser, | |
// (Optional) Selenium WebDriverWait value in seconds | |
#[arg(long, default_value = "10")] | |
wait: u8, | |
// (Optional) Regex pattern for test cases to execute with maven failsafe plugin | |
#[arg(long, default_value = ".*")] | |
tests: String, | |
} | |
// cargo run -- --browser=Firefox --env=v1.0 --wait=30 --xml=smoke.xml | |
fn main() { | |
let args = Args::parse(); | |
// Parse the environment or set the default value | |
let environment: String = args.env; | |
// Parse the test suite | |
let test_suite = args.xml; | |
// Parse the browser | |
let browser: String = format!("{:?}", args.browser); | |
// Parse the Selenium WebDriverWait value | |
let wait = args.wait; | |
// Parse the regex pattern for test cases | |
let tests = args.tests; | |
// Perform the desired actions based on the parsed arguments | |
println!("Environment: {}", environment); | |
println!("Test suite: {:?}", test_suite); | |
println!("Browser: {:?}", browser); | |
println!("Selenium WebDriverWait: {} seconds", wait); | |
if tests == ".*" { | |
println!("Regex pattern for test cases: Default (matches all test cases)"); | |
} else { | |
println!("Regex pattern for test cases: {}", tests); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some elisp to work with Rustic, Emacs:
Use it with lsp-booster for better LSP performance:
And for debugging with DAP: