Skip to content

Commit

Permalink
Maintain command-line compatibility with Dustin's petname #2
Browse files Browse the repository at this point in the history
This time it's the `--dir` option.
  • Loading branch information
allenap committed Sep 23, 2023
1 parent 1c60aa0 commit 6ffc871
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ If you have [installed Cargo][install-cargo], you can install rust-petname with
`cargo install petname`. This puts a `petname` binary in `~/.cargo/bin`, which
the Cargo installation process will probably have added to your `PATH`.

The `petname` binary from rust-petname is drop-in compatible with the original
`petname`. It's more strict when validating arguments, but for most uses it
should behave the same.
The `petname` binary from rust-petname is _mostly_ drop-in compatible with the
original `petname`. It has more options and it's stricter when validating
arguments, but for most uses it should behave the same[^differences].

[^differences]:
When using the `--dir` option, Dustin Kirkland's _petname_ looks for a
file named `names.txt` whereas this looks for `nouns.txt` first before
checking for `names.txt`.

```shellsession
$ petname -h
Expand Down Expand Up @@ -181,6 +186,8 @@ command-line too. Below are the most important:
- When using custom word lists with `--dir <DIR>`, nouns are now found in a file
named appropriately `DIR/nouns.txt`. Previously this was `names.txt` but this
was confusing; the term "names" is overloaded enough already.
- For compatibility, if `nouns.txt` is not found, an attempt will be made to
load nouns from `names.txt`.
- The option `--count 0` is no longer a synonym for `--stream`. Use `--stream`
instead. It's not an error to pass `--count 0`, but it will result in zero
names being generated.
Expand Down
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,15 @@ impl Words {
Ok(Self::Custom(
read_file_to_string(dirname.join("adjectives.txt"))?,
read_file_to_string(dirname.join("adverbs.txt"))?,
read_file_to_string(dirname.join("nouns.txt"))?,
// Load `nouns.txt`, but fall back to trying `names.txt` for
// compatibility with Dustin Kirkland's _petname_.
match read_file_to_string(dirname.join("nouns.txt")) {
Ok(nouns) => nouns,
Err(err) => match read_file_to_string(dirname.join("names.txt")) {
Ok(nouns) => nouns,
Err(_) => Err(err)?, // Error from `nouns.txt`.
},
},
))
}
}
Expand Down

0 comments on commit 6ffc871

Please sign in to comment.