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
//! Description: Definition file for the configuration namespace
//! Author:Jali <jali@orca-central.de>
//! Created On: Tue 04 Jun 2019 10:06:53 pm CEST
//! Last Modified: Tue 04 Jun 2019 10:06:53 pm CEST
//! File: settings.rs
//! Description: Defines a stuct that holds all of the configuration settings passed to the program
//! by line.
//! Author: Jali <jali@orcacentral.de>
//! 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()
}
}