There's a lot of command tools out there written by developers to help them automate all sorts of tasks from code refactoring, linting, and builds. Node has become a popular tool to write reusable tools that can be run from the command line, but many CF developers don't realize that CommandBox has allowed developers to do this nativley in CFML for years now.

CLI Goodness

Writing a CLI tool sounds a little scary, but it's actually very easy-- as easy as a CFC containing a single method. Let's take a look at a real CLI command that you can run on CommandBox:


component {
    function run(){
        return 'Hello World!'; 

Hey, that's not so bad is it? By convention, the name of your command will be the same as the CFC name (hello) which means you'd run the above like so from any operating system:

$> box hello
Hello World!

Now you can add this command to Jenkins builds, package scripts, cron jobs, cfexecute, or just from your local bash shell!


But there's more you can do. A lot more. I'm a little biased, but I honestly think CommandBox is better than Node in several of these areas. Check out this cool stuff you can add into your new command line tools:

  1. Nest commands into names spaces like coldbox create app, coldbox create module, etc
  2. Accept named or positional parameters with no manual parsing or additonal libs needed.
  3. Users are automatically prompted for missing required params
  4. Easy boolean flags out-of-the-box like --force
  5. Super-easy ANSI formatting like font colors and decoration like print.blueText( 'sad panda' )
  6. Bullt-in tab completion for your command names and parameter name/values
  7. Automatic help screens generated right from your CFC's metadata
  8. Complete interactivity with your users via helper methods like ask(), confirm(), and waitForKey()
  9. Automatically send back proper exit codes to the OS when an error happens
  10. Nice helpers for finding current working directory, expanding relative paths, and getting terminal size
  11. Even access a database via on-the-fly JDBC connections.

Where do I start?

Want to play around with all this? 100% of what I mentioned above is documented right here in our docs:

That page will walk you through creating your first command in only a few minutes. And when you're ready to share your work with the world, just stick it in a GitHub repo and use the publish command to send it up to ForgeBox. Setting up some package scripts will even help manage all your versions. When I want to make changes to a command live, I just have to type this and my package scripts take care of tagging the repo, pushing to GitHub, and publishing the latest version to ForgeBox!

CommandBox> bump --minor


Some people like to learn from checking out existing projects. Feast your eyes on these example CommandBox modules that have custom commands in them:

Any of those projects above can be installed globally into your local CommandBox installation just like this:

CommandBox> install commandbox-chuck-norris

Get going

OK, you have the tools. You're a CF dev and it's time you put some of your totally aweseome ideas into action. Go and create some sweet stuff, then show your friends and watch their suprise when you tell them your new CLI tooling is written in ColdFusion!