origen/commands/
aux_cmds.rs

1use super::_prelude::*;
2use super::launch_as;
3use crate::framework::aux_cmds::{add_aux_ns_helps, aux_ns_subcmd};
4use crate::framework::{build_path, AuxCmds};
5use indexmap::IndexMap;
6
7pub const BASE_CMD: &'static str = "auxillary_commands";
8
9pub(crate) fn add_helps(helps: &mut CmdHelps, aux_cmds: &AuxCmds) {
10    helps
11        .add_core_cmd(BASE_CMD)
12        .set_help_msg("Interface with auxillary commands")
13        .set_as_not_extendable();
14    add_aux_ns_helps(helps, aux_cmds);
15}
16
17pub(crate) fn add_commands<'a>(
18    app: App,
19    helps: &'a CmdHelps,
20    aux_commands: &'a AuxCmds,
21    exts: &'a Extensions,
22) -> Result<App> {
23    let mut aux_sub = helps
24        .core_cmd(BASE_CMD)
25        .visible_alias("aux_cmds")
26        .arg_required_else_help(true);
27    aux_sub = aux_ns_subcmd(aux_sub, helps, aux_commands, exts)?;
28    Ok(app.subcommand(aux_sub))
29}
30
31pub(crate) fn run(
32    cmd: &clap::ArgMatches,
33    mut app: &clap::Command,
34    exts: &crate::Extensions,
35    plugins: Option<&crate::Plugins>,
36    aux_cmds: &crate::AuxCmds,
37) -> origen::Result<()> {
38    if let Some(subc) = cmd.subcommand() {
39        let ns_ins = aux_cmds.namespaces.get(subc.0).expect(&format!(
40            "Expected auxillary command namespace '{}' to be present, but was not found",
41            subc.0
42        ));
43        let path = build_path(&subc.1)?;
44
45        let mut overrides = IndexMap::new();
46
47        let mut matches = subc.1;
48        let mut path_pieces: Vec<String> = vec![];
49        app = app.find_subcommand(BASE_CMD).unwrap();
50        app = app.find_subcommand(subc.0).unwrap();
51        while matches.subcommand_name().is_some() {
52            let n = matches.subcommand_name().unwrap();
53            matches = matches.subcommand_matches(&n).unwrap();
54            app = app.find_subcommand(n).unwrap();
55            path_pieces.push(n.to_string());
56        }
57
58        launch_as(
59            "_dispatch_aux_cmd_",
60            Some(&path_pieces),
61            matches,
62            app,
63            exts.get_aux_ext(subc.0, &path),
64            plugins,
65            Some({
66                overrides.insert(
67                    "dispatch_root".to_string(),
68                    Some(format!("r'{}'", ns_ins.root().display())),
69                );
70                overrides.insert("dispatch_src".to_string(), Some(format!("r'{}'", subc.0)));
71                overrides
72            }),
73        );
74        Ok(())
75    } else {
76        // This case shouldn't happen as any non-valid command should be
77        // caught previously by clap and a non-command invocation should
78        // print the help message.
79        unreachable!("Expected an AUX command but none was found!");
80    }
81}