Access grav instance in cli

How can I access the equivalent of $this->grav in a plugin, in a cli script?
I tried self::getGrav(), but I get a weird error if I try to use this object:

Fatal error: Call to a member function get() on a non-object in /Volumes/FILES/mygrav/system/src/Grav/Common/Debugger.php on line 42

You need to have a use GravTrait; statement in your class to make that available.

Also you could use use ConsoleTrait; that is used by GPM. This has the GravTrait, but also sets up some useful stuff for working with the CLI: https://github.com/getgrav/grav/blob/develop/system/src/Grav/Console/ConsoleTrait.php

This is what the core CLI stuff uses via ConsoleCommand.

I’m using ConsoleTrait now, but the error persists. This line in cache.php seems to be the problem, when I comment it out, the problem goes away:

// Dump Cache state
$grav[‘debugger’]->addMessage(‘Cache: [’ . ($this->enabled ? ‘true’ : ‘false’) . ‘] Driver: [’ . $this->driver_name . ‘]’);

My cache is overridden though, I’ve extended cache.php, this is the code:

namespace Grav\Plugin;

use \Doctrine\Common\Cache\Cache as DoctrineCache;
use Grav\Common\Config\Config;
use Grav\Common\Filesystem\Folder;

/**
*
*/
class OverrideCache extends \Grav\Common\Cache
{

/** 
 *
 * Set static variables in parent class cache.php
 *
 */
public function setCacheRemove($caches)
{

    if(!empty($caches)){
      parent::$standard_remove = $caches;
    }
    else{

      parent::$standard_remove = [
          'cache://twig/',
          'cache://doctrine/',
          'cache://compiled/',
          'cache://validated-',
          'asset://',
      ];
    }
}

/**
 *
 * Prevent images from being flushed on any cache flush
 *
 */
public function overrideAllCaches($caches)
{
    if(!empty($caches)){
      parent::$standard_remove =
      parent::$all_remove =
      parent::$assets_remove =
      parent::$images_remove =
      parent::$cache_remove =

      $caches;

    }
    else{

      parent::$standard_remove =
      parent::$all_remove =
      parent::$assets_remove =
      parent::$images_remove =
      parent::$cache_remove =

      [
          'cache://twig/',
          'cache://doctrine/',
          'cache://compiled/',
          'cache://validated-',
          'asset://',
      ];
    }

}

}

Seems to be the get method on the config object in Grav\Common\Debugger.php that spawns this error. So in debugger.php my grav object doesn’t seem to have the config object, but when I dump it in my console it’s present.

makes no difference how I use getGrav(), same error:
$grav = self::getGrav();
$grav = $this->getGrav();

It would be easier to read your code if you pasted it in a markdown code block (see the ? for help in the input box on the forum)

Anyway this issue is probably because of the order things are initialized within Grav. The cache is initialized and created pretty early on in the order of things.

By the time any plugin is even loaded. Cache has been created with the core Grav one. You may be replacing it, but only after onPluginsInitialized() has run. Before that point, the regular cache will be used.