contrib: Add a sample IPython shell with magic commands #359
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Here's the sample IPython shell code, edited down to be standalone. The docstring should contain everything you need to know, and here's a little demo:
Sadly, the "ERROR: root:Invalid alias" thing can't be avoided, unless you fully disable logging, which I didn't want to do. But as you can see, you get a pretty standard Ipython shell, with the ability to run custom commands as %command. And, if the command is not found in the globals, then you can actually run it as "command" without the prefix. And IPython always gives you a range of other magic commands, as well as shell command access with
!
.The toy codebase I stripped it from actually had two CLIs:
crash()
. Thecrash()
global swapped into a new CLI, which would allow you to execute the commands shown here, or swap back to the drgn CLI. I would have added all of the fun bash-isms and piping features of crash, but I ended up coming to the conclusion that reimplementing Crash on top of drgn didn't seem like a great use of my time.That's why I didn't just write the commands as IPython magic class / magic functions. Also because I don't like the IPython magic API, but that's a separate story.