extensible method_missing() for Bash!
. : .,:::::::::::::::::: :: .: ... :::::::-.
;;,. ;;; ;;;;'''';;;;;;;;'''',;; ;;, .;;;;;;;. ;;, `';,
[[[[, ,[[[[, [[cccc [[ ,[[[,,,[[[ ,[[ \[[,`[[ [[
$$$$$$$$"$$$ $$"""" $$ "$$$"""$$$ $$$, $$$ $$, $$
888 Y88" 888o888oo,__ 88, 888 "88o"888,_ _,88P 888_,o8P'
MMM M' "MMM""""YUMMM MMM MMM YMM "YMMMMMP" MMMMP"`
.-:::::' ... ... ::::::. :::.:::::::-. :::::::...-:. ::-.
;;;'''' .;;;;;;;. ;; ;;;`;;;;, `;;; ;;, `';,;;;;``;;;;';;. ;;;;'
[[[,,==,[[ \[[,[[' [[[ [[[[[. '[[ `[[ [[ [[[,/[[[' '[[,[[['
`$$$"``$$$, $$$$$ $$$ $$$ "Y$c$$ $$, $$ $$$$$$c c$$"
888 "888,_ _,88P88 .d888 888 Y88 888_,o8P' 888b "88bo,,8P"`
"MM, "YMMMMMP" "YmmMMMM"" MMM YM MMMMP"` MMMM "W"mM"
# Method Missing for Bash!
MethodFoundry handles unrecognized shell input, allowing you to weld new behavior onto Bash!
If your bash command would result in a 'command not found' response, MethodFoundry tries hard to pair your input with any of its stored Methods. If a Method is found, MethodFoundry yields a replacement command to Bash for execution instead.
If multiple Methods are found, MethodFoundry will prompt you to select one.
Just about anything, really. MethodFoundry currently knows how to:
MethodFoundry is designed to be extensible. It is easy to add Methods to handle new input. It's Ruby, so you have its power and expressive syntax at your disposal.
Bash provides a mechanism to execute a shell function immediately prior to executing a shell command. This is done by trap()ing the DEBUG signal. This trap function tests the command about to be executed, and if it would result in a 'command not found' error, it invokes MethodFoundry. If MethodFoundry returns any text, this is evaluated by Bash instead of executing the original command.
Put these files where they make sense in your system, and run the methodfoundry.sh script to install it.
copy/pasting bare URLs to the command line should now just work. Additionally, specifying a full or relative path ending in '/' will 'cd' you to that directory.
MethodFoundry is Rake-aware. Assuming the output of rake -T:
> rake -T
rake about # List versions of all Rails frameworks and the environment
rake test # Runs test:units, test:functionals, test:integration
Then you can just say 'about' or 'test', and rake will be invoked accordingly.
See the file methods.rb for more examples.
I first encountered this awesome and crazy idea through Geoffrey Grosenbach's Peepcode Blog.
I was motivated to restructure his setup for the following reasons:
In order to meet my goals, I needed to have Bash eval() the output from MethodFoundry. This makes for a slightly more complex trap function, but I feel the tradeoff is worth it.
In the Happy Path, getting Bash to skip execution of the original command requires a hack involving setting the shell option 'extdebug', and returning a value >1. Per the BASH documentation, this simulates a RETURN trap.
(The MIT License)
Copyright © 2012 Erich Smith
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.