Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Added host, port (with default value) variables using cli program mode #3

Merged
merged 2 commits into from Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 62 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pub_weight/Cargo.toml
Expand Up @@ -11,6 +11,7 @@ uuid = { version = "0.7.4", features = ["v4"] }
futures = "0.1.28"
bincode = "1.1.4"
failure = "0.1.5"
clap = "2.33.0"

[dependencies.pirust_common]
path = "../common"
25 changes: 22 additions & 3 deletions pub_weight/src/main.rs
Expand Up @@ -4,6 +4,7 @@ use futures::stream::Stream;
use mqtt::{control::variable_header::ConnectReturnCode, packet::*, Decodable, Encodable};
use tokio::{runtime, timer::Interval};
use uuid::Uuid;
use clap::{Arg, App as CliApp};

use std::{io::Write, net::TcpStream, time::Duration};

Expand All @@ -14,10 +15,28 @@ fn generate_client_id() -> String {
}

fn main() -> Result<(), Error> {
let server_addr = "192.168.1.44:1883";
let matches = CliApp::new("Pirust")
.version("0.1.0")
.arg(Arg::with_name("host")
.required(true)
.takes_value(true)
.index(1)
.help("Host name or IP of the MQTT Server (without port)")
)
.arg(Arg::with_name("port")
.required(false)
.takes_value(true)
.index(2)
.help("Port of the MQTT Server")
)
.get_matches();

println!("Connecting to {:?} ... ", server_addr);
let mut stream = TcpStream::connect(server_addr).unwrap();
let server_addr = matches.value_of("host").unwrap().to_string();
let server_port = matches.value_of("port").unwrap_or("1883").to_string();
let server_host = [server_addr, ":".to_string(), server_port].concat();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    let server_addr = matches.value_of("host").unwrap();
    let server_port = matches.value_of("port").unwrap_or("1883");
    let server_host = format!("{}:{}", server_addr, server_port);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multi-line suggestions don't work :(


println!("Connecting to {:?} ... ", server_host);
let mut stream = TcpStream::connect(server_host).unwrap();
println!("Connected!");

let client_id = generate_client_id();
Expand Down