Parse multiple URLs via command line

Closes: #44, #47

I used pico-args over clap because the former only adds about 1KB of
binary bloat whereas clap adds almost 1MB.
This commit is contained in:
Josh Megnauth 2025-02-10 23:13:10 -05:00 committed by Jeremy Soller
parent f441d24760
commit 3fd8641df2
4 changed files with 76 additions and 19 deletions

71
src/argparse.rs Normal file
View file

@ -0,0 +1,71 @@
// Copyright 2024 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
use std::{fs, io};
use log::warn;
use url::Url;
#[derive(Debug, Default)]
pub struct Arguments {
/// URLs to play with associated metadata
pub urls: Vec<Url>,
}
impl Arguments {
pub fn from_args() -> Result<Self, pico_args::Error> {
let mut parser = pico_args::Arguments::from_env();
// Freestanding arguments are treated as URLs
let urls: Vec<Url> = std::iter::from_fn(|| {
parser
.opt_free_from_fn(|arg| Source::try_from(arg))
.ok()
.flatten()
})
.map(|source| source.0)
.collect();
let remainder = parser.finish();
for arg in remainder {
warn!("Unused argument: {arg:?}");
}
Ok(Arguments { urls })
}
}
// #[derive(Debug)]
// pub enum Source {
// File(Url),
// Directory(Url),
// // TODO: GStreamer handles streaming out of the box
// Other(Url),
// }
struct Source(Url);
impl TryFrom<&str> for Source {
type Error = io::Error;
fn try_from(arg: &str) -> Result<Self, Self::Error> {
match url::Url::parse(arg) {
Ok(url) => Ok(Source(url)),
Err(_) => match fs::canonicalize(arg) {
Ok(path) => {
match Url::from_file_path(&path).or_else(|_| Url::from_directory_path(&path)) {
Ok(url) => Ok(Source(url)),
Err(()) => {
warn!("failed to parse path {:?}", path);
Err(io::Error::other("Invalid URL and path"))
}
}
}
Err(err) => {
warn!("failed to parse argument {:?}: {}", arg, err);
Err(err)
}
},
}
}
}

View file

@ -37,6 +37,7 @@ use crate::{
project::ProjectNode,
};
mod argparse;
mod config;
mod key_bind;
mod localize;
@ -110,25 +111,8 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
settings = settings.theme(config.app_theme.theme());
settings = settings.size_limits(Limits::NONE.min_width(360.0).min_height(180.0));
let url_opt = match std::env::args().nth(1) {
Some(arg) => match url::Url::parse(&arg) {
Ok(url) => Some(url),
Err(_) => match fs::canonicalize(&arg) {
Ok(path) => match url::Url::from_file_path(&path).or_else(|_| url::Url::from_directory_path(&path)) {
Ok(url) => Some(url),
Err(()) => {
log::warn!("failed to parse path {:?}", path);
None
}
},
Err(err) => {
log::warn!("failed to parse argument {:?}: {}", arg, err);
None
}
},
},
None => None,
};
let args = argparse::Arguments::from_args().unwrap_or_default();
let url_opt = args.urls.into_iter().next();
let flags = Flags {
config_handler,