Skip to content

Instantly share code, notes, and snippets.

@bigeasy
Created March 6, 2012 12:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigeasy/1986123 to your computer and use it in GitHub Desktop.
Save bigeasy/1986123 to your computer and use it in GitHub Desktop.
Swipe me: CoffeeScript command line parser boilerplate.

I live in a bash environment. I create small programs frequently, in accordance with the UNIX way. I'm often creating command line programs. These days I'm often creating them in CoffeeScript.

I'd been using CoffeeScript's own OptionParse to date, but found myself wanting to distribute CoffeeScript compiled to JavaScript, without a dependency on CoffeeScript at runtime. So long as I was shedding dependencies, I didn't want to add a dependency on a command line parser library.

This file represents a swipe at a command line parser in CoffeeScript, that I can swipe for my command line programs when

[ foo, bar ] = process.argv.slice(2)

is no longer sufficient.

A command line library is useful for bash or C, where parsing the command line is a challenge, but in CoffeeScript, with regular expressions handy, it is pretty easy to chew through a command line, and do the right thing.

Because if your program is just a doodle, the command line interface doesn't need to be more than a doodle. If your program is meant to be a lean, mean command line machine, then your command line interface is too important to leave to a command line interface library mixin. This gist is a doodle to expand upon.

If you can read this, gists from git are working.

opts = """
usage: ./frobinate.coffee [options] source destination
options:
-p, --processes [count] number of processes to run
-d, --directory [path] alternate working directory
-v, --verbose enable chatty output
"""
usage = ->
console.log opts
process.exit 1
[ options, argv ] = do ->
[ options, flag, full, argv ] = [ {}, {}, {}, process.argv.slice(2) ]
converter =
count: (name, next) -> not isNaN(@[name] = parseInt next, 10)
path: (name, next) -> @[name] = next
none: (name) -> (@[name] = not @[name])?
for opt in opts.split /\n/
if match = ///
\s*(?:(-\w),)? # short option
\s*(--\w+) # long option
\s*(?:\[([^\]]+)\])? # parameter
///.exec(opt)
[ short, long, param ] = match[1..]
flag[short] = flag[long] = converter[param]
full[short] = long
while conv = flag[argv[0]]
name = argv.shift()
name = (full[name] or name).substring(2)
length = conv.length - 1
usage() if argv.length < length
usage() unless conv.apply options, [ name ].concat(argv.splice(0, length))
[ options, argv ]
console.log { options, argv }
The MIT License
Copyright (c) 2012 Alan Gutierrez
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment