Parse cli arguments with clap
This commit is contained in:
parent
1f33d09ec1
commit
49de8dd465
173
Cargo.lock
generated
173
Cargo.lock
generated
@ -2,6 +2,54 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstream"
|
||||||
|
version = "0.6.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44"
|
||||||
|
dependencies = [
|
||||||
|
"anstyle",
|
||||||
|
"anstyle-parse",
|
||||||
|
"anstyle-query",
|
||||||
|
"anstyle-wincon",
|
||||||
|
"colorchoice",
|
||||||
|
"utf8parse",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle"
|
||||||
|
version = "1.0.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-parse"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140"
|
||||||
|
dependencies = [
|
||||||
|
"utf8parse",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-query"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
|
||||||
|
dependencies = [
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-wincon"
|
||||||
|
version = "3.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628"
|
||||||
|
dependencies = [
|
||||||
|
"anstyle",
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anyhow"
|
name = "anyhow"
|
||||||
version = "1.0.75"
|
version = "1.0.75"
|
||||||
@ -54,6 +102,52 @@ dependencies = [
|
|||||||
"target-lexicon",
|
"target-lexicon",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap"
|
||||||
|
version = "4.4.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d04704f56c2cde07f43e8e2c154b43f216dc5c92fc98ada720177362f953b956"
|
||||||
|
dependencies = [
|
||||||
|
"clap_builder",
|
||||||
|
"clap_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_builder"
|
||||||
|
version = "4.4.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0e231faeaca65ebd1ea3c737966bf858971cd38c3849107aa3ea7de90a804e45"
|
||||||
|
dependencies = [
|
||||||
|
"anstream",
|
||||||
|
"anstyle",
|
||||||
|
"clap_lex",
|
||||||
|
"strsim",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_derive"
|
||||||
|
version = "4.4.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873"
|
||||||
|
dependencies = [
|
||||||
|
"heck",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.37",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_lex"
|
||||||
|
version = "0.5.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "colorchoice"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "equivalent"
|
name = "equivalent"
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
@ -426,6 +520,7 @@ name = "music-reader"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cairo-rs",
|
"cairo-rs",
|
||||||
|
"clap",
|
||||||
"gio",
|
"gio",
|
||||||
"glib",
|
"glib",
|
||||||
"glib-macros",
|
"glib-macros",
|
||||||
@ -612,6 +707,12 @@ version = "1.11.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a"
|
checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strsim"
|
||||||
|
version = "0.10.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "1.0.109"
|
version = "1.0.109"
|
||||||
@ -713,6 +814,12 @@ version = "1.0.12"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "utf8parse"
|
||||||
|
version = "0.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "version-compare"
|
name = "version-compare"
|
||||||
version = "0.1.1"
|
version = "0.1.1"
|
||||||
@ -747,6 +854,72 @@ version = "0.4.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-sys"
|
||||||
|
version = "0.48.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
|
||||||
|
dependencies = [
|
||||||
|
"windows-targets",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-targets"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
||||||
|
dependencies = [
|
||||||
|
"windows_aarch64_gnullvm",
|
||||||
|
"windows_aarch64_msvc",
|
||||||
|
"windows_i686_gnu",
|
||||||
|
"windows_i686_msvc",
|
||||||
|
"windows_x86_64_gnu",
|
||||||
|
"windows_x86_64_gnullvm",
|
||||||
|
"windows_x86_64_msvc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_gnullvm"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_msvc"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_gnu"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_msvc"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnu"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnullvm"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_msvc"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winnow"
|
name = "winnow"
|
||||||
version = "0.5.15"
|
version = "0.5.15"
|
||||||
|
@ -10,3 +10,4 @@ glib-macros = "0.15.11"
|
|||||||
gio = "0.15.12"
|
gio = "0.15.12"
|
||||||
glib = "0.15.12"
|
glib = "0.15.12"
|
||||||
gtk4 = "0.4.8"
|
gtk4 = "0.4.8"
|
||||||
|
clap = { version = "4.4.6", features = ["derive"] }
|
||||||
|
53
src/main.rs
53
src/main.rs
@ -1,4 +1,5 @@
|
|||||||
use cairo::Context;
|
use cairo::Context;
|
||||||
|
use clap::Parser;
|
||||||
use gio::ApplicationFlags;
|
use gio::ApplicationFlags;
|
||||||
use glib::clone;
|
use glib::clone;
|
||||||
use gtk4::prelude::*;
|
use gtk4::prelude::*;
|
||||||
@ -6,30 +7,34 @@ use gtk4::{Application, ApplicationWindow, DrawingArea, Label};
|
|||||||
use poppler::PopplerDocument;
|
use poppler::PopplerDocument;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::ffi::OsString;
|
use std::path::PathBuf;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
const APP_ID: &str = "de.frajul.music-reader";
|
const APP_ID: &str = "de.frajul.music-reader";
|
||||||
|
|
||||||
fn main() {
|
#[derive(Parser)]
|
||||||
let app = Application::builder()
|
#[command(author, version, about)]
|
||||||
.application_id(APP_ID)
|
struct Cli {
|
||||||
.flags(ApplicationFlags::HANDLES_OPEN | ApplicationFlags::HANDLES_COMMAND_LINE)
|
file: Option<PathBuf>,
|
||||||
.build();
|
|
||||||
|
|
||||||
app.connect_command_line(move |app, cmdline| {
|
|
||||||
build_ui(&app, cmdline.arguments());
|
|
||||||
0
|
|
||||||
});
|
|
||||||
app.run_with_args(&env::args().collect::<Vec<_>>());
|
|
||||||
app.run();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_ui(app: &Application, arguments: Vec<OsString>) {
|
fn main() {
|
||||||
let filename = arguments
|
let cli = Cli::parse();
|
||||||
.get(1)
|
println!("Parse args");
|
||||||
.and_then(|arg| arg.clone().into_string().ok());
|
let app = Application::builder()
|
||||||
|
.application_id(APP_ID)
|
||||||
|
.flags(gio::ApplicationFlags::FLAGS_NONE)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
app.connect_activate(move |app| {
|
||||||
|
build_ui(&app, &cli);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.run_with_args(&[] as &[&str]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_ui(app: &Application, cli: &Cli) {
|
||||||
|
println!("building ui");
|
||||||
let open_file_button = gtk4::Button::from_icon_name("document-open");
|
let open_file_button = gtk4::Button::from_icon_name("document-open");
|
||||||
|
|
||||||
let app_wrapper = gtk4::Box::builder()
|
let app_wrapper = gtk4::Box::builder()
|
||||||
@ -59,7 +64,8 @@ fn build_ui(app: &Application, arguments: Vec<OsString>) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let load_doc = move |filename: String| {
|
let load_doc = move |file: &PathBuf| {
|
||||||
|
println!("Loading file...");
|
||||||
let drawing_area = DrawingArea::builder()
|
let drawing_area = DrawingArea::builder()
|
||||||
.width_request(100)
|
.width_request(100)
|
||||||
.height_request(100)
|
.height_request(100)
|
||||||
@ -81,7 +87,7 @@ fn build_ui(app: &Application, arguments: Vec<OsString>) {
|
|||||||
}
|
}
|
||||||
bottom_bar.append(&page_indicator);
|
bottom_bar.append(&page_indicator);
|
||||||
|
|
||||||
let doc = PopplerDocument::new_from_file(filename, "").unwrap();
|
let doc = PopplerDocument::new_from_file(file, "").unwrap();
|
||||||
|
|
||||||
let num_pages = doc.get_n_pages();
|
let num_pages = doc.get_n_pages();
|
||||||
let num_pages_ref = Rc::new(RefCell::new(num_pages));
|
let num_pages_ref = Rc::new(RefCell::new(num_pages));
|
||||||
@ -161,8 +167,9 @@ fn build_ui(app: &Application, arguments: Vec<OsString>) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
if filename.is_some() {
|
match cli.file.as_ref() {
|
||||||
load_doc(filename.unwrap());
|
Some(file) => load_doc(file),
|
||||||
|
None => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
open_file_button.connect_clicked(clone!(@weak window, @strong load_doc => move |_button| {
|
open_file_button.connect_clicked(clone!(@weak window, @strong load_doc => move |_button| {
|
||||||
@ -176,8 +183,8 @@ fn build_ui(app: &Application, arguments: Vec<OsString>) {
|
|||||||
filechooser.set_transient_for(Some(&window));
|
filechooser.set_transient_for(Some(&window));
|
||||||
filechooser.connect_response(clone!(@strong load_doc => move |d, response| {
|
filechooser.connect_response(clone!(@strong load_doc => move |d, response| {
|
||||||
if response == gtk4::ResponseType::Accept {
|
if response == gtk4::ResponseType::Accept {
|
||||||
let path = d.file().unwrap().path().unwrap().into_os_string().into_string().unwrap();
|
let path = d.file().unwrap().path().unwrap();
|
||||||
load_doc(path);
|
load_doc(&path);
|
||||||
}
|
}
|
||||||
d.destroy();
|
d.destroy();
|
||||||
}));
|
}));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user