void pragma_environment_add::run( const data::command::iterator& args_begin_, const data::command::iterator& args_end_, iface::displayer& displayer_ ) const { const std::string cmd = tokens_to_string(args_begin_, args_end_); _shell.store_in_buffer(cmd, displayer_); if (is_environment_setup_command(args_begin_, args_end_)) { displayer_.show_comment( data::text( "You don't need the environment add pragma to add this to the" " environment. The following command does this as well:" ) ); displayer_.show_cpp_code(cmd); } else { displayer_.show_comment( data::text( "Metashell (incorrectly) thinks that this command should execute a" " metaprogram and would not add it to the environment without using" " the \"environment add\" pragma. Please file a bug report containing" " this command (" + cmd + ") at" " https://github.com/sabel83/metashell/issues. Thank you." ) ); } }
void pragma_switch::run(const data::command::iterator&, const data::command::iterator&, const data::command::iterator& args_begin_, const data::command::iterator& args_end_, iface::displayer& displayer_) const { auto i = args_begin_; if (i != args_end_) { const std::string v = i->value().value(); if (valid_argument(v)) { ++i; if (i == args_end_) { _update(element_of(true_values, v)); } else { displayer_.show_error("Invalid arguments after " + v + ": " + tokens_to_string(i, args_end_).value()); } } else { displayer_.show_error("Invalid argument " + v + ". Valid values are: " + valid_arguments()); } } displayer_.show_comment( data::text(_name + " is " + (_query() ? "on" : "off"))); }
void pragma_config_load::run(const data::command::iterator&, const data::command::iterator&, const data::command::iterator& args_begin_, const data::command::iterator& args_end_, iface::displayer& displayer_) const { const data::shell_config_name name = data::shell_config_name( tokens_to_string(args_begin_, args_end_).value()); const auto& configs = _shell.get_config().shell_configs(); const auto cfg = std::find_if( configs.begin(), configs.end(), [&name](const data::shell_config& cfg_) { return cfg_.name == name; }); if (cfg == configs.end()) { throw exception("Config " + name + " not found."); } else { const auto old_config = _shell.get_config().active_shell_config().name; _shell.get_config().activate(name); try { _shell.rebuild_environment(); displayer_.show_comment(data::text("Switched to config " + name)); } catch (const std::exception& e) { displayer_.show_error("Error loading config " + name + ": " + e.what()); restore_config(old_config, _shell); } catch (...) { displayer_.show_error("Error loading config " + name + ": unknown exception"); restore_config(old_config, _shell); } } }
void pragma_help::run(const data::command::iterator&, const data::command::iterator&, const data::command::iterator& args_begin_, const data::command::iterator& args_end_, iface::displayer& displayer_) const { using boost::algorithm::join; if (args_begin_ == args_end_) { display_all(displayer_, _pragma_handlers); } else { std::vector<std::string> args; for (auto i = args_begin_; i != args_end_; ++i) { switch (i->category()) { case data::token_category::whitespace: case data::token_category::comment: // skip token break; default: args.push_back(i->value().value()); } } data::text help_text; bool was_pragma = false; for (const auto& h : _pragma_handlers) { if (prefix_of(args.begin(), args.end(), h.first.begin(), h.first.end())) { if (was_pragma) { help_text.paragraphs.push_back(data::paragraph("")); } else { was_pragma = true; } const std::string p_args = h.second.arguments(); help_text.paragraphs.push_back( data::paragraph("#msh " + join(h.first, " ") + (p_args.empty() ? std::string() : " " + p_args))); help_text.paragraphs.push_back( data::paragraph(h.second.description(), " ")); } } if (was_pragma) { displayer_.show_comment(help_text); } else { displayer_.show_error("Pragma " + join(args, " ") + " not found."); } } }