MaCh3  2.8.0
Reference Guide
MaCh3Program.hpp
Go to the documentation of this file.
1 
4 #pragma once
5 #include <map>
6 #include <memory>
7 #include <string>
8 #include <vector>
9 #include <filesystem>
10 #include "CLI/API/plugin.hpp"
11 #include "CLI/DynamicPlugin.hpp"
12 
13 namespace fs = std::filesystem;
14 
15 namespace M3{
26  public:
27  using MaCh3ArgumentParser::MaCh3ArgumentParser;
28 
30  virtual ~MaCh3Program(){
31  //std::cout<<"UNLOADED"<< std::endl;
32  this->unload_dynamic_plugins();
33  }
34 
37  void add_core_module(IModule& module);
38 
40  void load_dynamic_plugins();
41 
43  void install_completions() const;
44 
48  void completions(const std::string& prefix, const std::vector<std::string>& context) const;
49 
52 
55  int Run();
56 
57  private:
60  std::string detect_shell() const;
61 
66  bool write_file(const fs::path& path, std::string_view content) const;
67 
71  std::vector<std::string> expand_plugin_path(const std::string& path) const;
72 
73  private:
74  std::vector<std::string> m_subcommands;
75  std::map<const MaCh3ArgumentParser*, IModule*> m_module_map;
76  std::map<const MaCh3ArgumentParser*, std::unique_ptr<DynamicPlugin>> m_dynamic_plugin_map;
77 
78  static constexpr std::string_view BASH_COMPLETION = R"(
79 _mach3_complete() {
80  local cur prev words cword
81  _init_completion || return
82 
83  # Pass current prefix + preceding words as context; binary filters by prefix
84  local -a raw
85  mapfile -t raw < <(mach3 --complete "$cur" "${words[@]:1:$((cword-1))}" 2>/dev/null)
86 
87  if [[ ${#raw[@]} -eq 0 ]]; then
88  # No completions from binary — fall back to default (file) completion
89  compopt -o default 2>/dev/null
90  return
91  fi
92  COMPREPLY=( "${raw[@]}" )
93 }
94 complete -F _mach3_complete mach3
95 )";
96  static constexpr std::string_view ZSH_COMPLETION = R"(
97 #compdef mach3
98 
99 _mach3() {
100  local cur="${words[$CURRENT]}"
101  local -a ctx
102  ctx=("${words[@]:1:$((CURRENT-2))}")
103 
104  local -a completions
105  completions=("${(@f)$(mach3 --complete "$cur" "${ctx[@]}" 2>/dev/null)}")
106 
107  if [[ ${#completions[@]} -eq 0 ]]; then
108  _files
109  return
110  fi
111  compadd -a completions
112 }
113 
114 _mach3
115 )";
116  };
117 }
Wrapper class for dynamically loaded plugins.
Interface for MaCh3 plugins and modules.
Definition: plugin.hpp:16
Extended ArgumentParser with MaCh3-specific functionality.
Definition: m3argparse.hpp:17
Main program class that manages modules, plugins, and command-line parsing.
void add_core_module(IModule &module)
Add a core module to the program.
virtual ~MaCh3Program()
Destructor that unloads all dynamic plugins.
void unload_dynamic_plugins()
Unload all dynamically loaded plugins.
bool write_file(const fs::path &path, std::string_view content) const
Write content to a file, creating parent directories if needed.
static constexpr std::string_view ZSH_COMPLETION
std::vector< std::string > m_subcommands
List of registered subcommand names.
void completions(const std::string &prefix, const std::vector< std::string > &context) const
Generate completion suggestions given the current prefix and preceding words.
void install_completions() const
Install shell completion scripts for bash and zsh.
static constexpr std::string_view BASH_COMPLETION
int Run()
Run the selected subcommand.
std::map< const MaCh3ArgumentParser *, IModule * > m_module_map
Map of parsers to core modules (non-owning)
std::map< const MaCh3ArgumentParser *, std::unique_ptr< DynamicPlugin > > m_dynamic_plugin_map
Map of parsers to dynamic plugins (owning)
std::string detect_shell() const
Detect the user's shell from the SHELL environment variable.
void load_dynamic_plugins()
Load dynamic plugins from the MACH3_PLUGINS environment variable.
std::vector< std::string > expand_plugin_path(const std::string &path) const
Expand a plugin path (file or directory) into a list of .so files.
Main namespace for MaCh3 software.
Plugin interface and registration macros for MaCh3.