select.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. extern crate clap;
  2. extern crate rrecutils;
  3. extern crate failure;
  4. mod common;
  5. use failure::Error;
  6. fn rr_select_args() -> clap::ArgMatches<'static> {
  7. clap::App::new("rr-sel")
  8. .version(common::VERSION)
  9. .author(common::AUTHOR)
  10. .about("Print records from a recfile")
  11. .arg(clap::Arg::with_name("input")
  12. .short("i")
  13. .long("input")
  14. .value_name("FILE")
  15. .help("The input recfile (or - for stdin)"))
  16. .arg(clap::Arg::with_name("output")
  17. .short("o")
  18. .long("output")
  19. .value_name("FILE")
  20. .help("The desired output location (or - for stdout)"))
  21. .arg(clap::Arg::with_name("type")
  22. .long("type")
  23. .short("t")
  24. .required(false)
  25. .takes_value(true))
  26. .arg(clap::Arg::with_name("include-descriptors")
  27. .long("include-descriptors")
  28. .short("d")
  29. .required(false)
  30. .takes_value(false))
  31. .arg(clap::Arg::with_name("collapse")
  32. .long("collapse")
  33. .short("C")
  34. .required(false)
  35. .takes_value(false))
  36. .arg(clap::Arg::with_name("sort")
  37. .long("sort")
  38. .short("S")
  39. .required(false)
  40. .takes_value(true))
  41. .arg(clap::Arg::with_name("group-by")
  42. .long("group-by")
  43. .short("G")
  44. .required(false)
  45. .takes_value(true))
  46. .get_matches()
  47. }
  48. fn run() -> Result<(), Error> {
  49. let matches = rr_select_args();
  50. let input = common::input_from_spec(
  51. matches.value_of("input"))?;
  52. let mut output = common::output_from_spec(
  53. matches.value_of("output"))?;
  54. let mut records = rrecutils::Recfile::parse(input)?;
  55. if let Some(typ) = matches.value_of("type") {
  56. records.filter_by_type(typ);
  57. }
  58. records.write(&mut output)?;
  59. Ok(())
  60. }
  61. fn main() {
  62. match run() {
  63. Ok(()) => (),
  64. Err(e) => println!("{}", e),
  65. }
  66. }