1use clap::{arg, Parser};
2use faststr::FastStr;
3
4use clap_runner::{CallbackStatus, Context, Dispatcher, Error, RunnableCommand};
5use tokio::io::AsyncWriteExt;
6
7#[derive(Parser, Debug, Default)]
8#[command(name = "rt")]
9struct RootCommand {
10 #[arg(short, long)]
11 debug: bool,
12}
13
14#[async_trait::async_trait]
15impl RunnableCommand for RootCommand {}
16
17#[derive(Parser, Debug, Default)]
18#[command(name = "hi")]
19struct HiCommand {
20 #[arg(short, long)]
21 name: String,
22}
23
24#[async_trait::async_trait]
25impl RunnableCommand for HiCommand {
26 async fn callback(&self, cx: Context) -> Result<CallbackStatus, Error> {
27 cx.write_all(format!("hi {}", self.name).as_bytes())
28 .await
29 .map_err(|e| Error::Unknown(anyhow::anyhow!("write failed {e}")))?;
30 Ok(CallbackStatus::Abort {
31 reason: FastStr::new("done"),
32 })
33 }
34}
35
36#[tokio::main]
37async fn main() -> anyhow::Result<()> {
38 let dispatcher = Dispatcher::new(RootCommand::default())?.register(HiCommand::default())?;
39 let buf = vec![];
41 let w = tokio::io::BufWriter::new(buf);
42 let w = std::sync::Arc::new(tokio::sync::Mutex::new(w));
43 let _cx = dispatcher
44 .dispatch(&["rt", "--debug", "hi", "--name", "me"], w.clone())
45 .await?;
46 w.lock().await.flush().await?;
47 let output = String::from_utf8(w.lock().await.get_ref().to_owned())?;
48 debug_assert_eq!(output, "hi me");
49 println!("{}", output);
50 Ok(())
51}