Skip to content

Commit

Permalink
handle input errors gracefully in examples (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
keepsimple1 authored Feb 22, 2023
1 parent f3d0033 commit 1fbd084
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
11 changes: 8 additions & 3 deletions examples/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ fn main() {
// Create a daemon
let mdns = ServiceDaemon::new().expect("Failed to create daemon");

let mut service_type = std::env::args()
.nth(1)
.expect("it requires a service_type as argument");
let mut service_type = match std::env::args().nth(1) {
Some(arg) => arg,
None => {
println!("ERROR: require a service_type as argument. For example: ");
println!("cargo run --example query _my-service._udp");
return;
}
};

// Browse for a service type.
service_type.push_str(".local.");
Expand Down
22 changes: 16 additions & 6 deletions examples/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@ use mdns_sd::{ServiceDaemon, ServiceInfo};
fn main() {
// Create a new mDNS daemon.
let mdns = ServiceDaemon::new().expect("Could not create service daemon");
let service_type = std::env::args()
.nth(1)
.expect("require a service_type as the 1st argument");
let instance_name = std::env::args()
.nth(2)
.expect("require a instance_name as the 2nd argument");
let service_type = match std::env::args().nth(1) {
Some(arg) => arg,
None => {
println!("ERROR: register requires a service_type as the 1st argument. For example:");
println!("cargo run --example register _my-hello._udp.local. test1");
return;
}
};
let instance_name = match std::env::args().nth(2) {
Some(arg) => arg,
None => {
println!("ERROR: require a instance_name as the 2nd argument. For example: ");
println!("cargo run --example register _my-hello._udp.local. test1");
return;
}
};

// With `enable_addr_auto()`, we can give empty addrs and let the lib find them.
// If the caller knows specific addrs to use, then assign the addrs here.
Expand Down

0 comments on commit 1fbd084

Please sign in to comment.