select.rs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. extern crate clap;
  2. extern crate failure;
  3. extern crate rrecutils;
  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(
  12. clap::Arg::with_name("input")
  13. .short("i")
  14. .long("input")
  15. .value_name("FILE")
  16. .help("The input recfile (or - for stdin)"),
  17. )
  18. .arg(
  19. clap::Arg::with_name("output")
  20. .short("o")
  21. .long("output")
  22. .value_name("FILE")
  23. .help("The desired output location (or - for stdout)"),
  24. )
  25. .arg(
  26. clap::Arg::with_name("type")
  27. .long("type")
  28. .short("t")
  29. .required(false)
  30. .takes_value(true),
  31. )
  32. .arg(
  33. clap::Arg::with_name("include-descriptors")
  34. .long("include-descriptors")
  35. .short("d")
  36. .required(false)
  37. .takes_value(false),
  38. )
  39. .arg(
  40. clap::Arg::with_name("collapse")
  41. .long("collapse")
  42. .short("C")
  43. .required(false)
  44. .takes_value(false),
  45. )
  46. .arg(
  47. clap::Arg::with_name("sort")
  48. .long("sort")
  49. .short("S")
  50. .required(false)
  51. .takes_value(true),
  52. )
  53. .arg(
  54. clap::Arg::with_name("group-by")
  55. .long("group-by")
  56. .short("G")
  57. .required(false)
  58. .takes_value(true),
  59. )
  60. .get_matches()
  61. }
  62. fn run() -> Result<(), Error> {
  63. let matches = rr_select_args();
  64. let input = common::input_from_spec(matches.value_of("input"))?;
  65. let mut output = common::output_from_spec(matches.value_of("output"))?;
  66. let mut records = rrecutils::Recfile::parse(input)?;
  67. if let Some(typ) = matches.value_of("type") {
  68. records.filter_by_type(typ);
  69. }
  70. records.write(&mut output)?;
  71. Ok(())
  72. }
  73. fn main() {
  74. match run() {
  75. Ok(()) => (),
  76. Err(e) => println!("{}", e),
  77. }
  78. }