Trollop: Simple Yet Powerful Command Line Option Processor
Trollop is a command-line argument processing library for Ruby. Developer William Morgan says Trollop is "designed to provide the maximal amount of GNU-style argument processing in the minimum number of lines of code." It makes a refreshing change to the more popular, but generally scary, cmdparse. The homepage features some examples of its usage.
Once you've installed trollop with the usual gem install trollop, you could write:
require 'trollop' opts = Trollop::options do opt :http_1_0, "Force HTTP/1.0" opt :http_1_1, "Force HTTP/1.1" opt :hide_referer, "Hide referer", :default => true opt :connections, "Set number of simultaneous connections", :default => 2 end p opts
Running the script with no command line options would result in opt becoming:
{:http_1_0=>false, :http_1_1=>false, :hide_referer=>true, :connections=>2, :help=>false}
You also get a --help (or -h) option for free that describes how to use the options:
Options: --http-1-0, -h: Force HTTP/1.0 --http-1-1, -t: Force HTTP/1.1 --hide-referer, -i: Hide referer (default: true) --connections, -c : Set number of simultaneous connections (default: 2) --help, -e: Show this message
Note that trollop takes care of assigning the short-hand individual letter options, assigning the next letter within the string if the previous ones are taken.
Another option is Optiflag, which present a more DSL-esque solution. Its official homepage features some compelling examples, though the simplicity of Trollop appeals to me more for some reason.
Post supported by Brightbox: Brightbox is a specialist European Rails hosting company. Each Brightbox server includes an optimised Ruby on Rails stack, SAN storage and access to a managed MySQL database cluster. They also manage dedicated clusters for large scale Rails deployments. Click here to learn more...
July 7, 2008 at 7:31 pm
I've never used cmdparse (looks scary) but I have use getoptlong that is included in the stdlib for Ruby. Less scary looking then cmdparse it is still more of a pain then it needs to be.
Trollop seems exactly like what I think command-line argument processing should look like. Optiflag is nice also but I love the simplicity of Trollop. I feel like it basically turning ARGV into a hash. How much simpler can you get. Add the more advanced options (defaults, sub-options, etc) and you have everything you might need.
July 7, 2008 at 9:25 pm
Thanks! I'm always looking for some way to make commandline option parsing more easy :)
July 7, 2008 at 9:40 pm
If all you want is ARGV in a hash then use getopt-std or getopt-long (from the getopt library).
gem install getopt
July 8, 2008 at 12:10 am
I think that Yehuda Katz was also working on such a library called Thor:
http://github.com/wycats/thor/tree/master
It RubyConf, Gregory Brown lamented to Matz that we have 3 or 4 option parsers in the standard library. Yet people still want something better!
July 8, 2008 at 12:49 am
IMHO, Thor is awesome as it maps all your command line options directly to your methods. It handles all the delegation of the options and also allows different methods to require different sets of options. It also (rudimentarily) supports option typing.
It's more of a command-line framework than just an options parser, but I think its even easier to use than Trollop et al. and potentially very powerful.
July 8, 2008 at 3:56 pm
There is another super-simple library much like Trollop I've put together called Clip. The goal is to get away from the details of command-line parsing as quickly as you can. Check it out at https://github.com/alexvollmer/clip/tree.
July 9, 2008 at 8:36 am
Since I'm the author of cmdparse, I don't think that cmdparse is that scary :) And also, cmdparse itself isn't an option parsing library like trollop, it is just a wrapper around option parsing libraries.
All it does is that it adds support for specifying sub commands (or sub sub commands, ...). For example, you can have such command lines:
webgen -V 0 create -t project -s default new_site
where create is a sub command taking the -t and -s options.
July 9, 2008 at 1:21 pm
Thomas: I certainly don't mean any offense to your library, and it is clearly not scary to everyone since I have seen it used perhaps more than all the other libraries put together.
Perhaps there is room to put together an article covering the pros and cons of all of these various libraries in future so that developers can choose the one that matches with their expectations. For example, the sub command support you highlight could be important to one developer, whereas the "DSL approach" could be important to others. Thanks for highlighting that, as it might encourage others to check out your library if that's what they need!
July 9, 2008 at 2:48 pm
No offense taken, I just wanted to clarify the purpose of cmdparse :) If I implemented cmdparse now I would probably take another approach - I have reimplemented another one of my projects 5 times already, always with a complete change of the core functionality.
An overview of all command line parsing libraries would be cool since there are so many now.
July 11, 2008 at 9:41 pm
I've got a tiny library that I always use which takes a different approach.
Instead of focusing on all of the option parsing, I focus on a RubyGems-like CLI interface, giving you a way to quickly make command-line interfaces that act like `yourapp dosomething [args]` ... command listing and usage/help documentation is built in, but that's ALL. I leave you to your own devices for actually parsing options that're passed to commands (I'm perfectly happy using optparse).
SimpleCLI: http://github.com/remi/simplecli
It's pretty ridiculously simple - but it works great for me. It takes me about 60 seconds to get me up and running with a command line interface that's got useful help documentation and can list all of your commands and howto use them, etc etc.
Here's an example of how I commonly use it (in conjunction with optparse): http://github.com/remi/syntax-on/tree/master/lib/syntax-on/bin.rb
July 21, 2008 at 1:40 pm
Sounds interesting.... i've been using Choice which i've found to be pretty good. Will have to have a look at Trollop (if anything cos i like the name :P )