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 <set>
8 #include <string>
9 #include <vector>
10 #include <filesystem>
11 #include "CLI/API/plugin.hpp"
12 #include "CLI/DynamicPlugin.hpp"
13 
14 namespace fs = std::filesystem;
15 
16 namespace M3{
27  public:
28  using MaCh3ArgumentParser::MaCh3ArgumentParser;
29  using MaCh3ArgumentParser::parse_args;
30 
32  void parse_args(int argc, const char *const argv[]);
33 
36  virtual ~MaCh3Program() = default;
37 
40  void add_core_module(IModule& module);
41 
43  void load_dynamic_plugins();
44 
46  int install_completions() const;
47 
51  void completions(const std::string& prefix, const std::vector<std::string>& context) const;
52 
55  int Run();
56 
57  private:
58  void validate_parser(const MaCh3ArgumentParser* parser) const;
59 
62  std::string detect_shell() const;
63 
68  bool write_file(const fs::path& path, std::string_view content) const;
69 
73  std::vector<std::string> expand_plugin_path(const std::string& path) const;
74 
75  private:
76  std::map<const MaCh3ArgumentParser*, IModule*> m_module_map;
77  std::set<std::string> m_attempted_plugin_paths; // keeps track of dynamic plugin paths that have been attempted for loading
78  std::map<const MaCh3ArgumentParser*, std::unique_ptr<DynamicPlugin>> m_dynamic_plugin_map;
79 
80  static constexpr std::string_view BASH_COMPLETION = R"(
81 _mach3_complete() {
82  local cur prev words cword
83  _init_completion || return
84 
85  # Pass current prefix + preceding words as context; binary filters by prefix
86  local -a raw
87  mapfile -t raw < <(mach3 --complete "$cur" "${words[@]:1:$((cword-1))}" 2>/dev/null)
88 
89  if [[ ${#raw[@]} -eq 0 ]]; then
90  # No completions from binary — fall back to default (file) completion
91  compopt -o default 2>/dev/null
92  return
93  fi
94  COMPREPLY=( "${raw[@]}" )
95 }
96 complete -F _mach3_complete mach3
97 )";
98  static constexpr std::string_view ZSH_COMPLETION = R"(
99 #compdef mach3
100 
101 _mach3() {
102  local cur="${words[$CURRENT]}"
103  local -a ctx
104  ctx=("${words[@]:1:$((CURRENT-2))}")
105 
106  local -a completions
107  completions=("${(@f)$(mach3 --complete "$cur" "${ctx[@]}" 2>/dev/null)}")
108 
109  if [[ ${#completions[@]} -eq 0 ]]; then
110  _files
111  return
112  fi
113  compadd -a completions
114 }
115 
116 _mach3
117 )";
118  };
119 }
Wrapper class for dynamically loaded plugins.
Interface for MaCh3 plugins and modules.
Definition: plugin.hpp:19
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.
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
void completions(const std::string &prefix, const std::vector< std::string > &context) const
Generate subcommand or long-option suggestions at the active parser depth.
static constexpr std::string_view BASH_COMPLETION
int install_completions() const
Install shell completion scripts for bash and zsh.
int Run()
Run the selected subcommand.
std::map< const MaCh3ArgumentParser *, IModule * > m_module_map
Map of parsers to core modules (non-owning)
std::set< std::string > m_attempted_plugin_paths
virtual ~MaCh3Program()=default
Destructor that unloads all dynamic plugins Fix plugin lifetime: shutdown-only unloading keeps borrow...
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 canonical shared-library paths.
void validate_parser(const MaCh3ArgumentParser *parser) const
void parse_args(int argc, const char *const argv[])
Parse args after inserting any default subcommands.
Main namespace for MaCh3 software.
Plugin interface and registration macros for MaCh3.