Repository: versotile-org/verso
File: src/config.rs (Rust)

Code before:

    let mut opts = getopts::Options::new();
    opts.optopt("", "url", "URL to load on start", "URL");
    opts.optopt(
        "",
        "ipc-channel",
        "IPC channel name to communicate and control verso",
        "",
    );
    opts.optflag("", "no-panel", "Launch Verso without control panel");

    opts.optopt(
        "w",
        "width",
        "Initial window's width in physical unit, the height command line arg much also be set",
        "",
    );
    opts.optopt(
        "h",
        "height",
        "Initial window's height in physical unit, the width command line arg much also be set",
        "",
    );

    let matches: getopts::Matches = opts.parse(&args[1..])?;
    let url = matches
        .opt_str("url")
        .and_then(|url| match url::Url::parse(&url) {
            Ok(url_parsed) => Some(url_parsed),
            Err(e) => {
                if e == url::ParseError::RelativeUrlWithoutBase {
                    if let Ok(url_parsed) = url::Url::parse(&format!("https://{url}")) {
                        return Some(url_parsed);
                    }
                }
                log::error!("Invalid initial url: {url}");
                None
            }
        });
    let ipc_channel = matches.opt_str("ipc-channel");
    let no_panel = matches.opt_present("no-panel");

    let width = matches.opt_get::<u32>("width").unwrap_or_else(|e| {
        log::error!("Failed to parse width command line argument: {e}");
        None
    });
    let height = matches.opt_get::<u32>("height").unwrap_or_else(|e| {
        log::error!("Failed to parse height command line argument: {e}");
        None
    });


Review comment (bug):
Single character like `"x"` and `"y"` as long name seemed invalid.

```sh
$ cargo r
   Compiling versoview v0.0.1 (D:\Codes\verso)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 11.67s
     Running `target\debug\versoview.exe`
thread 'main' panicked at D:\packages\cargo\registry\src\index.crates.io-6f17d22bba15001f\getopts-0.2.21\src\lib.rs:604:5:
the long_name (second argument) should be longer than a single character, or an empty string for none
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\versoview.exe` (exit code: 101)
```

Code after:

    let mut opts = getopts::Options::new();
    opts.optopt("", "url", "URL to load on start", "URL");
    opts.optopt(
        "",
        "ipc-channel",
        "IPC channel name to communicate and control verso",
        "",
    );
    opts.optflag("", "no-panel", "Launch Verso without control panel");

    opts.optopt(
        "w",
        "width",
        "Initial window's width in physical unit, the height command line arg must also be set",
        "",
    );
    opts.optopt(
        "h",
        "height",
        "Initial window's height in physical unit, the width command line arg must also be set",
        "",
    );
    opts.optopt(
        "",
        "x",
        "Initial window's top left x position in physical unit, the y command line arg must also be set. Wayland isn't supported.",
        "",
    );
    opts.optopt(
        "",
        "y",
        "Initial window's top left y position in physical unit, the x command line arg must also be set. Wayland isn't supported.",
        "",
    );

    let matches: getopts::Matches = opts.parse(&args[1..])?;
    let url = matches
        .opt_str("url")
        .and_then(|url| match url::Url::parse(&url) {
            Ok(url_parsed) => Some(url_parsed),
            Err(e) => {
                if e == url::ParseError::RelativeUrlWithoutBase {
                    if let Ok(url_parsed) = url::Url::parse(&format!("https://{url}")) {
                        return Some(url_parsed);
                    }
                }
                log::error!("Invalid initial url: {url}");
                None
            }
        });