Build Thor CLI Project in Under a Second

Tung Nguyen
BoltOps

--

We’ll build a CLI project that is based on Thor in under a second.

thor_template is a tool that builds a fully functional CLI command based on Thor. The commands immediately work and there are even specs.

Quick Start

Summary of commands so you can copy and paste:

gem install thor_template
thor_template new mycli
cd mycli
bin/mycli help
bin/mycli hello world
rake

A Little More Details

Commands with a little more detailed explaination. Install and generate the project:

gem install thor_template
thor_template new mycli
cd mycli

What the generated directory structure looks like:

$ tree
.
├── Gemfile
├── Gemfile.lock
├── Guardfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── bin
│ └── mycli
├── lib
│ ├── mycli
│ │ ├── cli
│ │ │ └── help.rb
│ │ ├── cli.rb
│ │ ├── command.rb
│ │ └── version.rb
│ ├── mycli.rb
│ └── thor_template
├── mycli.gemspec
└── spec
├── lib
│ └── cli_spec.rb
└── spec_helper.rb
7 directories, 15 files

Default help menu provided:

$ bin/mycli help
Commands:
mycli hello NAME # say hello to NAME
mycli help [COMMAND] # Describe available commands or one specific command
Options:
[--verbose], [--no-verbose]
[--noop], [--no-noop]
$

A built-in starter command provided:

$ bin/mycli hello tung
Hello tung
$

Spec output:

$ rake spec
Mycli::CLI
mycli
should hello world
Finished in 0.10423 seconds (files took 0.29091 seconds to load)
1 example, 0 failures
$

Hope you enjoyed this post 🎊

Thanks for reading this far. If you found this post useful, I’d really appreciate it if you recommend this post (by clicking the clap button) so others can find it too! Also, follow me on Medium.

P.S. Be sure to join the BoltOps newsletter to receive free DevOps tips and updates.

Originally published at blog.boltops.com on September 14, 2017.

--

--