Class: ToolOpts

Inherits:
Object
  • Object
show all
Defined in:
lib/shed/opts/tool_opts.rb

Overview

Abstract layer for the tool shed options parsers. This sets the basic paramaters the tools respond to via the command line.

Class Method Summary collapse

Class Method Details

.add_mandatory(op, config) ⇒ Object

Add all mandatory arguments to the options parser.



60
61
# File 'lib/shed/opts/tool_opts.rb', line 60

def self.add_mandatory(op,config)
end

.add_optional(op, config) ⇒ Object

Add all optional arguments to the options parser.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/shed/opts/tool_opts.rb', line 66

def self.add_optional(op,config)
  op.on("-s", "--source [PATH]", String, "Path to source folder, defaults to current directory.") do |value|
    config[:src] = value
  end

  op.on("-o", "--output [FILE]", String, "Path to output file, defaults to #{config[:output]}.") do |value|
    config[:output] = value
  end

  op.on("-v", "--verbose", "Run verbosely.") do |value|
    config[:verbose] = value
  end

  op.on("--silent", "Supress all output.") do |value|
    config[:silent] = value
  end
end

.add_tail(op, out) ⇒ Object

Add tail arguments to the options parser.



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/shed/opts/tool_opts.rb', line 87

def self.add_tail(op,out)
  op.on_tail("-h", "--help", "Show this help message.") do
    out.puts op
    exit
  end

  op.on_tail("--version", "Show version.") do
    out.puts "#{description} version #{version}"
    exit
  end
end

.create_parserObject

Create and return the options parser with the default header.



48
49
50
51
52
53
54
55
# File 'lib/shed/opts/tool_opts.rb', line 48

def self.create_parser
  op = OptionParser.new
  op.banner = "Usage: #{name} [options]"

  op.separator ""
  op.separator "Options:"
  op
end

.default_configObject

Default configuration hash.



36
37
38
39
40
41
42
43
# File 'lib/shed/opts/tool_opts.rb', line 36

def self.default_config
  {
    :src => ".",
    :output => 'output.xml',
    :verbose => false,
    :silent => false
  }
end

.descriptionObject

A basic description of the tools use.



21
22
23
# File 'lib/shed/opts/tool_opts.rb', line 21

def self.description
  'Abstract tool from the tool shed.'
end

.nameObject

The name of the tool, as invoked on the command line.



14
15
16
# File 'lib/shed/opts/tool_opts.rb', line 14

def self.name
  ToolShed::NAME
end

.parse(args, out = STDOUT) ⇒ Object

Parse the arugments and return a config hash.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/shed/opts/tool_opts.rb', line 102

def self.parse(args,out=STDOUT)

  config = default_config()
  options = create_parser()

  add_mandatory(options,config)
  add_optional(options,config)

  add_tail(options,out)

  args = options.parse!(args)

  config[:default] = args.shift

  config
end

.versionObject

A version string to describe the version of the tool these options are designed to invoke.



29
30
31
# File 'lib/shed/opts/tool_opts.rb', line 29

def self.version
  ToolShed::VERSION::STRING
end