panic on unknown CLI arguments

Currently silently ignores which is not helpful ...
This commit is contained in:
Ken Sedgwick
2025-02-05 10:43:21 -08:00
parent 201cfb2db1
commit 480f98eda4
7 changed files with 113 additions and 7 deletions

View File

@@ -7,6 +7,7 @@ use egui::ThemePreference;
use enostr::RelayPool;
use nostrdb::{Config, Ndb, Transaction};
use std::cell::RefCell;
use std::collections::BTreeSet;
use std::path::Path;
use std::rc::Rc;
use tracing::{error, info};
@@ -29,6 +30,7 @@ pub struct Notedeck {
app: Option<Rc<RefCell<dyn App>>>,
zoom: ZoomHandler,
app_size: AppSizeHandler,
unrecognized_args: BTreeSet<String>,
}
fn margin_top(narrow: bool) -> f32 {
@@ -106,7 +108,9 @@ impl Notedeck {
#[cfg(feature = "profiling")]
setup_profiling();
let parsed_args = Args::parse(args);
// Skip the first argument, which is the program name.
let args_to_parse: Vec<String> = args[1..].to_vec();
let (parsed_args, unrecognized_args) = Args::parse(&args_to_parse);
let data_path = parsed_args
.datapath
@@ -203,6 +207,7 @@ impl Notedeck {
app: None,
zoom,
app_size,
unrecognized_args,
}
}
@@ -236,4 +241,8 @@ impl Notedeck {
pub fn theme(&self) -> ThemePreference {
self.theme.load()
}
pub fn unrecognized_args(&self) -> &BTreeSet<String> {
&self.unrecognized_args
}
}

View File

@@ -1,3 +1,5 @@
use std::collections::BTreeSet;
use enostr::{Keypair, Pubkey, SecretKey};
use tracing::error;
@@ -18,7 +20,9 @@ pub struct Args {
}
impl Args {
pub fn parse(args: &[String]) -> Self {
// parse arguments, return set of unrecognized args
pub fn parse(args: &[String]) -> (Self, BTreeSet<String>) {
let mut unrecognized_args = BTreeSet::new();
let mut res = Args {
relays: vec![],
is_mobile: None,
@@ -112,11 +116,13 @@ impl Args {
res.use_keystore = false;
} else if arg == "--relay-debug" {
res.relay_debug = true;
} else {
unrecognized_args.insert(arg.clone());
}
i += 1;
}
res
(res, unrecognized_args)
}
}

View File

@@ -60,6 +60,19 @@ pub async fn android_main(app: AndroidApp) {
setup_chrome(ctx, &notedeck.args(), notedeck.theme());
let damus = Damus::new(&mut notedeck.app_context(), &app_args);
// ensure we recognized all the arguments
let completely_unrecognized: Vec<String> = notedeck
.unrecognized_args()
.intersection(damus.unrecognized_args())
.cloned()
.collect();
assert!(
completely_unrecognized.is_empty(),
"unrecognized args: {:?}",
completely_unrecognized
);
notedeck.set_app(damus);
Ok(Box::new(notedeck))

View File

@@ -78,6 +78,19 @@ async fn main() {
setup_chrome(ctx, notedeck.args(), notedeck.theme());
let damus = Damus::new(&mut notedeck.app_context(), &args);
// ensure we recognized all the arguments
let completely_unrecognized: Vec<String> = notedeck
.unrecognized_args()
.intersection(&damus.unrecognized_args())
.cloned()
.collect();
assert!(
completely_unrecognized.is_empty(),
"unrecognized args: {:?}",
completely_unrecognized
);
// TODO: move "chrome" frame over Damus app somehow
notedeck.set_app(damus);
@@ -173,9 +186,21 @@ mod tests {
let ctx = egui::Context::default();
let mut notedeck = Notedeck::new(&ctx, &tmpdir, &args);
let unrecognized_args = notedeck.unrecognized_args().clone();
let mut app_ctx = notedeck.app_context();
let app = Damus::new(&mut app_ctx, &args);
// ensure we recognized all the arguments
let completely_unrecognized: Vec<String> = unrecognized_args
.intersection(app.unrecognized_args())
.cloned()
.collect();
assert!(
completely_unrecognized.is_empty(),
"unrecognized args: {:?}",
completely_unrecognized
);
assert_eq!(app.columns(app_ctx.accounts).columns().len(), 2);
let tl1 = app
@@ -200,4 +225,39 @@ mod tests {
rmrf(tmpdir);
}
#[tokio::test]
async fn test_unknown_args() {
let tmpdir = create_tmp_dir();
let npub = "npub1xtscya34g58tk0z605fvr788k263gsu6cy9x0mhnm87echrgufzsevkk5s";
let args: Vec<String> = [
"--testrunner",
"--no-keystore",
"--unknown-arg", // <-- UNKNOWN
"--pub",
npub,
"-c",
"notifications",
"-c",
"contacts",
]
.iter()
.map(|s| s.to_string())
.collect();
let ctx = egui::Context::default();
let mut notedeck = Notedeck::new(&ctx, &tmpdir, &args);
let mut app_ctx = notedeck.app_context();
let app = Damus::new(&mut app_ctx, &args);
// ensure we recognized all the arguments
let completely_unrecognized: Vec<String> = notedeck
.unrecognized_args()
.intersection(app.unrecognized_args())
.cloned()
.collect();
assert_eq!(completely_unrecognized, ["--unknown-arg"]);
rmrf(tmpdir);
}
}

View File

@@ -33,6 +33,11 @@ impl PreviewRunner {
let ctx = &cc.egui_ctx;
let mut notedeck = Notedeck::new(ctx, &base_path, &args);
assert!(
notedeck.unrecognized_args().is_empty(),
"unrecognized args: {:?}",
notedeck.unrecognized_args()
);
setup_chrome(ctx, notedeck.args(), notedeck.theme());
notedeck.set_app(PreviewApp::new(preview));

View File

@@ -22,7 +22,7 @@ use egui_extras::{Size, StripBuilder};
use nostrdb::{Ndb, Transaction};
use std::collections::HashMap;
use std::collections::{BTreeSet, HashMap};
use std::path::Path;
use std::time::Duration;
use tracing::{error, info, trace, warn};
@@ -51,6 +51,8 @@ pub struct Damus {
pub debug: bool,
pub since_optimize: bool,
pub textmode: bool,
pub unrecognized_args: BTreeSet<String>,
}
fn handle_key_events(input: &egui::InputState, columns: &mut Columns) {
@@ -357,7 +359,7 @@ impl Damus {
pub fn new(ctx: &mut AppContext<'_>, args: &[String]) -> Self {
// arg parsing
let parsed_args = ColumnsArgs::parse(
let (parsed_args, unrecognized_args) = ColumnsArgs::parse(
args,
ctx.accounts
.get_selected_account()
@@ -433,6 +435,7 @@ impl Damus {
support,
decks_cache,
debug,
unrecognized_args,
}
}
@@ -475,12 +478,17 @@ impl Damus {
view_state: ViewState::default(),
support,
decks_cache,
unrecognized_args: BTreeSet::default(),
}
}
pub fn subscriptions(&mut self) -> &mut HashMap<String, SubKind> {
&mut self.subscriptions.subs
}
pub fn unrecognized_args(&self) -> &BTreeSet<String> {
&self.unrecognized_args
}
}
/*

View File

@@ -1,3 +1,5 @@
use std::collections::BTreeSet;
use crate::timeline::TimelineKind;
use enostr::{Filter, Pubkey};
use tracing::{debug, error, info};
@@ -9,7 +11,8 @@ pub struct ColumnsArgs {
}
impl ColumnsArgs {
pub fn parse(args: &[String], deck_author: Option<&Pubkey>) -> Self {
pub fn parse(args: &[String], deck_author: Option<&Pubkey>) -> (Self, BTreeSet<String>) {
let mut unrecognized_args = BTreeSet::new();
let mut res = Self {
columns: vec![],
since_optimize: true,
@@ -132,12 +135,14 @@ impl ColumnsArgs {
} else {
error!("failed to parse filter in '{}'", filter_file);
}
} else {
unrecognized_args.insert(arg.clone());
}
i += 1;
}
res
(res, unrecognized_args)
}
}