I'd like to parse ipv4 addresses given as command line argument values.
I have got two arguments accepting ipv4 address.
If I specify single such option all is fine.
If I specify both, I 'm getting error like this:
 
thread 'main' (624061) panicked at /home/peto/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/clap-3.2.25/src/parser/matches/arg_matches.rs:1879:13:
Must use `Arg::allow_invalid_utf8` with `_os` lookups at `[hash: A8F400C40154F09]`This is simplified version of my code showcasing the issue:
```
use std::net::{IpAddr, Ipv4Addr};
use clap::{App, AppSettings, Arg, value_parser};
#[tokio::main]
async fn main() -> Result<(), Error> {
 let mut app = App::new("Server APP")
 .about("My super cool app")
 .setting(AppSettings::DeriveDisplayOrder)
 .setting(AppSettings::SubcommandsNegateReqs)
 .arg(
 Arg::with_name("socket")
 .required(true)
 .takes_value(true)
 .long("socket")
 .help("Unix socket path"),
 )
 .arg(
 Arg::with_name("relayaddress")
 .required(false)
 .takes_value(true)
 .long("relay-address")
 .value_parser(value_parser!(Ipv4Addr))
 .help("External relay ipv4 address used together with --listen-address to run behind a nat"),
 )
 .arg(
 Arg::with_name("listenaddress")
 .required(false)
 .takes_value(true)
 .long("listen-address")
 .value_parser(value_parser!(Ipv4Addr))
 .help("Local listen ipv4 address used together with --relay-address to run behind a nat"),
 );
 let matches = app.clone().get_matches();
 if matches.is_present("relayaddress") & matches.is_present("listenaddress") {
 let external_ip = IpAddr::V4(matches.get_one::<Ipv4Addr>("relayaddress").expect("Invalid address"));
 let local_ip = IpAddr::V4(matches.get_one::<Ipv4Addr>("listenaddress").expect("Invalid address"));
 println!("Listening on local IP: {local_ip}");
 println!("Relaying through external IP: {external_ip}");
 }}
 ```
reshared this
Federico Mena Quintero
in reply to Peter Vágner • • •Sensitive content
is this clap 3? Is there a reason not to use the latest version?
I had to make a few changes to make your code compile with clap 3.2.25, but passing both options works fine and it prints them out as expected.
Peter Vágner
in reply to Federico Mena Quintero • •--socket argument is required, other two arguments are supposed to be used together and this condition is tested at runtime.
So if I specify all three command line arguments, I am always getting that error.
I have attempted using os::str and casting but the issue remains. I am simply compiling the app with cargo build --release.
Have you been just adding stuff I may have overlooked when trying to simplify for posting or did you actually changed something please?
Peter Vágner
in reply to Federico Mena Quintero • •I have changed it to use std::path::PathBuf and it's working fine for me now.
Huge thanks for friendly hint and looking at my code.