@@ -4268,6 +4268,53 @@ int cmd_self_version(const mcpplibs::cmdline::ParsedArgs& /*parsed*/) {
42684268 return 0 ;
42694269}
42704270
4271+ int cmd_self_init (const mcpplibs::cmdline::ParsedArgs& parsed) {
4272+ bool force = parsed.is_flag_set (" force" );
4273+
4274+ if (force) {
4275+ // --force: delete registry (sandbox) + caches and re-bootstrap.
4276+ // Preserves: bin/mcpp (self-contained mode), config.toml, log/.
4277+ mcpp::ui::info (" Resetting" , " mcpp sandbox (registry, caches)" );
4278+
4279+ // Resolve MCPP_HOME without running bootstrap (which may fail).
4280+ std::filesystem::path home;
4281+ if (auto * e = std::getenv (" MCPP_HOME" ); e && *e)
4282+ home = e;
4283+ else {
4284+ const char * userHome = nullptr ;
4285+ #if defined(_WIN32)
4286+ userHome = std::getenv (" USERPROFILE" );
4287+ #endif
4288+ if (!userHome) userHome = std::getenv (" HOME" );
4289+ if (userHome) home = std::filesystem::path (userHome) / " .mcpp" ;
4290+ }
4291+ if (!home.empty ()) {
4292+ std::error_code ec;
4293+ std::filesystem::remove_all (home / " registry" , ec);
4294+ std::filesystem::remove_all (home / " bmi" , ec);
4295+ std::filesystem::remove_all (home / " cache" , ec);
4296+ }
4297+ }
4298+
4299+ // (Re-)run the full load_or_init, which does bootstrap.
4300+ mcpp::ui::info (" Initializing" , " mcpp sandbox" );
4301+ auto cfg = mcpp::config::load_or_init ();
4302+ if (!cfg) {
4303+ mcpp::ui::error (cfg.error ().message );
4304+ return 1 ;
4305+ }
4306+
4307+ // Verify result.
4308+ auto problem = mcpp::config::check_base_init (*cfg);
4309+ if (!problem.empty ()) {
4310+ mcpp::ui::error (std::format (" init incomplete: {}" , problem));
4311+ return 1 ;
4312+ }
4313+
4314+ mcpp::ui::status (" Ready" , " sandbox initialized" );
4315+ return 0 ;
4316+ }
4317+
42714318std::string upper_ascii (std::string s) {
42724319 for (char & ch : s) {
42734320 if (ch >= ' a' && ch <= ' z' ) ch = static_cast <char >(ch - ' a' + ' A' );
@@ -4635,6 +4682,10 @@ int run(int argc, char** argv) {
46354682 // ─── about mcpp itself ─────────────────────────────────────────
46364683 .subcommand (cl::App (" self" )
46374684 .description (" Inspect and manage mcpp itself" )
4685+ .subcommand (cl::App (" init" )
4686+ .description (" Initialize or repair mcpp sandbox" )
4687+ .option (cl::Option (" force" )
4688+ .help (" Delete registry and re-initialize from scratch" )))
46384689 .subcommand (cl::App (" doctor" )
46394690 .description (" Diagnose mcpp environment health" ))
46404691 .subcommand (cl::App (" env" )
@@ -4650,11 +4701,12 @@ int run(int argc, char** argv) {
46504701 .arg (cl::Arg (" code" ).help (" Error code such as E0001" ).required ()))
46514702 .action (wrap_rc ([&dispatch_sub](const cl::ParsedArgs& p) {
46524703 return dispatch_sub (" self" , p, {
4653- {" doctor" , cmd_doctor},
4654- {" env" , cmd_env},
4655- {" config" , cmd_self_config},
4656- {" version" , cmd_self_version},
4657- {" explain" , cmd_explain_action},
4704+ {" init" , cmd_self_init},
4705+ {" doctor" , cmd_doctor},
4706+ {" env" , cmd_env},
4707+ {" config" , cmd_self_config},
4708+ {" version" , cmd_self_version},
4709+ {" explain" , cmd_explain_action},
46584710 });
46594711 })))
46604712
0 commit comments