Skip to content

Commit

Permalink
improved CLI and updated README CLI docs
Browse files Browse the repository at this point in the history
  • Loading branch information
cqb13 committed Jul 8, 2024
1 parent ae1f47d commit 3452e20
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 95 deletions.
88 changes: 43 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ To install and build from source, you must have [Rust](https://www.rust-lang.org
```bash
$ git clone https://github.com/cqb13/vocab-vault.git
$ cd vocab-vault
$ cargo install --path .
$ cargo build --release
$ cargo run --release -- [command] [arguments]
# to add to path
$ cargo install --path .
```

#### From Binary
Expand All @@ -37,60 +38,57 @@ You can also use the [website](https://learninglatin.net/translate) to translate

**Note:** The website is currently using the original TypeScript code, not the Rust code.

### Command Line Arguments

- `--help` or `-h`: Display help information

#### `transEng` Command (Translate English to Latin)
### Usage

Translate English text to Latin using the following command:

```bash
$ vocab_vault transEng "English text to translate"
```sh
vocab-vault [COMMAND] [OPTIONS]
```

- `"English text to translate"`: The English text you want to translate
- `-m` or `--max ` `<MAX_ENTRIES>`: The maximum number of entries to return (default: 6)
- `-s` or `--sort`: Sort the output by frequency
- `-p` or `--pretty`: Display a pretty version of the output (requires `-f`)
- `-d` or `--detailed`: Add more information to prettified output (requires `-p`)

#### `transLat` Command (Translate Latin to English)

Translate Latin text to English using the following command:
#### Commands

```bash
$ vocab_vault transLat "Latin text to translate"
```
transEng
Translate english to latin
<WORDS> The words to translate
-m --max <MAX> The maximum number of translations per definition (default: 6)
-s --sort <> Sort the output by word frequency
-p --pretty <> Prints the output in a pretty format
-d --detailed <> Adds more information to the pretty output
transLat
Translate latin to english
<WORDS> The words to translate
-m --max <MAX> The maximum number of translations per definition (default: 6)
-s --sort <> Sort the output by word frequency
-p --pretty <> Prints the output in a pretty format
-d --detailed <> Adds more information to the pretty output
-t --tricks <> Will attempt to use various tricks to find the translation
getList
Gets a list of words based on the options provided
<TYPE> The type of words to get. Options: english, latin, inflections, not_packons, packons, prefixes, stems, suffixes, tackons, tickons, unique_latin
-p --pos <POS> The part of speeches to include, separated by commas
-m --max <MAX> The maximum word length
-n --min <MIN> The minimum word length
-e --exact <EXACT> The exact word length
-a --amount <AMOUNT> The amount of words to get
-r --random <> Get words from a random position
-d --display <> Will display as json
-t --to <TO> The file to export the results to
help
Helps you
<COMMAND> A command to help with
tui
Starts the tui (.help for info)
```

- `"Latin text to translate"`: The Latin text you want to translate
- `-t` or `--tricks`: Attempt to use various tricks on words for better results
- `-m` or `--max ` `<MAX_ENTRIES>`: The maximum number of entries to return (default: 6)
- `-s` or `--sort`: Sort the output by frequency
- `-p` or `--pretty`: Display a pretty version of the output (requires `-f`)
- `-d` or `--detailed`: Add more information to prettified output (requires `-p`)

#### `getList` Command (Gets a specific list of words from the dictionary)
### Example Usage

Get a specific list of words from the dictionary using the following command:
Help:

```bash
$ vocab_vault getList "word_type"
```

- `"word_type"`: The type of word list you want to get
- english, latin, inflections, not_packons, packons, prefixes, stems, suffixes, tackons, tickons, unique_latin
- `-p` or `--pos` `<part Of Speech List>`: A list of parts of speech to filter by (separated by commas)
- noun, verb, participle, adjective, preposition, pronoun, interjection, numeral, conjunction, adverb, number, supine, packon, tackon, prefix, suffix
- `-m` or `--max` `<MAX_WORD_LEN>`: The maximum length of words to return
- `-n` or `--min` `<MIN_WORD_LEN>`: The minimum length of words to return
- `-e` or `--exact` `<EXACT>`: Only return words that match the exact length
- `-a` or `--amount` `<AMOUNT>`: The amount of words to return
- `-r` or `--random`: Picks words from random positions
- `-d` or `--display`: Display the words as json
- `-t` or `--to` `<TO>`: Saves the list of words to a json file
$ vocab_vault help

### Example Usage
$ vocab_vault help transEng
```

Translate English to Latin with 2 options per translation which are sorted by frequency:

Expand Down
92 changes: 45 additions & 47 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub struct Arg {
#[derive(Debug)]
pub struct Command<'a> {
pub name: &'a str,
pub short: Option<char>,
pub description: &'a str,
pub args: Option<Vec<Arg>>,
}
Expand Down Expand Up @@ -83,17 +82,11 @@ impl<'a> Command<'a> {
pub fn new(name: &'a str, description: &'a str) -> Command<'a> {
Command {
name,
short: None,
description,
args: None,
}
}

pub fn with_short(mut self, short: char) -> Command<'a> {
self.short = Some(short);
self
}

/**
* Adds an argument to the command
*/
Expand Down Expand Up @@ -286,26 +279,14 @@ impl<'a> Cli<'a> {
std::process::exit(0);
})
} else {
self.help();
self.help(None);
std::process::exit(0);
}
} else {
let command_name = &args[1];
self.commands
.iter()
.find(|&command| {
command.name == command_name
|| (command.short
== Some(
command_name
.replace("-", "")
.to_lowercase()
.chars()
.next()
.unwrap(),
)
&& command_name.len() == 1)
})
.find(|&command| command.name == command_name)
.unwrap_or_else(|| {
println!("Command not found: {}", command_name);
std::process::exit(0);
Expand All @@ -317,7 +298,7 @@ impl<'a> Cli<'a> {
println!("{} {}", self.name, self.version);
}

pub fn help(&self) {
pub fn help(&self, command_name: Option<String>) {
println!("{} {}", self.name, self.version);
println!("{}", self.description);
println!("Author: {}", self.author);
Expand All @@ -327,35 +308,52 @@ impl<'a> Cli<'a> {
println!(" {} [COMMAND] [OPTIONS]", self.bin);
println!();
println!("COMMANDS:");
for command in &self.commands {
println!(" {:<12} -{}", command.name, command.short.unwrap_or(' '));
println!(" {}", command.description);

if let Some(args) = &command.args {
for arg in args {
let short = arg
.short
.map(|s| format!("-{}", s))
.unwrap_or("".to_string());
let long = arg
.long
.map(|s| format!("--{}", s))
.unwrap_or("".to_string());
let value = format!("<{}>", arg.value_name);
let default = arg.default.as_ref().map(|s| format!(" (default: {})", s));
println!(
" {:<12} {:<12} {:<12} {:<12}{}",
short,
long,
value,
arg.help,
default.unwrap_or("".to_string())
);
}
if command_name.is_some() {
let command = self
.commands
.iter()
.find(|&command| command.name == command_name.as_ref().unwrap())
.unwrap_or_else(|| {
println!("Command not found: {}", command_name.unwrap());
std::process::exit(0);
});

self.command_help(command)
} else {
for command in &self.commands {
self.command_help(&command)
}
}
println!();
}

fn command_help(&self, command: &Command<'a>) {
println!(" {:<12}", command.name);
println!(" {}", command.description);

if let Some(args) = &command.args {
for arg in args {
let short = arg
.short
.map(|s| format!("-{}", s))
.unwrap_or("".to_string());
let long = arg
.long
.map(|s| format!("--{}", s))
.unwrap_or("".to_string());
let value = format!("<{}>", arg.value_name);
let default = arg.default.as_ref().map(|s| format!(" (default: {})", s));
println!(
" {:<12} {:<12} {:<12} {:<12}{}",
short,
long,
value,
arg.help,
default.unwrap_or("".to_string())
);
}
}
}
}

#[derive(Debug)]
Expand Down
13 changes: 10 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,14 @@ fn main() {
.with_value_name("TO")
.with_help("The file to export the results to"),
),
Command::new("help", "Helps you"),
Command::new("tui", "Starts the tui"),
Command::new("help", "Helps you")
.with_arg(
Arg::new()
.with_name("command")
.with_value_name("COMMAND")
.with_help("A command to help with"),
),
Command::new("tui", "Starts the tui (.help for info)"),
]);

let command = cli.match_commands();
Expand Down Expand Up @@ -235,7 +241,8 @@ fn main() {
);
}
"help" => {
cli.help();
let command = command.get_value().to_option();
cli.help(command);
}
"tui" => {
let mut input = String::new();
Expand Down

0 comments on commit 3452e20

Please # to comment.