Class: Cyclops

Inherits:
Object
  • Object
show all
Defined in:
lib/cyclops/option_parser_extension.rb,
lib/cyclops.rb,
lib/cyclops/version.rb

Overview

#

cyclops – A command-line option parser #

#

Copyright © 2014 Jens Wille #

#

Authors: #

Jens Wille <jens.wille@gmail.com>                                       #
                                                                        #

cyclops is free software; you can redistribute it and/or modify it under # the terms of the GNU Affero General Public License as published by the Free # Software Foundation; either version 3 of the License, or (at your option) # any later version. #

#

cyclops is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for # more details. #

#

You should have received a copy of the GNU Affero General Public License # along with cyclops. If not, see <www.gnu.org/licenses/>. #

#

++

Defined Under Namespace

Modules: OptionParserExtension, Version

Constant Summary collapse

VERSION =
Version.to_s

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defaults = nil, *args) ⇒ Cyclops

Returns a new instance of Cyclops.



71
72
73
74
75
76
77
78
# File 'lib/cyclops.rb', line 71

def initialize(defaults = nil, *args)
  @defaults, @prog = defaults || self.class.defaults, $0

  init(*args)

  # prevent backtrace on ^C
  trap(:INT) { exit 130 }
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



66
67
68
# File 'lib/cyclops.rb', line 66

def config
  @config
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



66
67
68
# File 'lib/cyclops.rb', line 66

def defaults
  @defaults
end

#optionsObject (readonly)

Returns the value of attribute options.



66
67
68
# File 'lib/cyclops.rb', line 66

def options
  @options
end

#progObject

Returns the value of attribute prog.



69
70
71
# File 'lib/cyclops.rb', line 69

def prog
  @prog
end

#stderrObject (readonly)

Returns the value of attribute stderr.



67
68
69
# File 'lib/cyclops.rb', line 67

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



67
68
69
# File 'lib/cyclops.rb', line 67

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



67
68
69
# File 'lib/cyclops.rb', line 67

def stdout
  @stdout
end

Class Method Details

.defaultsObject



44
45
46
# File 'lib/cyclops.rb', line 44

def defaults
  {}
end

.execute(*args) ⇒ Object



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

def execute(*args)
  new.execute(*args)
end

.usage(prog) ⇒ Object



36
37
38
# File 'lib/cyclops.rb', line 36

def usage(prog)
  "Usage: #{prog} [-h|--help] [options]"
end

.versionObject



40
41
42
# File 'lib/cyclops.rb', line 40

def version
  parent_const_get(:VERSION)
end

Instance Method Details

#execute(arguments = ARGV, *inouterr) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cyclops.rb', line 92

def execute(arguments = ARGV, *inouterr)
  reset(*inouterr)
  parse_options(arguments)
  run(arguments)
rescue => err
  raise if $VERBOSE
  abort err.is_a?(OptionParser::ParseError) ?
    "#{err}\n#{usage}" : "#{err.backtrace.first}: #{err} (#{err.class})"
ensure
  options.each_value { |value| value.close if value.is_a?(Zlib::GzipWriter) }
end

#prognameObject



80
81
82
# File 'lib/cyclops.rb', line 80

def progname
  File.basename(prog)
end

#reset(stdin = STDIN, stdout = STDOUT, stderr = STDERR) ⇒ Object



108
109
110
111
# File 'lib/cyclops.rb', line 108

def reset(stdin = STDIN, stdout = STDOUT, stderr = STDERR)
  @stdin, @stdout, @stderr = stdin, stdout, stderr
  @options, @config = {}, {}
end

#run(arguments) ⇒ Object

Raises:

  • (NotImplementedError)


104
105
106
# File 'lib/cyclops.rb', line 104

def run(arguments)
  raise NotImplementedError, 'must be implemented by subclass'
end

#usageObject



84
85
86
# File 'lib/cyclops.rb', line 84

def usage
  self.class.usage(prog)
end

#versionObject



88
89
90
# File 'lib/cyclops.rb', line 88

def version
  self.class.version
end