Skip to content

Commit

Permalink
Upgrade to clap v4 (#8)
Browse files Browse the repository at this point in the history
Also cleanup some documentation and handle some errors from Clap in a different module.
  • Loading branch information
slawlor authored Jan 30, 2023
1 parent fb300e3 commit 127f027
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 19 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,22 @@ jobs:
with:
command: fmt
args: --all -- --check

docs:
name: docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- name: Install minimal stable with rustfmt
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
components: rustfmt
override: true

- name: Run cargo doc on release profile
uses: actions-rs/cargo@v1
with:
command: doc
args: --lib -r
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@

# rustyrepl

The easy Rust Read-Evaluate-Print-Loop (REPL) utility crate

[<img alt="github" src="https://img.shields.io/badge/github-slawlor/repl-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/slawlor/repl)
[<img alt="crates.io" src="https://img.shields.io/crates/v/rustyrepl.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/rustyrepl)
[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-rustyrepl-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/rustyrepl)
[<img alt="build status" src="https://img.shields.io/github/workflow/status/slawlor/repl/CI/main?style=for-the-badge" height="20">](https://github.com/slawlor/repl/actions?query=branch%3Amain)
[![CI/main](https://github.com/slawlor/repl/actions/workflows/ci.yaml/badge.svg?branch=main)](https://github.com/slawlor/repl/actions/workflows/ci.yaml)

## About

`rustyrepl` is a simple crate to facilitate creation of Read, Evaluate, Print, Loop utilities at the command-line.
`rustyrepl` is a simple crate to facilitate creation of Read, Evaluate, Print, Loop utilities at the command-line. A unique combination of `rustyline` and `clap` to build a simple REPL interface
with handy argument parsing.

## Purpose

1. Capturing exits/quits of the REPL interface
2. Storing and managing REPL history commands as well as an index of said commands for you
3. Allowing operators to get a help menu at any point, using the full Clap supported help interface (i.e. sub-command help as well)
4. Processing the commands as incoming

# Usage
First, add rustyrepl to your Cargo.toml file

First, add rustyrepl to your `Cargo.toml` file

```toml
[dependencies]
rustyrepl = "0.1"
rustyrepl = "0.2"
```

Next:

```rust
use anyhow::Result;
use clap::{Parser, Subcommand};
Expand Down Expand Up @@ -62,3 +69,20 @@ async fn main() -> Result<()> {
repl.process().await
}
```

This small program will startup up a REPL with the prompt ">>" which you can interact with

```text
>> help
The general CLI, essentially a wrapper for the sub-commands [Commands]
Usage: repl-interface <COMMAND>
Commands:
test Execute a test command
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
```
2 changes: 1 addition & 1 deletion repl-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ publish = false
[dependencies]
anyhow = { version = "1" }
async-trait = "0.1"
clap = { version = "3", features = ["derive"] }
clap = { version = "4", features = ["derive"] }
colored = "2"
log = { version = "0.4.8", features = ["kv_unstable"] }
multi_log = "0.1"
Expand Down
8 changes: 4 additions & 4 deletions repl-test/src/console_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ impl ConsoleLogger {
let target = {
if let Some(target_str) = record.target().split(':').last() {
if let Some(line) = record.line() {
format!(" ({}:{})", target_str, line)
format!(" ({target_str}:{line})")
} else {
format!(" ({})", target_str)
format!(" ({target_str})")
}
} else {
"".to_string()
Expand Down Expand Up @@ -65,9 +65,9 @@ impl ConsoleLogger {
Level::Warn => msg.yellow().bold(),
Level::Error => msg.red().bold(),
};
let _ = writeln!(io, "{}", msg);
let _ = writeln!(io, "{msg}");
} else {
let _ = writeln!(io, "{}", msg);
let _ = writeln!(io, "{msg}");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion repl-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub enum Command {
Test,
}

/// The general CLI, essentially a wrapper for the sub-commands [Commands]
/// The general CLI, essentially a wrapper for the sub-commands [Command]
#[derive(Parser, Clone, Debug)]
pub struct Cli {
#[clap(subcommand)]
Expand Down
4 changes: 2 additions & 2 deletions rustyrepl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustyrepl"
version = "0.1.4"
version = "0.2.0"
authors = ["Sean Lawlor <[email protected]>"]
description = "A Rust read, evaluate, print, loop (REPL) utility "
license = "MIT"
Expand All @@ -16,7 +16,7 @@ default = []
[dependencies]
# Required dependencies
anyhow = { version = "1" }
clap = { version = "3", features = ["derive"] }
clap = { version = "4", features = ["derive"] }
dirs = "2"
log = { version = "0.4", features = ["kv_unstable"] }
rustyline = "7"
Expand Down
8 changes: 4 additions & 4 deletions rustyrepl/src/common_test/console_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ impl ConsoleLogger {
let target = {
if let Some(target_str) = record.target().split(':').last() {
if let Some(line) = record.line() {
format!(" ({}:{})", target_str, line)
format!(" ({target_str}:{line})")
} else {
format!(" ({})", target_str)
format!(" ({target_str})")
}
} else {
"".to_string()
Expand Down Expand Up @@ -60,9 +60,9 @@ impl ConsoleLogger {
Level::Warn => msg.yellow().bold(),
Level::Error => msg.red().bold(),
};
let _ = writeln!(io, "{}", msg);
let _ = writeln!(io, "{msg}");
} else {
let _ = writeln!(io, "{}", msg);
let _ = writeln!(io, "{msg}");
}
}
}
Expand Down
19 changes: 17 additions & 2 deletions rustyrepl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//!
//! ```toml
//! [dependencies]
//! rustyrepl = "0.1"
//! rustyrepl = "0.2"
//! ```
//!
//! Next:
Expand All @@ -39,7 +39,7 @@
//! Test,
//! }
//!
//! /// The general CLI, essentially a wrapper for the sub-commands [Commands]
//! /// The general CLI, essentially a wrapper for the sub-commands [Command]
//! #[derive(Parser, Clone, Debug)]
//! pub struct Cli {
//! #[clap(subcommand)]
Expand Down Expand Up @@ -71,7 +71,22 @@
//! let mut repl = Repl::<Cli>::new(processor, None, Some(">>".to_string()))?;
//! repl.process().await
//! }
//! ```
//!
//! This small program will startup up a REPL with the prompt ">>" which you can interact with
//!
//! ```text
//! >> help
//! The general CLI, essentially a wrapper for the sub-commands [Commands]
//!
//! Usage: repl-interface <COMMAND>
//!
//! Commands:
//! test Execute a test command
//! help Print this message or the help of the given subcommand(s)
//!
//! Options:
//! -h, --help Print help
//!
//! ```
Expand Down
3 changes: 2 additions & 1 deletion rustyrepl/src/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ macro_rules! process_block {
get_specific_processing_call!($self, cli);
}
Err(clap_err) => match clap::Error::kind(&clap_err) {
clap::ErrorKind::DisplayHelp | clap::ErrorKind::DisplayVersion => {

clap::error::ErrorKind::DisplayHelp | clap::error::ErrorKind::DisplayVersion => {
println!("{}", clap_err);
}
_ => {
Expand Down

0 comments on commit 127f027

Please sign in to comment.