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

@ -1,72 +1,66 @@
name: "mcstat" 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: - protocol-version:
help: "the port of the server" long: "protocol"
long: "port" help: "the protocol version to use"
short: p default_value: "751"
default_value: "25565" takes_value: true
takes_value: true - timeout:
- protocol-version: long: "timeout"
long: "protocol" short: t
help: "the protocol version to use" help: "the time before the server ping times out in milliseconds"
default_value: "751" takes_value: true
takes_value: true default_value: "500"
- timeout: - raw:
long: "timeout" short: r
short: t help: "if supplied, the raw json response from the server will be printed"
help: "the time before the server ping times out in milliseconds" - mods:
takes_value: true short: m
default_value: "500" help: "if supplied, a mod list will be printed"
- raw: - modversions:
short: r short: v
help: "if supplied, the raw json response from the server will be printed" help: "if supplied, mods will also have their version info printed"
- mods: requires: "mods"
short: m - channels:
help: "if supplied, a mod list will be printed" long: "channels"
- modversions: help: "displays forge mod channels if the server sends them"
short: v
help: "if supplied, mods will also have their version info printed"
requires: "mods"
- channels:
long: "channels"
help: "displays forge mod channels if the server sends them"
# IMAGE ARGS # IMAGE ARGS
# TODO due to a bug in clap, the image argument is always required because size has a default value # TODO due to a bug in clap, the image argument is always required because size has a default value
- image: - image:
short: "i" short: "i"
help: "if the server's favicon should be printed as ASCII art" help: "if the server's favicon should be printed as ASCII art"
required: false required: false
- color: - color:
short: "c" short: "c"
help: "if the favicon image should be printed with ANSI color formatting or monochrome" help: "if the favicon image should be printed with ANSI color formatting or monochrome"
- size: - size:
short: "s" short: "s"
help: "the size of the image" help: "the size of the image"
takes_value: true takes_value: true
default_value: "16" default_value: "16"
- deep: - deep:
short: "d" short: "d"
help: "if provided the ascii image will have more different characters" help: "if provided the ascii image will have more different characters"
- invert: - invert:
short: "n" short: "n"
help: "inverts the ascii image thickness" help: "inverts the ascii image thickness"
groups: groups:
- img-flags: - img-flags:
# TODO uncomment once bug is fixed # TODO uncomment once bug is fixed
# requires: # requires:
# - image # - image
multiple: true multiple: true
required: false required: false
args: args:
- color - color
- size - size
- deep - deep
- invert - invert

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,36 +19,25 @@ 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)? .splitn(2, ':');
.to_owned(),
) 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| { .with_protocol_version(
// the port must be above 0 matches
if p > 0 { .value_of("protocol-version")
Ok(p) .context(ARGUMENT_FAIL_MESSAGE)?
} else { .parse()
// this error will be overriden anyways .context("invalid protocol version")?,
Err(anyhow!("")) );
}
})
.context("invalid port")?,
)
.with_protocol_version(
matches
.value_of("protocol-version")
.context(ARGUMENT_FAIL_MESSAGE)?
.parse()
.context("invalid protocol version")?,
);
// create timeout for server connection // create timeout for server connection
let mut timeout = time::delay_for(Duration::from_millis( let mut timeout = time::delay_for(Duration::from_millis(