I am building a CLI tool which will potentially support many commands. Ideally, I want to abstract out each command to implement an interface that demands a "run" method. From there on, it would be a simple regex match that delegates to the appropriate command class.
The issue is, not all of these commands are stateless. One command may modify program state in a way that a future command relies on. For example, a command will build a search index, that a subsequent command uses.
I have a main CLI class handling the regex matching, and independent "Command" classes implementing the RunnableCommand interface.
Is it reasonable to pass the instance of the CLI class to individual "Command" instances in order to mutate state? (whether by getters/setters or by making CLI fields public). In C++ I would use a friend class for the commands, but that is not available here.