From e0b7ff1953f82b15bfebfb8391bda651766318dd Mon Sep 17 00:00:00 2001 From: Wim Hueskes Date: Mon, 18 Jul 2016 22:46:04 +0200 Subject: [PATCH] od: implement --help and --version --- Cargo.lock | 1 + src/od/Cargo.toml | 1 + src/od/od.rs | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 31636641b..aafb0f1cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -660,6 +660,7 @@ version = "0.0.1" dependencies = [ "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "unindent 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/src/od/Cargo.toml b/src/od/Cargo.toml index d6320dd65..43bd178c5 100644 --- a/src/od/Cargo.toml +++ b/src/od/Cargo.toml @@ -10,6 +10,7 @@ path = "od.rs" [dependencies] getopts = "*" libc = "*" +unindent = "*" [[bin]] name = "od" diff --git a/src/od/od.rs b/src/od/od.rs index 62693a1a3..f09cb8134 100644 --- a/src/od/od.rs +++ b/src/od/od.rs @@ -10,6 +10,7 @@ */ extern crate getopts; +extern crate unindent; use std::fs::File; use std::io::Read; @@ -17,6 +18,7 @@ use std::mem; use std::io::BufReader; use std::io::Write; use std::io; +use unindent::*; //This is available in some versions of std, but not all that we target. macro_rules! hashmap { @@ -27,6 +29,8 @@ macro_rules! hashmap { }} } +static NAME: &'static str = "od"; +static VERSION: &'static str = env!("CARGO_PKG_VERSION"); #[derive(Debug)] enum Radix { Decimal, Hexadecimal, Octal, Binary } @@ -77,6 +81,20 @@ pub fn uumain(args: Vec) -> i32 { Err(f) => panic!("Invalid options\n{}", f) }; + if matches.opt_present("h") { + let msg = unindent(&format!(" + Usage: + {0} [OPTION]... [FILENAME]... + + Displays data in various human-readable formats.", NAME)); + println!("{}", opts.usage(&msg)); + return 0; + } + if matches.opt_present("version") { + println!("{} {}", NAME, VERSION); + return 0; + } + let input_offset_base = match parse_radix(matches.opt_str("A")) { Ok(r) => r, Err(f) => { panic!("Invalid -A/--address-radix\n{}", f) }