subspace_board_command/
cli.rs1use crate::{execute, fail, root, tree};
2use std::process::ExitCode;
3use subspace_agent_tools::{Catalog, ToolEnvironment};
4use subspace_product::ProductContext;
5use usage::parse::{ParseOutput, ParseValue};
6
7pub fn run(
9 context: &ProductContext,
10 boards: &[subspace_mcp::Board],
11 environment: &ToolEnvironment,
12 args: &[String],
13) -> ExitCode {
14 match subspace_mcp::served_catalog(boards, &[], environment) {
15 Ok(catalog) => run_catalog(context, &catalog, args),
16 Err(error) => fail(&error, 1),
17 }
18}
19
20pub(crate) fn commands(
21 context: &ProductContext,
22 catalog: &Catalog,
23) -> Result<tree::Commands, String> {
24 tree::Commands::new(context, catalog, root::tree)
25}
26
27pub(crate) fn run_catalog(
28 context: &ProductContext,
29 catalog: &Catalog,
30 args: &[String],
31) -> ExitCode {
32 let generated = match commands(context, catalog) {
33 Ok(generated) => generated,
34 Err(error) => return fail(&error, 1),
35 };
36 if let Some(words) = args.strip_prefix(&[root::COMPLETE_WORD.into(), "--".into()]) {
37 for word in root::complete_word(&generated.spec, words) {
38 println!("{word}");
39 }
40 return ExitCode::SUCCESS;
41 }
42 if args.len() == 1
43 && generated
44 .routes
45 .iter()
46 .any(|route| route.path.len() == 2 && route.path[0] == args[0])
47 {
48 return parse_and_run(context, &generated, &[args[0].clone(), "--help".into()]);
49 }
50 parse_and_run(context, &generated, args)
51}
52
53fn parse_and_run(context: &ProductContext, commands: &tree::Commands, args: &[String]) -> ExitCode {
54 let argv: Vec<String> = std::iter::once(commands.spec.bin.clone())
55 .chain(args.iter().cloned())
56 .collect();
57 let output = match usage::parse::Parser::new(&commands.spec).explain(&argv) {
58 Ok(output) => output,
59 Err(error) => return fail(&error.to_string(), 2),
60 };
61 if let Some(page) = output.errors.iter().find(|error| {
62 matches!(
63 error,
64 usage::error::UsageErr::Help(_) | usage::error::UsageErr::Version(_)
65 )
66 }) {
67 print!("{page}");
68 return ExitCode::SUCCESS;
69 }
70 if !output.errors.is_empty() {
71 for error in &output.errors {
72 eprintln!("{error}");
73 }
74 return ExitCode::from(2);
75 }
76 let path: Vec<&str> = output
77 .cmds
78 .iter()
79 .skip(1)
80 .map(|cmd| cmd.name.as_str())
81 .collect();
82 if path == [root::COMPLETION] {
83 return completion(context, &output);
84 }
85 match commands.routes.iter().find(|route| {
86 route
87 .path
88 .iter()
89 .map(String::as_str)
90 .eq(path.iter().copied())
91 }) {
92 Some(route) => execute(route, &output),
93 None => fail(&format!("Unknown command: {}", args.join(" ")), 2),
94 }
95}
96
97fn completion(context: &ProductContext, output: &ParseOutput) -> ExitCode {
98 let shell = output.args.iter().find_map(|(arg, value)| match value {
99 ParseValue::String(shell) if arg.name == "shell" => Some(shell.as_str()),
100 _ => None,
101 });
102 match shell.and_then(|shell| root::init_script(context.product.binary_name.as_str(), shell)) {
103 Some(script) => {
104 print!("{script}");
105 ExitCode::SUCCESS
106 }
107 None => fail("Choose a completion shell: bash, zsh or fish.", 2),
108 }
109}