class UserChoices::CommandLineSource

Treat the command line (including the arguments) as a source of choices.

Public Class Methods

new() click to toggle source
# File lib/user-choices/command-line-source.rb, line 13
def initialize
  super
  @parser = OptionParser.new
  @arglist_handler = NoArguments.new(self)
end

Public Instance Methods

add_help_line(string) click to toggle source

Add a single line composed of string to the current position in the help output.

# File lib/user-choices/command-line-source.rb, line 96
def add_help_line(string)
  @parser.separator(string)
end
help() click to toggle source

Called in the case of command-line error or explicit request (–help) to print usage information.

# File lib/user-choices/command-line-source.rb, line 32
def help
  $stderr.puts @parser
  exit
end
usage(*usage_lines) click to toggle source

The usage_lines will be used to produce the output from –help (or on error).

# File lib/user-choices/command-line-source.rb, line 25
def usage(*usage_lines) 
  help_banner(*usage_lines)
  self
end
uses_arg(choice) click to toggle source

The single argument required argument is turned into a string indexed by choice. Any other case is an error.

# File lib/user-choices/command-line-source.rb, line 83
def uses_arg(choice)
  use_strategy(choice, OneRequiredArg)
end
uses_arglist(choice) click to toggle source

Bundle up all non-option and non-switch arguments into an array of strings indexed by choice.

# File lib/user-choices/command-line-source.rb, line 77
def uses_arglist(choice)
  use_strategy(choice, ArbitraryArglist)
end
uses_option(choice, *args) click to toggle source

Describes how a particular choice is represented on the command line. The args are passed to OptionParser. Each arg will either describe one variant of option (such as "-s" or "--show VALUE") or is a line of help text about the option (multiple lines are allowed).

If the option takes an array of values, separate the values by commas: –files a,b,c There’s currently no way to escape a comma and no cleverness about quotes.

# File lib/user-choices/command-line-source.rb, line 49
def uses_option(choice, *args)
  external_names[choice] = '--' + extract_switch_raw_name(args)
  @parser.on(*args) do | value |
    self[choice] = value
  end
end
uses_optional_arg(choice) click to toggle source

If a single argument is present, it (as a string) is the value of choice. If no argument is present, choice has no value. Any other case is an error.

# File lib/user-choices/command-line-source.rb, line 90
def uses_optional_arg(choice)
  use_strategy(choice, OneOptionalArg)
end
uses_switch(choice, *args) click to toggle source

A switch is an option that doesn’t take a value. A switch described as "--switch" has these effects:

  • If it is not given, the choice is the default value or is not present in the hash that holds all the choices.

  • If it is given as --switch, the choice has the value "true". (If the choice was described in UserChoices::ChoicesBuilder#add_choice as having a :type => :boolean, that value is converted from a string to true.)

  • If it is given as --no-switch, the choice has the value "false".

# File lib/user-choices/command-line-source.rb, line 66
def uses_switch(choice, *args)
  external_name = extract_switch_raw_name(args)
  external_names[choice] = '--' + external_name
  args = change_name_to_switch(external_name, args)
  @parser.on(*args) do | value |
    self[choice] = value.to_s
  end
end