Skip to content

Commit c163ee5

Browse files
author
Matthijs van Otterdijk
committed
print error help and exit when openai key is not provided but needed
1 parent c054690 commit c163ee5

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/main.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::io::ErrorKind;
22
use std::path::Path;
33

4+
use clap::CommandFactory;
45
use clap::{Parser, Subcommand, ValueEnum};
56
use hnsw::Hnsw;
67
use indexer::serialize_index;
@@ -94,8 +95,15 @@ enum DistanceVariant {
9495
}
9596

9697
fn key_or_env(k: Option<String>) -> String {
97-
k.or_else(|| std::env::var("OPENAI_KEY").ok())
98-
.expect("No OpenAI key given")
98+
let result = k.or_else(|| std::env::var("OPENAI_KEY").ok());
99+
if result.is_none() {
100+
let mut app = Args::command();
101+
eprintln!("Error: no OpenAI key given. Configure it with the OPENAI_KEY environment variable, or by passing in the --key argument");
102+
app.print_help().unwrap();
103+
std::process::exit(2);
104+
}
105+
106+
result.unwrap()
99107
}
100108

101109
#[tokio::main]

0 commit comments

Comments
 (0)