Blog

Brad Wood

March 02, 2017

Spread the word


Share your thoughts

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:

/commands/hello.cfc

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!

Interactivity

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: https://ortus.gitbooks.io/commandbox-documentation/content/developing/commands/developing_commands.html

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

https://ortus.gitbooks.io/commandbox-documentation/content/packages/creating_packages/creating_packages.html

Examples

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!

Recent Entries

Hackers demand a ransom to restore data from my ColdFusion web applications!

Hackers demand a ransom to restore data from my ColdFusion web applications!

Hackers demand a ransom to restore data from my ColdFusion web applications!

Unfortunately, we often hear this message from clients who thought it would never happen to them... until it did. Some believed they could delay the expense of Implementing ColdFusion security best practices for one year, while others were tempted to put it off for just a few months. However, in today's rapidly evolving digital landscape, the security of web applications, including ColdFusio...

Cristobal Escobar
Cristobal Escobar
April 16, 2024
Ortus March Newsletter

Ortus March Newsletter

Welcome to Ortus Solutions’ monthly roundup, where we're thrilled to showcase cutting-edge advancements, product updates, and exciting events! Join us as we delve into the latest innovations shaping the future of technology.

Maria Jose Herrera
Maria Jose Herrera
April 01, 2024
Into the Box 2024 Last Early Bird Days!

Into the Box 2024 Last Early Bird Days!

Time is ticking, with less than 60 days remaining until the excitement of Into the Box 2024 unfolds! Don't let this golden opportunity slip away; our exclusive Early Bird Pricing is here for a limited time only, available until March 31st. Why wait? Secure your seat now and take advantage of this steal!

Maria Jose Herrera
Maria Jose Herrera
March 20, 2024