remove port argument

port will now be parsed from the ip argument
This commit is contained in:
LordMZTE 2020-10-29 15:25:06 +01:00
parent d0f9ed1afe
commit a934e610ec
2 changed files with 82 additions and 99 deletions

View file

@ -2,16 +2,10 @@ name: "mcstat"
about: "queries information about a minecraft server" about: "queries information about a minecraft server"
args: args:
- ip: - ip:
help: "the ip of the server to ping" help: "the ip of the server to ping. you may also specify the port, if it is not specified or invalid it will default to 25565"
takes_value: true takes_value: true
index: 1 index: 1
required: true required: true
- port:
help: "the port of the server"
long: "port"
short: p
default_value: "25565"
takes_value: true
- protocol-version: - protocol-version:
long: "protocol" long: "protocol"
help: "the protocol version to use" help: "the protocol version to use"

View file

@ -2,13 +2,13 @@ use std::io::Cursor;
use time::Duration; use time::Duration;
use tokio::time; use tokio::time;
use anyhow::{Context, Result, anyhow}; use anyhow::{anyhow, Context, Result};
use asciify::AsciiBuilder; use asciify::AsciiBuilder;
use async_minecraft_ping::{ConnectionConfig, ServerDescription, StatusResponse}; use async_minecraft_ping::{ConnectionConfig, ServerDescription, StatusResponse};
use clap::{App, load_yaml}; use clap::{load_yaml, App};
use image::ImageFormat; use image::ImageFormat;
use itertools::Itertools; use itertools::Itertools;
use mcstat::{AsciiConfig, get_table, none_if_empty, print_table, remove_formatting}; use mcstat::{get_table, none_if_empty, print_table, remove_formatting, AsciiConfig};
use termcolor::{Buffer, BufferWriter, ColorChoice, WriteColor}; use termcolor::{Buffer, BufferWriter, ColorChoice, WriteColor};
/// this message is used if getting a value from the arguments fails /// this message is used if getting a value from the arguments fails
@ -19,28 +19,17 @@ async fn main() -> Result<()> {
let matches = App::from_yaml(yaml).get_matches(); let matches = App::from_yaml(yaml).get_matches();
// region Network // region Network
let config = ConnectionConfig::build( let mut ip = matches
matches
.value_of("ip") .value_of("ip")
.context(ARGUMENT_FAIL_MESSAGE)? .context(ARGUMENT_FAIL_MESSAGE)?
.to_owned(), .splitn(2, ':');
)
let config = ConnectionConfig::build(ip.next().context("invalid ip")?.to_owned())
.with_port( .with_port(
matches ip.next()
.value_of("port") .map_or(Err(()), |p| p.parse::<u16>().map_err(|_| ()))
.context(ARGUMENT_FAIL_MESSAGE)? .and_then(|p| if p > 0 { Ok(p) } else { Err(()) })
.parse() .unwrap_or(25565),
.context("invalid port")
.and_then(|p| {
// the port must be above 0
if p > 0 {
Ok(p)
} else {
// this error will be overriden anyways
Err(anyhow!(""))
}
})
.context("invalid port")?,
) )
.with_protocol_version( .with_protocol_version(
matches matches