Module: Miniparse

Defined in:
lib/miniparse.rb,
lib/miniparse.rb,
lib/miniparse/parser.rb,
lib/miniparse/command.rb,
lib/miniparse/control.rb,
lib/miniparse/version.rb,
lib/miniparse/commander.rb,
lib/miniparse/option_broker.rb

Defined Under Namespace

Classes: Command, Commander, FlagOption, Option, OptionBroker, Parser, SwitchOption

Constant Summary collapse

ERR_HELP_REQ =

module ErrorCodes

1
ERR_ARGUMENT =
2
DEFAULT_CONTROLS =
{
# gives an error if there is an unrecognized option either short or long
# (if not then passes them as arguments) 
raise_on_unrecognized: true, 

# intercepts .parse ArgumentError (i.e. the commandline user introduced 
# wrong or invalid options) and exits with a helpful msg  
rescue_argument_error: true,

# gives usage help and exits if commandline is empty
# useful if your app always needs args or options to work
help_cmdline_empty: false,

# raises an ArgumentError if there are global args 
# (after parsing options and commands)
# useful if you don't expect any args
raise_global_args: false,

# formats help output with the width_... controls
formatted_help: true,

width_display: 79,
width_indent: 3,
width_left: 18,

# use a detailed options help usage msg or a generic one
detailed_usage: true,

# uses --no-... options for all options
# useful if you want all your options to be negatable by default
autonegatable: false,

# uses short options (besides long ones) for all options
# useful if you want all your options to be shortable by default
autoshortable: false,

# TODO: consider adding this option
# admits -h besides --help in help predefined option
# shortable_help: false,
}
VERSION =
"0.3.1"

Class Method Summary collapse

Class Method Details

.control(key) ⇒ Object

raises a KeyError if key is not a recognized control TODO consider raising SyntaxError with a custom msg instead of KeyError



54
55
56
# File 'lib/miniparse/control.rb', line 54

def self.control(key)
  @behaviour_controls.fetch(key)
end

.debug(msg) ⇒ Object



42
43
44
# File 'lib/miniparse.rb', line 42

def self.debug(msg)
  puts "\nDEBUG #{caller[0]}: #{msg}" 
end

.help_usage_format(right_text) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/miniparse.rb', line 29

def self.help_usage_format(right_text)
  left_text = "usage: #{File.basename($PROGRAM_NAME)}"
  if Miniparse.control(:formatted_help)
    width_display = Miniparse.control(:width_display)
    width_left = left_text.size
    WordWrap.two_cols_word_wrap(left_text, ' ', right_text, 
        width_left, width_display - 1 - width_left)
  else
    left_text + " " + right_text
  end
end

.reset_controlsObject



45
46
47
48
# File 'lib/miniparse/control.rb', line 45

def self.reset_controls 
  @behaviour_controls = {}
  @behaviour_controls.merge! DEFAULT_CONTROLS
end

.set_control(opts = {}) ⇒ Object

raises a KeyError if any key is not a recognized control



59
60
61
62
63
# File 'lib/miniparse/control.rb', line 59

def self.set_control(opts = {})
  opts.keys.each  { |key|  @behaviour_controls.fetch key }
  @behaviour_controls.merge! opts
  nil
end