Skip to content
This repository was archived by the owner on Oct 23, 2025. It is now read-only.

Commit 3bae065

Browse files
committed
Expand CLI example
1 parent 9a46c78 commit 3bae065

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ assert_fs = "0.9.0"
2727
predicates = "0.9.0"
2828
convey_derive = { version = "0.2", path = "convey_derive" }
2929
rand = "0.5.5"
30+
structopt = "0.2"

examples/cli.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
extern crate failure;
22
#[macro_use]
3+
extern crate structopt;
4+
#[macro_use]
5+
extern crate serde_derive;
6+
#[macro_use]
37
extern crate convey;
48

9+
use structopt::StructOpt;
510
use convey::{
611
components::{newline, text},
712
human, json,
813
};
914

15+
/// Demo stuff
16+
#[derive(StructOpt)]
17+
struct Cli {
18+
/// Output JSON instead of human readable messages
19+
#[structopt(long = "json")]
20+
json: bool,
21+
}
22+
1023
fn main() -> Result<(), failure::Error> {
11-
let mut out = convey::new()
12-
.add_target(json::file("target/foo.log")?)
13-
.add_target(human::stdout()?);
24+
let args = Cli::from_args();
25+
let mut out = if args.json {
26+
convey::new().add_target(json::stdout()?)
27+
} else {
28+
convey::new().add_target(human::stdout()?)
29+
};
1430

1531
let x = 42;
1632

@@ -25,5 +41,30 @@ fn main() -> Result<(), failure::Error> {
2541
span!(intense = true, ["intense text",]),
2642
]))?;
2743

44+
#[derive(Serialize)]
45+
struct ErrorMessage {
46+
code: i32,
47+
name: String,
48+
message: String,
49+
}
50+
51+
impl convey::Render for ErrorMessage {
52+
render_for_humans!(self -> [
53+
span!(fg = "white", bg = "black", [text(self.code.to_string()), text(" "),]),
54+
span!(fg = "red", bg = "black", [text(&self.name),]),
55+
newline(),
56+
text("> "),
57+
text(&self.message),
58+
]);
59+
60+
render_json!();
61+
}
62+
63+
out.print(&ErrorMessage {
64+
code: 42,
65+
name: String::from("error"),
66+
message: String::from("Oh god no"),
67+
})?;
68+
2869
Ok(())
2970
}

0 commit comments

Comments
 (0)