Class: Miniparse::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/miniparse/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(program_description = nil) ⇒ Parser

Returns a new instance of Parser.



22
23
24
25
26
27
28
29
30
31
# File 'lib/miniparse/parser.rb', line 22

def initialize(program_description = nil)
  @commander = Commander.new
  @program_desc = program_description     
  @global_broker = OptionBroker.new do
    print program_desc + "\n"    if program_desc
    print help_usage + "\n"
    print help_desc + "\n"
    exit ERR_HELP_REQ
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns after parsing (i.e. specified) rest of arguments.

Returns:

  • after parsing (i.e. specified) rest of arguments



9
10
11
# File 'lib/miniparse/parser.rb', line 9

def args
  @args
end

#program_descObject (readonly)

Returns after parsing (i.e. specified) rest of arguments.

Returns:

  • after parsing (i.e. specified) rest of arguments



9
10
11
# File 'lib/miniparse/parser.rb', line 9

def program_desc
  @program_desc
end

Instance Method Details

#add_command(name, desc, opts = {}, &block) ⇒ Object

:no_options indicates the command has no command line options

Parameters:

  • name

    is the command name (ex. either “kill” or :kill)

  • desc

    is a short description of the command

  • opts (defaults to: {})

    are the options to apply to the command



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

def add_command(name, desc, opts={}, &block)
  args = opts.merge(spec: name, desc: desc)
  commander.add_command(args, &block)
end

#add_option(spec, desc, opts = {}, &block) ⇒ Object

in the command line (ex. “–debug” or “–verbose LEVEL”)

:default :negatable (used only for switches) :shortable

Parameters:

  • spec

    is the option specification, similar to the option invocation

  • desc

    is a short description of the option

  • opts (defaults to: {})

    are the options to apply to the option



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

def add_option(spec, desc, opts={}, &block)
  args = opts.merge(spec: spec, desc: desc)
  current_broker.add_option(args, &block)
end

#commandObject



14
# File 'lib/miniparse/parser.rb', line 14

def command; commander.parsed_command; end

#command_argsObject



16
# File 'lib/miniparse/parser.rb', line 16

def command_args; commander.parsed_args; end

#command_optionsObject



15
# File 'lib/miniparse/parser.rb', line 15

def command_options; commander.parsed_values; end

#current_commandObject

Returns the command the next add_option will apply to.

Returns:

  • the command the next add_option will apply to



19
# File 'lib/miniparse/parser.rb', line 19

def current_command; commander.current_command; end

#help_descObject

Returns a help message with the short descriptions.

Returns:

  • a help message with the short descriptions



81
82
83
84
85
86
87
88
89
90
# File 'lib/miniparse/parser.rb', line 81

def help_desc
  #FIXME
  text = ""
  if (global = global_broker.help_desc).size > 0
    text += "\nOptions:\n"
    text += global
  end
  text += commander.help_desc   
  text
end

#help_usageObject

Returns a usage message.

Returns:

  • a usage message



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/miniparse/parser.rb', line 93

def help_usage
  #FIXME
  if Miniparse.control(:detailed_usage)
    right_text = @global_broker.help_usage
  elsif current_command
    right_text = "[global_options]"   
  else
    right_text = "[options]"
  end
  if current_command
    right_text += " <command> [command_options]"
  end
  right_text += " <args>"
  Miniparse.help_usage_format(right_text)
end

#optionsObject



11
# File 'lib/miniparse/parser.rb', line 11

def options; global_broker.parsed_values; end

#parse(argv) ⇒ Object

Returns unprocessed arguments.

Parameters:

  • argv

    is like ARGV but just for this parser

Returns:

  • unprocessed arguments



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/miniparse/parser.rb', line 60

def parse(argv)
  if Miniparse.control(:help_cmdline_empty) && argv.empty?
    puts help_usage
    exit ERR_HELP_REQ  
  end
  try_argument do
    global_argv, cmd_name, cmd_argv = commander.split_argv(argv)
    @args = global_broker.parse_argv(global_argv)
    if cmd_name
      commander.parse_argv(cmd_name, cmd_argv)
    end
    if Miniparse.control(:raise_global_args) && (! args.empty?)
      # FIXME review this logic later
      error = current_command  ?  "unrecognized command"  :  "extra arguments"
      raise ArgumentError, "#{error} '#{args[0]}'"
    end
    args      
  end
end