wala-rust

Content-adressed HTTP file server
Info | Log | Files | Refs | README | LICENSE

arg.rs (2058B)


      1 use std::path::PathBuf;
      2 use clap::{
      3     App, 
      4     Arg,
      5     ArgMatches,
      6     SubCommand,
      7 };
      8 
      9 pub struct Settings {
     10     pub host: String,
     11     pub port: u16,
     12     pub dir: PathBuf,
     13 }
     14 
     15 const BIND_HOST: &str  = "0.0.0.0";
     16 const BIND_PORT: u16 = 8000;
     17 const DATA_DIR: &str = ".";
     18 
     19 impl Settings {
     20 
     21     pub fn new() -> Settings {
     22         Settings {
     23             host: BIND_HOST.to_string(),
     24             port: BIND_PORT,
     25             dir: PathBuf::from(DATA_DIR),
     26         }
     27     }
     28 
     29     fn bind_from_args(&mut self, arg: &ArgMatches) {
     30         match arg.value_of("host") {
     31             Some(v) => {
     32                 self.host = v.to_string();
     33             },
     34             _ => {},
     35         };
     36 
     37         match arg.value_of("port") {
     38             Some(v) => {
     39                 let port = u16::from_str_radix(&v, 10);
     40                 self.port = port.unwrap();
     41             },
     42             _ => {},
     43         };
     44 
     45         match arg.value_of("datadir") {
     46             Some(v) => {
     47                 self.dir = PathBuf::from(v);
     48             },
     49             _ => {},
     50 
     51         };
     52     }
     53 
     54     pub fn from_args() -> Settings {
     55         let mut o = App::new("wala");
     56         o = o.version(env!("CARGO_PKG_VERSION"));
     57         o = o.author("Louis Holbrook <dev@holbrook.no>");
     58         o = o.arg(
     59             Arg::with_name("host")
     60                 .long("host")
     61                 .short("h")
     62                 .value_name("Host or ip to bind server to.")
     63                 .takes_value(true)
     64                 );
     65         o = o.arg(
     66             Arg::with_name("port")
     67                 .long("port")
     68                 .short("p")
     69                 .value_name("Port to bind server to")
     70                 .takes_value(true)
     71                 );
     72         o = o.arg(
     73             Arg::with_name("datadir")
     74                 .long("data-dir")
     75                 .short("d")
     76                 .value_name("Data directory")
     77                 .takes_value(true)
     78                 );
     79 
     80         let arg_matches = o.get_matches();
     81         let mut settings = Settings::new();
     82         settings.bind_from_args(&arg_matches);
     83         settings
     84     }
     85 }