cli-template
is a generator tool that builds a starter CLI project based on Thor. It is the successor tool to thor_template, which is also a tool that generates CLI projects.
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
- auto-completion
cli-template new mycli
cd mycli
exe/mycli hello world
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
Use the --subcommand
to have the generator also include a subcommand example.
cli-template new mycli --subcommand
cd mycli
exe/foo sub goodbye # subcommand example
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
And run:
rake release
When installed as a gem, you no longer have to prepend exe in front of the command. For example, exe/mycli
becomes the mycli
command.
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
vsmycli 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 ~/.bashrc
or ~/.profile
:
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
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