MaCh3  2.6.1
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{
16 
27  public:
28  using MaCh3ArgumentParser::MaCh3ArgumentParser;
29 
31  virtual ~MaCh3Program(){
32  //std::cout<<"UNLOADED"<< std::endl;
33  this->unload_dynamic_plugins();
34  }
35 
38  void add_core_module(IModule& module);
39 
41  void load_dynamic_plugins();
42 
44  void install_completions() const;
45 
48  void completions(const std::string& prefix) 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  COMPREPLY=( $(mach3 --complete "$cur" "${words[@]:1:$cword}") )
83 }
84 complete -F _mach3_complete mach3
85 )";
86  static constexpr std::string_view ZSH_COMPLETION = R"(
87 #compdef mach3
88 
89 local -a completions
90 local cur="$words[-1]"
91 completions=("${(@f)$(mach3 --complete "$cur" "${words[@]:1}")}")
92 compadd -a completions
93 )";
94  };
95 }
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 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)
void completions(const std::string &prefix) const
Generate completion suggestions for the given prefix.
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.