Browse Source

Use getopts to parse command-line

master
Matteo Cypriani 2 years ago
parent
commit
2b8f4a98d3
4 changed files with 89 additions and 27 deletions
  1. 10
    0
      Cargo.lock
  2. 1
    0
      Cargo.toml
  3. 77
    26
      src/main.rs
  4. 1
    1
      src/metadata/metadata.rs

+ 10
- 0
Cargo.lock View File

@@ -1,4 +1,14 @@
1 1
 [root]
2 2
 name = "nrgrip"
3 3
 version = "0.1.0"
4
+dependencies = [
5
+ "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
6
+]
4 7
 
8
+[[package]]
9
+name = "getopts"
10
+version = "0.2.14"
11
+source = "registry+https://github.com/rust-lang/crates.io-index"
12
+
13
+[metadata]
14
+"checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685"

+ 1
- 0
Cargo.toml View File

@@ -9,3 +9,4 @@ name = "nrgrip"
9 9
 doc = false
10 10
 
11 11
 [dependencies]
12
+getopts = "0.2"

+ 77
- 26
src/main.rs View File

@@ -24,31 +24,52 @@ use std::env;
24 24
 use std::fs::File;
25 25
 use std::process;
26 26
 
27
-extern crate nrgrip;
28
-
27
+extern crate getopts;
28
+use getopts::Options;
29 29
 
30
-fn exit_usage(prog_name: &String) {
31
-    println!("Usage:\n\t{} <image.nrg>", prog_name);
32
-    process::exit(1);
33
-}
30
+extern crate nrgrip;
34 31
 
35 32
 
36 33
 fn main() {
37
-    let mut argv = env::args();
34
+    let args: Vec<String> = env::args().collect();
35
+    let prog_name = &args.first().expect("Can't retrieve program's name");
38 36
 
39
-    let prog_name = argv.next().unwrap_or("nrgrip".to_string());
37
+    let mut opts = Options::new();
38
+    opts.optflag("i", "info",
39
+                 "display the image's metadata (default action)");
40
+    opts.optflag("x", "extract",
41
+                 "same as --extract-cue --extract-raw");
42
+    opts.optflag("c", "extract-cue",
43
+                 "extract cue sheet from the NRG metadata");
44
+    opts.optflag("r", "extract-raw",
45
+                 "extract the raw audio tracks");
46
+    opts.optflag("h", "help",
47
+                 "print this help message");
40 48
 
41
-    let img_path = argv.next().unwrap_or("".to_string());
42
-    if img_path == "" {
43
-        exit_usage(&prog_name);
49
+    let options = match opts.parse(&args[1..]) {
50
+        Ok(m) => m,
51
+        Err(err) => panic!(err.to_string()),
52
+    };
53
+
54
+    if options.opt_present("help") {
55
+        exit_usage(&prog_name, &opts);
44 56
     }
45 57
 
58
+    // Get input NRG image name
59
+    if options.free.len() != 1 {
60
+        // We need exactly one input file!
61
+        fail_usage(&prog_name, &opts);
62
+    }
63
+    let img_path = &options.free[0];
46 64
     println!("NRG image path: \"{}\"", img_path);
47 65
 
48
-    // We don't support more than one input file
49
-    if argv.next().is_some() {
50
-        exit_usage(&prog_name);
51
-    }
66
+    // See what actions are to be taken on that file
67
+    let action_cue =
68
+        options.opt_present("extract-cue") || options.opt_present("extract");
69
+    let action_raw =
70
+        options.opt_present("extract-raw") || options.opt_present("extract");
71
+    let action_info =
72
+        options.opt_present("info") || !(action_cue || action_raw);
52 73
 
53 74
     // Open the image file
54 75
     let fd = File::open(&img_path);
@@ -59,7 +80,7 @@ fn main() {
59 80
     }
60 81
     let mut fd = fd.unwrap();
61 82
 
62
-    // Read and display the image's metadata
83
+    // Read the image's metadata
63 84
     let metadata = nrgrip::metadata::read_nrg_metadata(&mut fd);
64 85
     if metadata.is_err() {
65 86
         println!("Error reading \"{}\": {}",
@@ -67,20 +88,50 @@ fn main() {
67 88
         process::exit(1);
68 89
     }
69 90
     let metadata = metadata.unwrap();
70
-    println!("\n{}", metadata);
91
+
92
+    // Display metadata if requested
93
+    if action_info {
94
+        println!("\n{}", metadata);
95
+    }
71 96
 
72 97
     // Read and write the cue sheet
73
-    println!("\nNow extracting cue sheet...");
74
-    if let Err(err) = nrgrip::cue_sheet::write_cue_sheet(&img_path, &metadata) {
75
-        println!("Error writing cue sheet: {}", err.to_string());
76
-        process::exit(1);
98
+    if action_cue {
99
+        println!("\nExtracting cue sheet...");
100
+        if let Err(err) = nrgrip::cue_sheet::write_cue_sheet(&img_path,
101
+                                                             &metadata) {
102
+            println!("Error writing cue sheet: {}", err.to_string());
103
+            process::exit(1);
104
+        }
105
+        println!("OK!");
77 106
     }
78 107
 
79 108
     // Extract raw audio data
80
-    println!("Now extracting raw audio data...");
81
-    if let Err(err) = nrgrip::raw_audio::extract_nrg_raw_audio(
82
-        &mut fd, &img_path, &metadata) {
83
-        println!("Error extracting raw audio data: {}", err.to_string());
109
+    if action_raw {
110
+        println!("\nExtracting raw audio data...");
111
+        if let Err(err) = nrgrip::raw_audio::extract_nrg_raw_audio(
112
+            &mut fd, &img_path, &metadata) {
113
+            println!("Error extracting raw audio data: {}", err.to_string());
114
+        }
115
+        println!("OK!");
84 116
     }
85
-    println!("OK!");
117
+}
118
+
119
+
120
+fn exit_usage(prog_name: &str, opts: &Options) {
121
+    print_usage(prog_name, opts);
122
+    process::exit(0);
123
+}
124
+
125
+fn fail_usage(prog_name: &str, opts: &Options) {
126
+    print_usage(prog_name, opts);
127
+    process::exit(1);
128
+}
129
+
130
+fn print_usage(prog_name: &str, opts: &Options) {
131
+    let brief = format!("NRGrip - rip Nero Burning ROM audio images
132
+
133
+Usage:
134
+    {prog} [-icrx] [options] <image.nrg>
135
+    {prog} -h", prog = prog_name);
136
+    print!("{}", opts.usage(&brief));
86 137
 }

+ 1
- 1
src/metadata/metadata.rs View File

@@ -105,7 +105,7 @@ impl fmt::Display for NrgMetadata {
105 105
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
106 106
         try!(write!(f, "Image size: {} Bytes\n\
107 107
                         NRG format version: {}\n\
108
-                        First chunk offset: {}",
108
+                        First NRG chunk offset: {}",
109 109
                     self.file_size,
110 110
                     self.nrg_version,
111 111
                     self.chunk_offset,

Loading…
Cancel
Save