Moved parameter reader into own file

This commit is contained in:
2019-06-09 00:10:33 +02:00
parent 126d0da7c7
commit e5dc929135
3 changed files with 80 additions and 67 deletions

View File

@@ -1,7 +1,72 @@
//! File: configuration.rs //! File: settings.rs
//! Description: Definition file for the configuration namespace //! Description: Defines a stuct that holds all of the configuration settings passed to the program
//! Author:Jali <jali@orca-central.de> //! by line.
//! Created On: Tue 04 Jun 2019 10:06:53 pm CEST //! Author: Jali <jali@orcacentral.de>
//! Last Modified: Tue 04 Jun 2019 10:06:53 pm CEST //! Created On: Tue 04 Jun 2019 10:30:46 pm CEST
//! Last Modified: TIMESTAMP
pub mod configuration; use clap::ArgMatches;
use std::str::FromStr;
/**
* Stores the configuration needed for the server.
*/
pub struct Settings {
pub foreground: bool,
pub listen: String,
pub plugins: Vec<String>,
pub port: i32,
}
impl Settings {
/**
* # Description
*
* Creates a new instance of a Settings object either from
* the command line, or the default configuration.
*
* # Returns
*
* A new ```Settings``` object
*
* # Exanples
*
* ```rust
* let settings = Settings::create();
* ```
*/
pub fn create() -> Settings {
let matches = Settings::parse_command_line();
Settings {
foreground: matches.is_present("FOREGROUND"),
listen: matches.value_of("LISTEN").unwrap_or("default").to_owned(),
plugins: matches.value_of("PLUGIN").unwrap_or("filesystem").split(",").map(|s| s.to_string()).collect(),
port: FromStr::from_str(matches.value_of("PORT").unwrap_or("70")).unwrap()
}
}
/**
* # Description
*
* Parses the command line and returns all matches that fit the known
* options
*
* # returns
* A collection of matches.
*/
fn parse_command_line<'a>() -> ArgMatches<'a> {
// Read the configuration from the command line
clap_app!(myapp =>
(version: "0.1")
(author: "jali@orca-central.de")
(about: "A simple modular gopher server, that pushes converted web-content to a client")
(@arg FOREGROUND: -f --foreground "Keeps the service in the foreground")
(@arg LISTEN: -l --listen +takes_value "Specifies the interface to listn on. Is either a hostname or an address-pattern.")
(@arg PLUGIN: -u --plugins +takes_value +required "A comma separated list of plugins to load.")
(@arg PORT: -p --port +takes_value "Specifies the port the server listens on. Default is 70")
)
.get_matches()
}
}

View File

@@ -1,15 +0,0 @@
//! File: settings.rs
//! Description: Defines a stuct that holds all of the configuration settings passed to the program
//! by command line.
//! Author: Jali <jali@orca-central.de>
//! Created On: Tue 04 Jun 2019 10:30:46 pm CEST
//! Last Modified: TIMESTAMP
/**
* Defines a data stucture to to store the configuration in.
*/
pub struct settings {
Foreground: bool,
Plugins: Vec<String>,
Port: i32,
}

View File

@@ -1,57 +1,20 @@
#[macro_use]
extern crate clap; extern crate clap;
mod configuration;
use clap::{App, Arg, ArgMatches}; use crate::configuration::Settings;
/** /**
* Initiates an instance of the gopherbridge server and * Initiates an instance of the gopherbridge server and
* starts listening for connections on the given port. * starts listening for connections on the given port.
*/ */
fn main() { fn main() {
let matches = parse_command_line(); let settings = Settings::create();
let pluginlist = matches.value_of("PLUGIN");
let port = matches.value_of("PORT");
match port { println!("Program listens on {}", settings.listen);
None => println!("Default port 70 used"), println!("Loading plugins {:?}", settings.plugins);
Some(p) => println!("Using port {}", p), println!("Listening on port {}", settings.port);
} if settings.foreground {
println!("Server runs in the foreground");
match pluginlist {
None => println!("No plugins given"),
Some(p) => println!("Loading the following plugins: {}", p),
} }
} }
/**
* Defines the command line parameters passed to the server and creates the
* server lines
*/
fn parse_command_line<'a>() -> ArgMatches<'a> {
return App::new("gopherbridge")
.version("0.1")
.author("jali@orca-central.de")
.about("a simple modular gopher server, that pushes converted web-content to a client")
.arg(
Arg::with_name("FORGROUND")
.short("f")
.long("foreground")
.help("Keeps the service in the foreground")
.takes_value(false),
)
.arg(
Arg::with_name("PLUGIN")
.short("l")
.long("plugins")
.value_name("PLUGINLIST")
.takes_value(true)
.index(1),
)
.arg(
Arg::with_name("PORT")
.short("p")
.long("port")
.help("Specifies the port the server listens on. Default is 70")
.takes_value(true),
)
.get_matches();
}