Sha256: 2d37dd9db41455c102ff099c427318ff156c07c7759510de274bd9276b5495f8

Contents?: true

Size: 1.44 KB

Versions: 5

Compression:

Stored size: 1.44 KB

Contents

### The following adjusts the load path so that the correct version of
### a self-contained package is found, no matter where the script is
### run from. 
require 'pathname'
$:.unshift((Pathname.new(__FILE__).parent.parent.parent + 'lib').to_s)
require 's4t-utils/load-path-auto-adjuster'


require 'pp'
require 'user-choices'

class SwitchExample < UserChoices::Command

  def add_sources(builder)
    builder.add_source(UserChoices::CommandLineSource, :usage,
                       "Usage: ruby #{$0} [options] args...",
                       "There may be 2-4 arguments.")
    
  end

  # Switches are slightly different than options. (The difference is
  # in how they're invoked, as either --switch or --no-switch.) Almost
  # certainly, you want the switch to be of type :boolean and have a
  # default.
  def add_choices(builder)
    builder.add_choice(:switch,
                       :default => false,
                       :type => :boolean) { | command_line |
      command_line.uses_switch("--switch", "-s")
    }

    # You control the allowable length of a choice with the :length
    # keyword argument. It applies to command-line arglists, lists given
    # in configuration files, and the like.
    builder.add_choice(:args, :length => 2..4) { | command_line |
      command_line.uses_arglist
    }
  end

  def execute
    pp @user_choices
  end
end


if $0 == __FILE__
  S4tUtils.with_pleasant_exceptions do
    SwitchExample.new.execute
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
user-choices-1.1.0 examples/older/switches.rb
user-choices-1.1.2 examples/older/switches.rb
user-choices-1.1.1 examples/older/switches.rb
user-choices-1.1.3 examples/older/switches.rb
user-choices-1.1.4 examples/older/switches.rb