This is a ruby CLI skeleton using ripl.
- Add methods to
lib/commands.rb
- run
ruby-cli-skeleton
executable - read comments in code
- Clone repo
- run
bundle install
- Customize the
lib/commands.rb
file and (optionally) theruby-cli-skeleton
file.
ruby-cli-skeleton
has the base CLI which is all set up and ready to use, but lacks useful commands.- Some commands are already defined, like
help
,ls
, andpwd
. - The commands available in the CLI are the instance methods
on the
RubyCliSkeleton
class and any class it inherits from. By default, it inherits from theCommands
class inlib/commands.rb
- Commands are simple to write. You can print something, return some value (this is a REPL), or just return nil.
- Custom commands are written in
lib/commands.rb
- If renaming the class/file in
lib/commands.rb
, also change theCOMMANDS_CLASS
andCOMMANDS_FILE
constants inruby-cli-skeleton
. Make sure theinitialize
method onCOMMANDS_CLASS
accepts an options hash. OptionParser
can be used inruby-cli-skeleton
, read the docs for more info.- Add a method's name to
no_help_methods
to prevent thehelp
command from listing it - Customize the database in
lib/db_setup.rb
, including migrating and models. ATodo
ActiveRecord model is already defined.
- After customizing, open the
ruby-cli-skeleton
executable to start the program
What kind of helpers does this provide:
- The
help
command (with no arguments) will print out all the methods available to the CLI. It does this usinginstance_methods(false)
onRubyCliSkeleton
andCOMMANDS_CLASS
, so once a method is defined, it will be included here (unless it's included inno_help_methods
) - The
help
command (when given a method name as an argument) will print out the source code for the method. It does this using the method_source gem - awesome_print and colored are installed for pretty printing / colors.
- Some ripl plugins are installed.
ripl-shell_commands
enables any shell method to be run in the CLI by prepending a bang ("!"). For example,!echo true
will printtrue
.
Update:
- This now includes a sample ActiveRecord database setup, see
lib/db_setup.rb
Since this is a REPL, it can't be piped in Bash without issing it an exit command.
$ (echo "print 'hello world'"; echo "exit") | ./ruby-cli-skeleton | grep hello
This does a couple thigns:
- combines multiple
echo
calls into a single output by wrapping them in parens and separating them using semicolons. - issues ruby commands via
echo
, including one forexit
which is necessary. - pipes the
echo
to the cli script, and pipes the script output togrep
, searching the text.