Module: App

Defined in:
lib/miniparse/app.rb

Overview

this optional module allows an easy integration of parsing and trivial output, in your application

simple example of use:

require "miniparse/app"

App.configure_parser do |parser|
  Miniparse.set_control(
      autoshortable: true,
      )
  parser.add_program_description "my program help introduction\n"
  parser.add_option("--list", "list something")
  parser.parse ARGV
end

# to see App.debug you have to specify the --debug option
# for example: 'ruby ex08_app_simple.rb --debug' 
App.debug App.options   # to stdout
App.info App.options    # to stdout
App.warn App.options    # to stderr
App.error App.options   # to stderr (note: it doesn't exit)

# you have direct access to the parser object
puts "parser object: #{App.parser}"

puts 'Done.'

Constant Summary

ERR_NOT_FOUND =

error exit codes

3
ERR_IO =
4

Class Method Summary (collapse)

Class Method Details

+ (void) configure_parser(&block)

This method returns an undefined value.

defines a block that takes parser as parameter and configures and parses it



71
72
73
74
# File 'lib/miniparse/app.rb', line 71

def self.configure_parser(&block)
  reset_parser
  @do_configure_parser = block    
end

+ (void) debug(msg)

This method returns an undefined value.

Parameters:

  • msg (string)

    is the message to output



60
61
62
# File 'lib/miniparse/app.rb', line 60

def self.debug(msg)
  $stdout.puts "debug: #{msg}"    if App.options[:debug]
end

+ (void) error(msg)

This method returns an undefined value.

Parameters:

  • msg (string)

    is the message to output



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

def self.error(msg)
  $stderr.puts "error: #{msg}"  
end

+ (void) info(msg)

This method returns an undefined value.

Parameters:

  • msg (string)

    is the message to output



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

def self.info(msg)
  $stdout.puts "info: #{msg}"  
end

+ (hash) options

Returns parsed options, i.e. specified in argv

Returns:

  • (hash)

    parsed options, i.e. specified in argv



65
66
67
# File 'lib/miniparse/app.rb', line 65

def self.options
  parser.options  
end

+ (Parser) parser

(the first time it is called after ##configure_parser, it does a lazy parser creation using the block defined in ##configure_parser)

Returns:

  • (Parser)

    parser



79
80
81
82
83
84
85
# File 'lib/miniparse/app.rb', line 79

def self.parser
  return @parser   if @parser
  @parser = Miniparse::Parser.new
  parser.add_option("--debug", nil, negatable: false)
  @do_configure_parser.call(parser)
  parser
end

+ (void) reset_parser

This method returns an undefined value.

re-initialize the parser (it doesn't change the configuration)



89
90
91
# File 'lib/miniparse/app.rb', line 89

def self.reset_parser
  @parser = nil
end

+ (void) warn(msg)

This method returns an undefined value.

Parameters:

  • msg (string)

    is the message to output



48
49
50
# File 'lib/miniparse/app.rb', line 48

def self.warn(msg)
  $stderr.puts "warning: #{msg}"  
end