The predecessor tool is covered in this original blog post, Build Thor CLI Project in Under a Second. It covers usage and also contains a video demo. An updated blog post will eventually be made, for now, refer to the original blog post.
The generated CLI project comes with:
- example passing specs
cli-template new mycli cd mycli exe/mycli hello world exe/mycli sub:goodbye world # namespaced command with colons
The above generated a starter CLI project called
mycli with a working hello command. The created project also has starter specs for you
$ rake Mycli::CLI mycli should hello world should goodbye world Finished in 1.12 seconds (files took 0.71706 seconds to load) 2 examples, 0 failures
Once you are satisfied with the CLI tool, you can release it as a gem.
1. edit the mycli.gemspec 2. edit lib/mycli/version.rb 3. update the CHANGELOG.md
When installed as a gem, you no longer have to prepend exe in front of the command. For example,
exe/mycli becomes the
Different starter templates
There are 2 different templates that the tool generates from:
- basic thor project: A standard CLI tool based on basic usage of Thor.
- colon namespaced project: The generated CLI project still uses Thor but it does some manipulation in order to allow defining namespaced methods with colons instead of spaces. Example:
mycli sub command.
To use the different templates:
TEMPLATE=colon_namespaces cli-template new mycli TEMPLATE=default cli-template new mycli cli-template new mycli # same as TEMPLATE=default
There is support for TAB completion in the newly generated CLI project for the default template. To enable auto completion, add this to
eval $(mycli completion_script)
Remember to re-load the shell. Note, the auto completion will only work if the cli command is avaialble in your PATH. You can do this by installing the gem. You can also create a wrapper bash script that calls to your development cli command like so:
cat > /usr/local/bin/mycli << 'EOL' #!/bin/bash exec ~/src/mycli/exe/mycli "$@" EOL chmod a+x /usr/local/bin/mycli
Experimental Auto Completion Support for colon_namespaces Template
The auto-completion for the colon_namespaces template CLI project is experimental. Ran into a few issues:
- Slow as to make auto-completion useless. This because the generated CLI invokes autoloading when it detects the methods for the completion words. Ideas on speeding this would be appreciated! Right now it takes about 1 second.
- Does not work with colons currently. Will have to look into this post Bash Command-Line Tab Completion Colon Character .
- Does not work with the last two characters are
Suggestions to are appreciated!
gem install cli-template
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am "Add some feature")
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request