Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ members = [
"pwd",
"sleep",
"true",
"tty",
"whoami",
"uname",
"tty",
"uniq",
"yes"
]
2 changes: 2 additions & 0 deletions uniq/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# For rls generated files in this directory
/target
13 changes: 13 additions & 0 deletions uniq/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "uniq"
version = "0.0.1"
authors = ["Vaibhav Yenamandra <yvvaibhav@gmail.com>"]
build = "build.rs"
edition = "2018"

[dependencies]
clap = { version = "^2.33.0", features = ["yaml", "wrap_help"] }
coreutils_core = { path = "../coreutils_core" }

[build-dependencies]
clap = { version = "^2.33.0", features = ["yaml"] }
19 changes: 19 additions & 0 deletions uniq/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::env;

use clap::{ App, Shell, load_yaml };

fn main() {
let yaml = load_yaml!("src/uniq.yml");
let mut app = App::from_yaml(yaml);

let out_dir = match env::var("OUT_DIR") {
Ok(dir) => dir,
_ => return
};

app.gen_completions("uniq", Shell::Zsh, out_dir.clone());
app.gen_completions("uniq", Shell::Fish, out_dir.clone());
app.gen_completions("uniq", Shell::Bash, out_dir.clone());
app.gen_completions("uniq", Shell::PowerShell, out_dir.clone());
app.gen_completions("uniq", Shell::Elvish, out_dir);
}
88 changes: 88 additions & 0 deletions uniq/src/execution_options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::process;

pub enum Counters {
All,
UniqueOnly,
DuplicateOnly
}

pub struct ExecutionOptions {
count_kind: Counters,
ignore_case: bool,
read_stdin: bool,
field_skip: u32,
char_skip: u32,
write_stdout: bool,
input_file_path: Option<String>,
output_file_path: Option<String>,
}

fn parse_num_or_panic(num_s: &str, message: String) -> u32 {
match num_s.parse() {
Ok(n) => n,
Err(_err) => {
eprintln!("{}", message);
process::exit(1);
}
}
}

/// Accept a match object from clap, and return a struct representing current
/// CLI options
///
/// # Arguments
/// * `matches` - A `clap::ArgMatches` object to transform into a struct
/// representing the parameters of execution
pub fn read_clap_matches(arg_matches: clap::ArgMatches) -> ExecutionOptions {
let read_stdin = !arg_matches.is_present("input_file");
let write_stdout = !arg_matches.is_present("output_file");

ExecutionOptions {
count_kind:
if arg_matches.is_present("duplicate-only") {
Counters::DuplicateOnly
} else if arg_matches.is_present("unique-only") {
Counters::UniqueOnly
} else {
Counters::All
},
ignore_case: arg_matches.is_present("ignore-case"),
field_skip: if arg_matches.is_present("num") {
let num_s = arg_matches.value_of("num").unwrap();
parse_num_or_panic(num_s,
format!("uniq: Illegal field skip value: {}", num_s))
} else {
0
},
char_skip: if arg_matches.is_present("chars") {
let num_s = arg_matches.value_of("chars").unwrap();
parse_num_or_panic(num_s,
format!("uniq: Illegal character skip value: {}", num_s))
} else {
0
},
read_stdin,
write_stdout,
input_file_path: if read_stdin {
None
} else {
match arg_matches.value_of("input_file") {
Some(ref path) =>
if path == &"-" {
None
} else {
Some(path.to_string())
}
None => None,
}
},
output_file_path: if write_stdout {
None
} else {
match arg_matches.value_of("output_file") {
Some(ref path) => Some(path.to_string()),
None => None,
}
},
}
}
8 changes: 8 additions & 0 deletions uniq/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use clap::{load_yaml, App};

mod execution_options;

fn main() {
let yaml = load_yaml!("uniq.yml");
let _opts = execution_options::read_clap_matches(App::from_yaml(yaml).get_matches());
}
34 changes: 34 additions & 0 deletions uniq/src/uniq.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: uniq
version: "0.0.0"
author: Vaibhav Yenamandra <yvvaibhav+rust@gmail.com>
about: "Report or fitler out duplicated lines from a file or stream.\nCommand descriptions taken from 'BSD General Commands Manual page for uniq.1'"
args:
- count-all:
help: Precede each output line with the count of number of times the
line has occured, followed by a single space
short: c
- duplicate-only:
help: Only output lines that are repeated in the input
short: d
- unique-only:
help: Only output lines that are not repeated in the input
short: u
- ignore-case:
short: i
help: Turn on case insensitive comparison of lines
- num:
short: f
takes_value: true
- chars:
short: s
takes_value: true
- input_file:
takes_value: true
- output_file:
takes_value: true
groups:
- counting-strategy:
args:
- count-all
- duplicate-only
- unique-only