瀏覽代碼

check if argument of initial-value is a number

fossedihelm 7 年之前
父節點
當前提交
1dc99e3a15
共有 2 個文件被更改,包括 10 次插入6 次删除
  1. 3 4
      README.md
  2. 7 2
      src/main.rs

+ 3 - 4
README.md

@@ -15,7 +15,7 @@ README.
 ## Building
 Rust 1.17.0 or later is required to build librespot.
 
-**If you are building librespot on macOS, the homebrew provided rust may fail due to the way in which homebrew installs rust. In this case, uninstall the homebrew version of rust and use [rustup](https://www.rustup.rs/), and librespot should then build.** 
+**If you are building librespot on macOS, the homebrew provided rust may fail due to the way in which homebrew installs rust. In this case, uninstall the homebrew version of rust and use [rustup](https://www.rustup.rs/), and librespot should then build.**
 
 It also requires a C, with portaudio.
 
@@ -43,7 +43,7 @@ cargo build --release
 A sample program implementing a headless Spotify Connect receiver is provided.
 Once you've built *librespot*, run it using :
 ```shell
-target/release/librespot --username USERNAME --cache CACHEDIR --name DEVICENAME
+target/release/librespot --username USERNAME --cache CACHEDIR --name DEVICENAME [--initial-volume 20]
 ```
 
 ## Discovery mode
@@ -63,7 +63,7 @@ target/release/librespot [...] --backend portaudio
 
 The following backends are currently available :
 - ALSA
-- PortAudio 
+- PortAudio
 - PulseAudio
 
 ## Cross-compiling
@@ -108,4 +108,3 @@ https://gitter.im/sashahilton00/spotify-connect-resources
 
 ## License
 Everything in this repository is licensed under the MIT license.
-

+ 7 - 2
src/main.rs

@@ -101,7 +101,7 @@ fn setup(args: &[String]) -> Setup {
         .optopt("", "backend", "Audio backend to use. Use '?' to list options", "BACKEND")
         .optopt("", "device", "Audio device to use. Use '?' to list options", "DEVICE")
         .optopt("", "mixer", "Mixer to use", "MIXER")
-        .optopt("", "initial-volume", "Initial volume in %, once connected", "VOLUME");
+        .optopt("", "initial-volume", "Initial volume in %, once connected (must be from 0 to 100)", "VOLUME");
 
     let matches = match opts.parse(&args[1..]) {
         Ok(m) => m,
@@ -136,7 +136,12 @@ fn setup(args: &[String]) -> Setup {
         .expect("Invalid mixer");
     let initial_volume;
     if matches.opt_present("initial-volume"){
-        initial_volume = matches.opt_str("initial-volume").unwrap().parse::<i32>().unwrap()* 0xFFFF as i32 / 100 ;
+        if matches.opt_str("initial-volume").unwrap().parse::<i32>().is_ok(){
+            initial_volume = matches.opt_str("initial-volume").unwrap().parse::<i32>().unwrap()* 0xFFFF as i32 / 100 ;
+            }
+        else {
+            initial_volume = 0x8000 as i32;
+            }
         }
     else{
         initial_volume = 0x8000 as i32;