This is a ruby CLI skeleton using ripl.
- Add methods to
lib/commands.rb - run
ruby-cli-skeletonexecutable - read comments in code
- Clone repo
- run
bundle install - Customize the
lib/commands.rbfile and (optionally) theruby-cli-skeletonfile.
ruby-cli-skeletonhas 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
RubyCliSkeletonclass and any class it inherits from. By default, it inherits from theCommandsclass 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_CLASSandCOMMANDS_FILEconstants inruby-cli-skeleton. Make sure theinitializemethod onCOMMANDS_CLASSaccepts an options hash. OptionParsercan be used inruby-cli-skeleton, read the docs for more info.- Add a method's name to
no_help_methodsto prevent thehelpcommand from listing it - Customize the database in
lib/db_setup.rb, including migrating and models. ATodoActiveRecord model is already defined.
- After customizing, open the
ruby-cli-skeletonexecutable to start the program
What kind of helpers does this provide:
- The
helpcommand (with no arguments) will print out all the methods available to the CLI. It does this usinginstance_methods(false)onRubyCliSkeletonandCOMMANDS_CLASS, so once a method is defined, it will be included here (unless it's included inno_help_methods) - The
helpcommand (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_commandsenables any shell method to be run in the CLI by prepending a bang ("!"). For example,!echo truewill 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 helloThis does a couple thigns:
- combines multiple
echocalls into a single output by wrapping them in parens and separating them using semicolons. - issues ruby commands via
echo, including one forexitwhich is necessary. - pipes the
echoto the cli script, and pipes the script output togrep, searching the text.