main.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (C) 2015, Alberto Corona <alberto@0x1a.us>
  2. // All rights reserved. This file is part of core-utils, distributed under the
  3. // GPL v3 license. For full terms please see the LICENSE file.
  4. #![crate_type = "bin"]
  5. #![crate_name = "mv"]
  6. #![feature(collections)]
  7. static VERS: &'static str = "0.1.0";
  8. static PROG: &'static str = "mv";
  9. extern crate getopts;
  10. extern crate util;
  11. use getopts::{Options};
  12. use util::{Status};
  13. use std::env;
  14. use std::fs;
  15. use std::path::{PathBuf};
  16. fn rename(from: PathBuf, to: PathBuf) {
  17. match fs::rename(from, to) {
  18. Ok(s) => { s },
  19. Err(e) => {
  20. util::err(PROG, Status::Error, e.to_string());
  21. }
  22. }
  23. }
  24. fn print_usage(prog: &str, opts: Options) {
  25. let brief = format!("Usage: {} [OPTION] SOURCE DEST\nRename SOURCE to DEST", prog);
  26. print!("{}", opts.usage(&brief));
  27. }
  28. fn main() {
  29. let args: Vec<String> = env::args().collect();
  30. let mut opts = Options::new();
  31. opts.optflag("h", "help", "Print the help menu");
  32. opts.optflag("", "version", "Print the version of mv");
  33. let matches = match opts.parse(&args[1..]) {
  34. Ok(m) => { m },
  35. Err(e) => {
  36. util::err(PROG, Status::OptError, e.to_string());
  37. panic!(e.to_string())
  38. }
  39. };
  40. if matches.opt_present("h") {
  41. print_usage(PROG, opts);
  42. } else if matches.opt_present("version") {
  43. util::copyright(PROG, VERS, "2015", vec!["Alberto Corona"]);
  44. } else {
  45. match matches.free.first() {
  46. Some(src) => {
  47. if matches.free.len() == 1 {
  48. println!("mv: No destination specified for '{}'", src);
  49. util::exit(Status::Error);
  50. }
  51. for items in matches.free.tail() {
  52. rename(PathBuf::from(src), PathBuf::from(items));
  53. }
  54. }
  55. None => {
  56. println!("mv: No files specified");
  57. util::exit(Status::Error);
  58. }
  59. };
  60. }
  61. }