Skip to content
A mixin for creating command line applications - gives an easy DSL for argument specification and processing
Ruby Cucumber
Find file
Latest commit 1e94387 @lamont-granquist lamont-granquist Merge pull request #21 from niku4i/missing-comma
Fix missing comma at the end of line in README



Mixlib::CLI provides a class-based command line option parsing object, like the one used in Chef, Ohai and Relish. To use in your project:

require 'rubygems'
require 'mixlib/cli'

class MyCLI
  include Mixlib::CLI

  option :config_file, 
    :short => "-c CONFIG",
    :long  => "--config CONFIG",
    :default => 'config.rb',
    :description => "The configuration file to use"

  option :log_level, 
    :short => "-l LEVEL",
    :long  => "--log_level LEVEL",
    :description => "Set the log level (debug, info, warn, error, fatal)",
    :required => true,
    :in => ['debug', 'info', 'warn', 'error', 'fatal'],
    :proc => { |l| l.to_sym }

  option :help,
    :short => "-h",
    :long => "--help",
    :description => "Show this message",
    :on => :tail,
    :boolean => true,
    :show_options => true,
    :exit => 0


# ARGV = [ '-c', 'foo.rb', '-l', 'debug' ]
cli =
cli.config[:config_file] # 'foo.rb'
cli.config[:log_level]   # :debug

If you are using this in conjunction with Mixlib::Config, you can do something like this (building on the above definition):

class MyConfig

  log_level   :info
  config_file "default.rb"

class MyCLI
  def run(argv=ARGV)

c =
# ARGV = [ '-l', 'debug' ]
MyConfig[:log_level] # :debug

Available arguments to 'option':


The short option, just like from optparse. Example: “-l LEVEL”


The long option, just like from optparse. Example: “–level LEVEL”


The description for this item, just like from optparse.


A default value for this option


Prints a message informing the user of the missing requirement, and exits. Default is false.


Set to :tail to appear at the end, or :head to appear at the top.


If this option takes no arguments, set this to true.


If you want the option list printed when this option is called, set this to true.


Exit your program with the exit code when this option is specified. Example: 0


If set, the configuration value will be set to the return value of this proc.


An array containing the list of accepted values

If you need access to the leftover options that aren't captured in the config, you can get at them through cli_arguments (referring to the above definition of MyCLI).

# ARGV = [ '-c', 'foo.rb', '-l', 'debug', 'file1', 'file2', 'file3' ]
cli =
cli.cli_arguments # [ 'file1', 'file2', 'file3' ]

New in 1.2.2

:required works, and we now support Ruby-style boolean option negation (e.g. '–no-cookie' will set 'cookie' to false if the option is boolean)

New in 1.2.0

We no longer destructively manipulate ARGV.

Have fun!

Something went wrong with that request. Please try again.