Skip to content

Commit

Permalink
remove file cli flag and add description
Browse files Browse the repository at this point in the history
  • Loading branch information
Fogapod committed Feb 10, 2024
1 parent 0821819 commit 619b3ef
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 35 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,23 @@ Full reference:
```

See more examples in [examples](examples) folder.

## CLI tool

This library comes with a simple command line tool you can install with:

```sh
cargo install sayit --features=cli
```

Interactive session:

```sh
sayit --accent examples/scotsman.ron
```

Apply to file:

```sh
cat filename.txt | sayit --accent examples/french.ron > newfile.txt
```
46 changes: 11 additions & 35 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,23 @@
use std::{
fs::{self, File},
io::{self, BufRead},
path::PathBuf,
};
use sayit::Accent;

use clap::Parser;
use std::{fs, io, path::PathBuf};

use sayit::Accent;
use clap::Parser;

#[derive(Parser, Debug)]
#[command(version, about)]
struct Args {
/// Accent file path (currently only ron supported)
/// Read accent from file (currently only ron supported)
#[arg(short, long, group = "accent_def")]
accent: Option<PathBuf>,

/// Directly provided accent (ron format)
/// Read accent from stdin(currently only ron supported)
#[arg(long, group = "accent_def")]
accent_string: Option<String>,

/// Accent intensity
/// Set intensity
#[arg(short, long, default_value_t = 0)]
intensity: u64,

/// File to apply accent to. Reads from stdin if unset
#[arg(short, long)]
file: Option<PathBuf>,
}

fn apply_accent(accent: &Accent, instensity: u64, line: io::Result<String>) -> Result<(), String> {
println!(
"{}",
accent.say_it(
&line.map_err(|err| format!("reading line: {err}"))?,
instensity
)
);

Ok(())
}

fn main() -> Result<(), String> {
Expand All @@ -54,15 +34,11 @@ fn main() -> Result<(), String> {
let accent =
ron::from_str::<Accent>(&accent_string).map_err(|err| format!("parsing accent: {err}"))?;

if let Some(filename) = args.file {
let file = File::open(filename).map_err(|err| format!("reading input file: {err}"))?;
for line in io::BufReader::new(file).lines() {
apply_accent(&accent, args.intensity, line)?;
}
} else {
for line in io::stdin().lines() {
apply_accent(&accent, args.intensity, line)?;
}
for line in io::stdin().lines() {
let line = line.map_err(|err| format!("reading line: {err}"))?;
let applied = accent.say_it(&line, args.intensity);

println!("{applied}");
}

Ok(())
Expand Down

0 comments on commit 619b3ef

Please sign in to comment.