summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMatthew Bloch <matthew@bytemark.co.uk>2014-10-31 02:43:35 +0000
committerMatthew Bloch <matthew@bytemark.co.uk>2014-10-31 02:43:35 +0000
commit1238c74fa01c009d7f76327f3beb30fee4b9f98f (patch)
tree2e0e0abde1b35f03ef515acc9ebd57f638af1c25 /lib
parentf23c6c91e2e2a8eb4154ec545199a5ecbe5136a1 (diff)
Refactored to improve logging and reduce cut & paste code, bumped Debian version number.
Diffstat (limited to 'lib')
-rwxr-xr-xlib/byteback.rb6
-rwxr-xr-xlib/byteback/backup_directory.rb103
-rwxr-xr-xlib/byteback/disk_free.rb41
-rwxr-xr-xlib/byteback/log.rb46
-rwxr-xr-xlib/byteback/util.rb68
-rwxr-xr-xlib/trollop.rb782
6 files changed, 1046 insertions, 0 deletions
diff --git a/lib/byteback.rb b/lib/byteback.rb
new file mode 100755
index 0000000..b6c2ab2
--- /dev/null
+++ b/lib/byteback.rb
@@ -0,0 +1,6 @@
+require 'trollop'
+require 'time'
+require 'byteback/backup_directory'
+require 'byteback/disk_free'
+require 'byteback/util'
+require 'byteback/log' \ No newline at end of file
diff --git a/lib/byteback/backup_directory.rb b/lib/byteback/backup_directory.rb
new file mode 100755
index 0000000..f0ceabc
--- /dev/null
+++ b/lib/byteback/backup_directory.rb
@@ -0,0 +1,103 @@
+module Byteback
+ # Represent a directory full of backups where "current" is a subvolume
+ # which is snapshotted to frozen backup directories called e.g.
+ # "yyyy-mm-ddThh:mm+zzzz".
+ #
+ class BackupDirectory
+ attr_reader :dir
+
+ def initialize(dir)
+ @dir = Dir.new(dir)
+ current
+ end
+
+ # Return total amount of free space in backup directory (bytes)
+ #
+ def free
+ df = DiskFree.new(@dir.path)
+ df.total - df.used
+ end
+
+ # Return an array of Times representing the current list of
+ # snapshots.
+ #
+ def snapshot_times
+ @dir.entries.map do |entry|
+ begin
+ Time.parse(entry)
+ rescue ArgumentError => error
+ nil
+ end
+ end.
+ compact.
+ sort
+ end
+
+ # What order to remove snapshots in to regain disk space?
+ #
+ # Order backups by their closeness to defined backup times, which are
+ # listed in a set order (i.e. today's backup is more important than yesterday's).
+ #
+ BACKUP_IMPORTANCE = [0, 1, 2, 3, 7, 14, 21, 28, 56, 112]
+ def snapshot_times_by_importance
+ now = Time.now
+ snapshot_times_unsorted = snapshot_times
+ snapshot_times_sorted = []
+ while !snapshot_times_unsorted.empty?
+ BACKUP_IMPORTANCE.each do |days|
+ target_time = now + (days*86400)
+ closest = snapshot_times_unsorted.inject(nil) do |best, time|
+ if best.nil? || (time-target_time).abs < (best-target_time).abs
+ time
+ else
+ best
+ end
+ end
+ break unless closest
+ snapshot_times_sorted << snapshot_times_unsorted.delete(closest)
+ end
+ end
+ snapshot_times_sorted
+ end
+
+ # Returns the size of the given snapshot (runs du, may be slow)
+ #
+ # Would much prefer to take advantage of this feature:
+ # http://dustymabe.com/2013/09/22/btrfs-how-big-are-my-snapshots/
+ # but it's not currently in Debian/wheezy.
+ #
+ def snapshot_size(time=snapshot_times.last)
+ `du -s -b #{snapshot_path(time)}`.to_i
+ end
+
+ def average_snapshot_size(number=10)
+ snapshot_times.sort[0..number].inject(0) { |total, time| snapshot_size(time) } / number
+ end
+
+ # Create a new snapshot of 'current'
+ #
+ def new_snapshot!
+ system_no_error("btrfs subvolume snapshot -r #{current.path} #{snapshot_path}")
+ end
+
+ def delete_snapshot!(time)
+ system_no_error("btrfs subvolume delete #{snapshot_path(time)}")
+ end
+
+ def current
+ Dir.new("#{dir.path}/current")
+ end
+
+ def snapshot_path(time=Time.now)
+ "#{dir.path}/#{time.strftime("%Y-%m-%dT%H:%M%z")}"
+ end
+
+ protected
+
+ def system_no_error(*args)
+ args[-1] += " > /dev/null" unless @verbose
+ raise RuntimeError.new("Command failed: "+args.join(" ")) unless
+ system(*args)
+ end
+ end
+end
diff --git a/lib/byteback/disk_free.rb b/lib/byteback/disk_free.rb
new file mode 100755
index 0000000..33952a3
--- /dev/null
+++ b/lib/byteback/disk_free.rb
@@ -0,0 +1,41 @@
+
+module Byteback
+ # Icky way to find out free disc space on our mount
+ #
+ class DiskFree
+ def initialize(mount)
+ @mount = mount
+ end
+
+ def total
+ all[2]
+ end
+
+ def used
+ all[3]
+ end
+
+ def available
+ all[4]
+ end
+
+ def fraction_used
+ disk_device, disk_fs, disk_total, disk_used, disk_available, *rest = all
+ disk_used.to_f / disk_available
+ end
+
+ protected
+
+ def all
+ disk_device, disk_fs, disk_total, disk_used, disk_available, *rest =
+ df.
+ split("\n")[1].
+ split(/\s+/).
+ map { |i| /^[0-9]+$/.match(i) ? i.to_i : i }
+ end
+
+ def df
+ `/bin/df -T -P -B1 #{@mount}`
+ end
+ end
+end
diff --git a/lib/byteback/log.rb b/lib/byteback/log.rb
new file mode 100755
index 0000000..66b86ef
--- /dev/null
+++ b/lib/byteback/log.rb
@@ -0,0 +1,46 @@
+require 'logger'
+require 'syslog'
+
+module Byteback
+ # Translates Ruby's Logger calls to similar calls to Syslog
+ # (implemented in Ruby 2.0 as Syslog::Logger)
+ #
+ class SyslogProxy
+ class << self
+ def debug(*a); Syslog.log(Syslog::LOG_DEBUG, *a); end
+ def info(*a); Syslog.log(Syslog::LOG_INFO, *a); end
+ def warn(*a); Syslog.log(Syslog::LOG_WARN, *a); end
+ def error(*a); Syslog.log(Syslog::LOG_ERR, *a); end
+ def fatal(*a); Syslog.log(Syslog::LOG_EMERG, *a); end
+ end
+ end
+
+ # Log proxy class that we can include in our scripts for some simple
+ # logging defaults.
+ #
+ module Log
+ @@me = File.expand_path($0).split("/").last
+
+ @@logger = if STDIN.tty? && !ENV['BYTEBACK_TO_SYSLOG']
+ logger = Logger.new(STDERR)
+ logger.level = Logger::DEBUG
+ logger.formatter = proc { |severity, datetime, progname, msg|
+ if severity == "FATAL" || severity == "ERROR"
+ "*** #{msg}\n"
+ else
+ "#{msg}\n"
+ end
+ }
+ logger
+ else
+ Syslog.open(@@me)
+ SyslogProxy
+ end
+
+ def debug(*a); @@logger.__send__(:debug, *a); end
+ def info(*a); @@logger.__send__(:info, *a); end
+ def warn(*a); @@logger.__send__(:warn, *a); end
+ def fatal(*a); @@logger.__send__(:fatal, *a); exit 1; end
+ def error(*a); @@logger.__send__(:error, *a); end
+ end
+end
diff --git a/lib/byteback/util.rb b/lib/byteback/util.rb
new file mode 100755
index 0000000..f9f6c62
--- /dev/null
+++ b/lib/byteback/util.rb
@@ -0,0 +1,68 @@
+require 'tempfile'
+
+module Byteback
+ module Util
+ @@lockfile = "/var/run/byteback/byteback.lock"
+
+ def remove_lockfile!
+ begin
+ File.unlink(@@lockfile)
+ rescue Errno::ENOENT
+ end
+ end
+
+ def claim_lockfile!
+ # Check the lockfile first
+ if File.directory?(File.dirname(@@lockfile))
+ if File.exists? @@lockfile
+ # check the lockfile is sane
+ exist_pid = File.read(@@lockfile).to_i
+ if exist_pid > 1 and exist_pid < (File.read("/proc/sys/kernel/pid_max").to_i)
+ begin
+ Process.getpgid(exist_pid)
+ # if no exception, process is running, abort
+ fatal("Process is running (#{exist_pid} from #{@@lockfile})")
+ rescue Errno::ESRCH
+ # no process running with that pid, pidfile is stale
+ remove_lockfile!
+ end
+ else
+ # lockfile isn't sane, remove it and continue
+ remove_lockfile!
+ end
+ end
+ else
+ Dir.mkdir(File.dirname(@@lockfile))
+ # lockfile didn't exist so just carry on
+ end
+
+ # Own the pidfile ourselves
+ File.open(@@lockfile, "w") do |lockfile|
+ lockfile.puts Process::pid
+ end
+ end
+
+ def lock_out_other_processes(name)
+ @@lockfile = "/var/run/byteback/#{name}.lock"
+ claim_lockfile!
+ at_exit { remove_lockfile! }
+ end
+
+ def log_system(*args)
+ debug("system: " + args.map { |a| / /.match(a) ? "\"#{a}\"" : a }.join(" "))
+ rd, wr = IO.pipe
+ pid = fork
+ if pid.nil? # child
+ rd.close
+ STDOUT.reopen(wr)
+ STDERR.reopen(wr)
+ # any cleanup actually necessary here?
+ exec(*args)
+ end
+ wr.close
+ rd.each_line { |line| debug(line.chomp) }
+ pid2, status = Process.waitpid2(pid, 0)
+ status.exitstatus
+ end
+ end
+end
diff --git a/lib/trollop.rb b/lib/trollop.rb
new file mode 100755
index 0000000..8264475
--- /dev/null
+++ b/lib/trollop.rb
@@ -0,0 +1,782 @@
+## lib/trollop.rb -- trollop command-line processing library
+## Author:: William Morgan (mailto: wmorgan-trollop@masanjin.net)
+## Copyright:: Copyright 2007 William Morgan
+## License:: the same terms as ruby itself
+
+require 'date'
+
+module Trollop
+
+VERSION = "1.16.2"
+
+## Thrown by Parser in the event of a commandline error. Not needed if
+## you're using the Trollop::options entry.
+class CommandlineError < StandardError; end
+
+## Thrown by Parser if the user passes in '-h' or '--help'. Handled
+## automatically by Trollop#options.
+class HelpNeeded < StandardError; end
+
+## Thrown by Parser if the user passes in '-h' or '--version'. Handled
+## automatically by Trollop#options.
+class VersionNeeded < StandardError; end
+
+## Regex for floating point numbers
+FLOAT_RE = /^-?((\d+(\.\d+)?)|(\.\d+))([eE][-+]?[\d]+)?$/
+
+## Regex for parameters
+PARAM_RE = /^-(-|\.$|[^\d\.])/
+
+## The commandline parser. In typical usage, the methods in this class
+## will be handled internally by Trollop::options. In this case, only the
+## #opt, #banner and #version, #depends, and #conflicts methods will
+## typically be called.
+##
+## If you want to instantiate this class yourself (for more complicated
+## argument-parsing logic), call #parse to actually produce the output hash,
+## and consider calling it from within
+## Trollop::with_standard_exception_handling.
+class Parser
+
+ ## The set of values that indicate a flag option when passed as the
+ ## +:type+ parameter of #opt.
+ FLAG_TYPES = [:flag, :bool, :boolean]
+
+ ## The set of values that indicate a single-parameter (normal) option when
+ ## passed as the +:type+ parameter of #opt.
+ ##
+ ## A value of +io+ corresponds to a readable IO resource, including
+ ## a filename, URI, or the strings 'stdin' or '-'.
+ SINGLE_ARG_TYPES = [:int, :integer, :string, :double, :float, :io, :date]
+
+ ## The set of values that indicate a multiple-parameter option (i.e., that
+ ## takes multiple space-separated values on the commandline) when passed as
+ ## the +:type+ parameter of #opt.
+ MULTI_ARG_TYPES = [:ints, :integers, :strings, :doubles, :floats, :ios, :dates]
+
+ ## The complete set of legal values for the +:type+ parameter of #opt.
+ TYPES = FLAG_TYPES + SINGLE_ARG_TYPES + MULTI_ARG_TYPES
+
+ INVALID_SHORT_ARG_REGEX = /[\d-]/ #:nodoc:
+
+ ## The values from the commandline that were not interpreted by #parse.
+ attr_reader :leftovers
+
+ ## The complete configuration hashes for each option. (Mainly useful
+ ## for testing.)
+ attr_reader :specs
+
+ ## Initializes the parser, and instance-evaluates any block given.
+ def initialize *a, &b
+ @version = nil
+ @leftovers = []
+ @specs = {}
+ @long = {}
+ @short = {}
+ @order = []
+ @constraints = []
+ @stop_words = []
+ @stop_on_unknown = false
+
+ #instance_eval(&b) if b # can't take arguments
+ cloaker(&b).bind(self).call(*a) if b
+ end
+
+ ## Define an option. +name+ is the option name, a unique identifier
+ ## for the option that you will use internally, which should be a
+ ## symbol or a string. +desc+ is a string description which will be
+ ## displayed in help messages.
+ ##
+ ## Takes the following optional arguments:
+ ##
+ ## [+:long+] Specify the long form of the argument, i.e. the form with two dashes. If unspecified, will be automatically derived based on the argument name by turning the +name+ option into a string, and replacing any _'s by -'s.
+ ## [+:short+] Specify the short form of the argument, i.e. the form with one dash. If unspecified, will be automatically derived from +name+.
+ ## [+:type+] Require that the argument take a parameter or parameters of type +type+. For a single parameter, the value can be a member of +SINGLE_ARG_TYPES+, or a corresponding Ruby class (e.g. +Integer+ for +:int+). For multiple-argument parameters, the value can be any member of +MULTI_ARG_TYPES+ constant. If unset, the default argument type is +:flag+, meaning that the argument does not take a parameter. The specification of +:type+ is not necessary if a +:default+ is given.
+ ## [+:default+] Set the default value for an argument. Without a default value, the hash returned by #parse (and thus Trollop::options) will have a +nil+ value for this key unless the argument is given on the commandline. The argument type is derived automatically from the class of the default value given, so specifying a +:type+ is not necessary if a +:default+ is given. (But see below for an important caveat when +:multi+: is specified too.) If the argument is a flag, and the default is set to +true+, then if it is specified on the the commandline the value will be +false+.
+ ## [+:required+] If set to +true+, the argument must be provided on the commandline.
+ ## [+:multi+] If set to +true+, allows multiple occurrences of the option on the commandline. Otherwise, only a single instance of the option is allowed. (Note that this is different from taking multiple parameters. See below.)
+ ##
+ ## Note that there are two types of argument multiplicity: an argument
+ ## can take multiple values, e.g. "--arg 1 2 3". An argument can also
+ ## be allowed to occur multiple times, e.g. "--arg 1 --arg 2".
+ ##
+ ## Arguments that take multiple values should have a +:type+ parameter
+ ## drawn from +MULTI_ARG_TYPES+ (e.g. +:strings+), or a +:default:+
+ ## value of an array of the correct type (e.g. [String]). The
+ ## value of this argument will be an array of the parameters on the
+ ## commandline.
+ ##
+ ## Arguments that can occur multiple times should be marked with
+ ## +:multi+ => +true+. The value of this argument will also be an array.
+ ## In contrast with regular non-multi options, if not specified on
+ ## the commandline, the default value will be [], not nil.
+ ##
+ ## These two attributes can be combined (e.g. +:type+ => +:strings+,
+ ## +:multi+ => +true+), in which case the value of the argument will be
+ ## an array of arrays.
+ ##
+ ## There's one ambiguous case to be aware of: when +:multi+: is true and a
+ ## +:default+ is set to an array (of something), it's ambiguous whether this
+ ## is a multi-value argument as well as a multi-occurrence argument.
+ ## In thise case, Trollop assumes that it's not a multi-value argument.
+ ## If you want a multi-value, multi-occurrence argument with a default
+ ## value, you must specify +:type+ as well.
+
+ def opt name, desc="", opts={}
+ raise ArgumentError, "you already have an argument named '#{name}'" if @specs.member? name
+
+ ## fill in :type
+ opts[:type] = # normalize
+ case opts[:type]
+ when :boolean, :bool; :flag
+ when :integer; :int
+ when :integers; :ints
+ when :double; :float
+ when :doubles; :floats
+ when Class
+ case opts[:type].name
+ when 'TrueClass', 'FalseClass'; :flag
+ when 'String'; :string
+ when 'Integer'; :int
+ when 'Float'; :float
+ when 'IO'; :io
+ when 'Date'; :date
+ else
+ raise ArgumentError, "unsupported argument type '#{opts[:type].class.name}'"
+ end
+ when nil; nil
+ else
+ raise ArgumentError, "unsupported argument type '#{opts[:type]}'" unless TYPES.include?(opts[:type])
+ opts[:type]
+ end
+
+ ## for options with :multi => true, an array default doesn't imply
+ ## a multi-valued argument. for that you have to specify a :type
+ ## as well. (this is how we disambiguate an ambiguous situation;
+ ## see the docs for Parser#opt for details.)
+ disambiguated_default =
+ if opts[:multi] && opts[:default].is_a?(Array) && !opts[:type]
+ opts[:default].first
+ else
+ opts[:default]
+ end
+
+ type_from_default =
+ case disambiguated_default
+ when Integer; :int
+ when Numeric; :float
+ when TrueClass, FalseClass; :flag
+ when String; :string
+ when IO; :io
+ when Date; :date
+ when Array
+ if opts[:default].empty?
+ raise ArgumentError, "multiple argument type cannot be deduced from an empty array for '#{opts[:default][0].class.name}'"
+ end
+ case opts[:default][0] # the first element determines the types
+ when Integer; :ints
+ when Numeric; :floats
+ when String; :strings
+ when IO; :ios
+ when Date; :dates
+ else
+ raise ArgumentError, "unsupported multiple argument type '#{opts[:default][0].class.name}'"
+ end
+ when nil; nil
+ else
+ raise ArgumentError, "unsupported argument type '#{opts[:default].class.name}'"
+ end
+
+ raise ArgumentError, ":type specification and default type don't match (default type is #{type_from_default})" if opts[:type] && type_from_default && opts[:type] != type_from_default
+
+ opts[:type] = opts[:type] || type_from_default || :flag
+
+ ## fill in :long
+ opts[:long] = opts[:long] ? opts[:long].to_s : name.to_s.gsub("_", "-")
+ opts[:long] =
+ case opts[:long]
+ when /^--([^-].*)$/
+ $1
+ when /^[^-]/
+ opts[:long]
+ else
+ raise ArgumentError, "invalid long option name #{opts[:long].inspect}"
+ end
+ raise ArgumentError, "long option name #{opts[:long].inspect} is already taken; please specify a (different) :long" if @long[opts[:long]]
+
+ ## fill in :short
+ opts[:short] = opts[:short].to_s if opts[:short] unless opts[:short] == :none
+ opts[:short] = case opts[:short]
+ when /^-(.)$/; $1
+ when nil, :none, /^.$/; opts[:short]
+ else raise ArgumentError, "invalid short option name '#{opts[:short].inspect}'"
+ end
+
+ if opts[:short]
+ raise ArgumentError, "short option name #{opts[:short].inspect} is already taken; please specify a (different) :short" if @short[opts[:short]]
+ raise ArgumentError, "a short option name can't be a number or a dash" if opts[:short] =~ INVALID_SHORT_ARG_REGEX
+ end
+
+ ## fill in :default for flags
+ opts[:default] = false if opts[:type] == :flag && opts[:default].nil?
+
+ ## autobox :default for :multi (multi-occurrence) arguments
+ opts[:default] = [opts[:default]] if opts[:default] && opts[:multi] && !opts[:default].is_a?(Array)
+
+ ## fill in :multi
+ opts[:multi] ||= false
+
+ opts[:desc] ||= desc
+ @long[opts[:long]] = name
+ @short[opts[:short]] = name if opts[:short] && opts[:short] != :none
+ @specs[name] = opts
+ @order << [:opt, name]
+ end
+
+ ## Sets the version string. If set, the user can request the version
+ ## on the commandline. Should probably be of the form "<program name>
+ ## <version number>".
+ def version s=nil; @version = s if s; @version end
+
+ ## Adds text to the help display. Can be interspersed with calls to
+ ## #opt to build a multi-section help page.
+ def banner s; @order << [:text, s] end
+ alias :text :banner
+
+ ## Marks two (or more!) options as requiring each other. Only handles
+ ## undirected (i.e., mutual) dependencies. Directed dependencies are
+ ## better modeled with Trollop::die.
+ def depends *syms
+ syms.each { |sym| raise ArgumentError, "unknown option '#{sym}'" unless @specs[sym] }
+ @constraints << [:depends, syms]
+ end
+
+ ## Marks two (or more!) options as conflicting.
+ def conflicts *syms
+ syms.each { |sym| raise ArgumentError, "unknown option '#{sym}'" unless @specs[sym] }
+ @constraints << [:conflicts, syms]
+ end
+
+ ## Defines a set of words which cause parsing to terminate when
+ ## encountered, such that any options to the left of the word are
+ ## parsed as usual, and options to the right of the word are left
+ ## intact.
+ ##
+ ## A typical use case would be for subcommand support, where these
+ ## would be set to the list of subcommands. A subsequent Trollop
+ ## invocation would then be used to parse subcommand options, after
+ ## shifting the subcommand off of ARGV.
+ def stop_on *words
+ @stop_words = [*words].flatten
+ end
+
+ ## Similar to #stop_on, but stops on any unknown word when encountered
+ ## (unless it is a parameter for an argument). This is useful for
+ ## cases where you don't know the set of subcommands ahead of time,
+ ## i.e., without first parsing the global options.
+ def stop_on_unknown
+ @stop_on_unknown = true
+ end
+
+ ## Parses the commandline. Typically called by Trollop::options,
+ ## but you can call it directly if you need more control.
+ ##
+ ## throws CommandlineError, HelpNeeded, and VersionNeeded exceptions.
+ def parse cmdline=ARGV
+ vals = {}
+ required = {}
+
+ opt :version, "Print version and exit" if @version unless @specs[:version] || @long["version"]
+ opt :help, "Show this message" unless @specs[:help] || @long["help"]
+
+ @specs.each do |sym, opts|
+ required[sym] = true if opts[:required]
+ vals[sym] = opts[:default]
+ vals[sym] = [] if opts[:multi] && !opts[:default] # multi arguments default to [], not nil
+ end
+
+ resolve_default_short_options
+
+ ## resolve symbols
+ given_args = {}
+ @leftovers = each_arg cmdline do |arg, params|
+ sym = case arg
+ when /^-([^-])$/
+ @short[$1]
+ when /^--([^-]\S*)$/
+ @long[$1]
+ else
+ raise CommandlineError, "invalid argument syntax: '#{arg}'"
+ end
+ raise CommandlineError, "unknown argument '#{arg}'" unless sym
+
+ if given_args.include?(sym) && !@specs[sym][:multi]
+ raise CommandlineError, "option '#{arg}' specified multiple times"
+ end
+
+ given_args[sym] ||= {}
+
+ given_args[sym][:arg] = arg
+ given_args[sym][:params] ||= []
+
+ # The block returns the number of parameters taken.
+ num_params_taken = 0
+
+ unless params.nil?
+ if SINGLE_ARG_TYPES.include?(@specs[sym][:type])
+ given_args[sym][:params] << params[0, 1] # take the first parameter
+ num_params_taken = 1
+ elsif MULTI_ARG_TYPES.include?(@specs[sym][:type])
+ given_args[sym][:params] << params # take all the parameters
+ num_params_taken = params.size
+ end
+ end
+
+ num_params_taken
+ end
+
+ ## check for version and help args
+ raise VersionNeeded if given_args.include? :version
+ raise HelpNeeded if given_args.include? :help
+
+ ## check constraint satisfaction
+ @constraints.each do |type, syms|
+ constraint_sym = syms.find { |sym| given_args[sym] }
+ next unless constraint_sym
+
+ case type
+ when :depends
+ syms.each { |sym| raise CommandlineError, "--#{@specs[constraint_sym][:long]} requires --#{@specs[sym][:long]}" unless given_args.include? sym }
+ when :conflicts
+ syms.each { |sym| raise CommandlineError, "--#{@specs[constraint_sym][:long]} conflicts with --#{@specs[sym][:long]}" if given_args.include?(sym) && (sym != constraint_sym) }
+ end
+ end
+
+ required.each do |sym, val|
+ raise CommandlineError, "option --#{@specs[sym][:long]} must be specified" unless given_args.include? sym
+ end
+
+ ## parse parameters
+ given_args.each do |sym, given_data|
+ arg = given_data[:arg]
+ params = given_data[:params]
+
+ opts = @specs[sym]
+ raise CommandlineError, "option '#{arg}' needs a parameter" if params.empty? && opts[:type] != :flag and not opts[:allow_blank]
+
+ vals["#{sym}_given".intern] = true # mark argument as specified on the commandline
+
+ case opts[:type]
+ when :flag
+ vals[sym] = !opts[:default]
+ when :int, :ints
+ vals[sym] = params.map { |pg| pg.map { |p| parse_integer_parameter p, arg } }
+ when :float, :floats
+ vals[sym] = params.map { |pg| pg.map { |p| parse_float_parameter p, arg } }
+ when :string, :strings
+ vals[sym] = params.map { |pg| pg.map { |p| p.to_s } }
+ when :io, :ios
+ vals[sym] = params.map { |pg| pg.map { |p| parse_io_parameter p, arg } }
+ when :date, :dates
+ vals[sym] = params.map { |pg| pg.map { |p| parse_date_parameter p, arg } }
+ end
+
+ if SINGLE_ARG_TYPES.include?(opts[:type])
+ unless opts[:multi] # single parameter
+ vals[sym] = vals[sym][0][0] rescue nil
+ else # multiple options, each with a single parameter
+ vals[sym] = vals[sym].map { |p| p[0] }
+ end
+ elsif MULTI_ARG_TYPES.include?(opts[:type]) && !opts[:multi]
+ vals[sym] = vals[sym][0] # single option, with multiple parameters
+ end
+ # else: multiple options, with multiple parameters
+ end
+
+ ## modify input in place with only those
+ ## arguments we didn't process
+ cmdline.clear
+ @leftovers.each { |l| cmdline << l }
+
+ ## allow openstruct-style accessors
+ class << vals
+ def method_missing(m, *args)
+ self[m] || self[m.to_s]
+ end
+ end
+ vals
+ end
+
+ def parse_date_parameter param, arg #:nodoc:
+ begin
+ begin
+ time = Chronic.parse(param)
+ rescue NameError
+ # chronic is not available
+ end
+ time ? Date.new(time.year, time.month, time.day) : Date.parse(param)
+ rescue ArgumentError => e
+ raise CommandlineError, "option '#{arg}' needs a date"
+ end
+ end
+
+ ## Print the help message to +stream+.
+ def educate stream=$stdout
+ width # just calculate it now; otherwise we have to be careful not to
+ # call this unless the cursor's at the beginning of a line.
+
+ left = {}
+ @specs.each do |name, spec|
+ left[name] = "--#{spec[:long]}" +
+ (spec[:short] && spec[:short] != :none ? ", -#{spec[:short]}" : "") +
+ case spec[:type]
+ when :flag; ""
+ when :int; " <i>"
+ when :ints; " <i+>"
+ when :string; " <s>"
+ when :strings; " <s+>"
+ when :float; " <f>"
+ when :floats; " <f+>"
+ when :io; " <filename/uri>"
+ when :ios; " <filename/uri+>"
+ when :date; " <date>"
+ when :dates; " <date+>"
+ end
+ end
+
+ leftcol_width = left.values.map { |s| s.length }.max || 0
+ rightcol_start = leftcol_width + 6 # spaces
+
+ unless @order.size > 0 && @order.first.first == :text
+ stream.puts "#@version\n" if @version
+ stream.puts "Options:"
+ end
+
+ @order.each do |what, opt|
+ if what == :text
+ stream.puts wrap(opt)
+ next
+ end
+
+ spec = @specs[opt]
+ stream.printf " %#{leftcol_width}s: ", left[opt]
+ desc = spec[:desc] + begin
+ default_s = case spec[:default]
+ when $stdout; "<stdout>"
+ when $stdin; "<stdin>"
+ when $stderr; "<stderr>"
+ when Array
+ spec[:default].join(", ")
+ else
+ spec[:default].to_s
+ end
+
+ if spec[:default]
+ if spec[:desc] =~ /\.$/
+ " (Default: #{default_s})"
+ else
+ " (default: #{default_s})"
+ end
+ else
+ ""
+ end
+ end
+ stream.puts wrap(desc, :width => width - rightcol_start - 1, :prefix => rightcol_start)
+ end
+ end
+
+ def width #:nodoc:
+ @width ||= if $stdout.tty?
+ begin
+ require 'curses'
+ Curses::init_screen
+ x = Curses::cols
+ Curses::close_screen
+ x
+ rescue Exception
+ 80
+ end
+ else
+ 80
+ end
+ end
+
+ def wrap str, opts={} # :nodoc:
+ if str == ""
+ [""]
+ else
+ str.split("\n").map { |s| wrap_line s, opts }.flatten
+ end
+ end
+
+ ## The per-parser version of Trollop::die (see that for documentation).
+ def die arg, msg
+ if msg
+ $stderr.puts "Error: argument --#{@specs[arg][:long]} #{msg}."
+ else
+ $stderr.puts "Error: #{arg}."
+ end
+ $stderr.puts "Try --help for help."
+ exit(-1)
+ end
+
+private
+
+ ## yield successive arg, parameter pairs
+ def each_arg args
+ remains = []
+ i = 0
+
+ until i >= args.length
+ if @stop_words.member? args[i]
+ remains += args[i .. -1]
+ return remains
+ end
+ case args[i]
+ when /^--$/ # arg terminator
+ remains += args[(i + 1) .. -1]
+ return remains
+ when /^--(\S+?)=(.*)$/ # long argument with equals
+ yield "--#{$1}", [$2]
+ i += 1
+ when /^--(\S+)$/ # long argument
+ params = collect_argument_parameters(args, i + 1)
+ unless params.empty?
+ num_params_taken = yield args[i], params
+ unless num_params_taken
+ if @stop_on_unknown
+ remains += args[i + 1 .. -1]
+ return remains
+ else
+ remains += params
+ end
+ end
+ i += 1 + num_params_taken
+ else # long argument no parameter
+ yield args[i], nil
+ i += 1
+ end
+ when /^-(\S+)$/ # one or more short arguments
+ shortargs = $1.split(//)
+ shortargs.each_with_index do |a, j|
+ if j == (shortargs.length - 1)
+ params = collect_argument_parameters(args, i + 1)
+ unless params.empty?
+ num_params_taken = yield "-#{a}", params
+ unless num_params_taken
+ if @stop_on_unknown
+ remains += args[i + 1 .. -1]
+ return remains
+ else
+ remains += params
+ end
+ end
+ i += 1 + num_params_taken
+ else # argument no parameter
+ yield "-#{a}", nil
+ i += 1
+ end
+ else
+ yield "-#{a}", nil
+ end
+ end
+ else
+ if @stop_on_unknown
+ remains += args[i .. -1]
+ return remains
+ else
+ remains << args[i]
+ i += 1
+ end
+ end
+ end
+
+ remains
+ end
+
+ def parse_integer_parameter param, arg
+ raise CommandlineError, "option '#{arg}' needs an integer" unless param =~ /^\d+$/
+ param.to_i
+ end
+
+ def parse_float_parameter param, arg
+ raise CommandlineError, "option '#{arg}' needs a floating-point number" unless param =~ FLOAT_RE
+ param.to_f
+ end
+
+ def parse_io_parameter param, arg
+ case param
+ when /^(stdin|-)$/i; $stdin
+ else
+ require 'open-uri'
+ begin
+ open param
+ rescue SystemCallError => e
+ raise CommandlineError, "file or url for option '#{arg}' cannot be opened: #{e.message}"
+ end
+ end
+ end
+
+ def collect_argument_parameters args, start_at
+ params = []
+ pos = start_at
+ while args[pos] && args[pos] !~ PARAM_RE && !@stop_words.member?(args[pos]) do
+ params << args[pos]
+ pos += 1
+ end
+ params
+ end
+
+ def resolve_default_short_options
+ @order.each do |type, name|
+ next unless type == :opt
+ opts = @specs[name]
+ next if opts[:short]
+
+ c = opts[:long].split(//).find { |d| d !~ INVALID_SHORT_ARG_REGEX && !@short.member?(d) }
+ if c # found a character to use
+ opts[:short] = c
+ @short[c] = name
+ end
+ end
+ end
+
+ def wrap_line str, opts={}
+ prefix = opts[:prefix] || 0
+ width = opts[:width] || (self.width - 1)
+ start = 0
+ ret = []
+ until start > str.length
+ nextt =
+ if start + width >= str.length
+ str.length
+ else
+ x = str.rindex(/\s/, start + width)
+ x = str.index(/\s/, start) if x && x < start
+ x || str.length
+ end
+ ret << (ret.empty? ? "" : " " * prefix) + str[start ... nextt]
+ start = nextt + 1
+ end
+ ret
+ end
+
+ ## instance_eval but with ability to handle block arguments
+ ## thanks to why: http://redhanded.hobix.com/inspect/aBlockCostume.html
+ def cloaker &b
+ (class << self; self; end).class_eval do
+ define_method :cloaker_, &b
+ meth = instance_method :cloaker_
+ remove_method :cloaker_
+ meth
+ end
+ end
+end
+
+## The easy, syntactic-sugary entry method into Trollop. Creates a Parser,
+## passes the block to it, then parses +args+ with it, handling any errors or
+## requests for help or version information appropriately (and then exiting).
+## Modifies +args+ in place. Returns a hash of option values.
+##
+## The block passed in should contain zero or more calls to +opt+
+## (Parser#opt), zero or more calls to +text+ (Parser#text), and
+## probably a call to +version+ (Parser#version).
+##
+## The returned block contains a value for every option specified with
+## +opt+. The value will be the value given on the commandline, or the
+## default value if the option was not specified on the commandline. For
+## every option specified on the commandline, a key "<option
+## name>_given" will also be set in the hash.
+##
+## Example:
+##
+## require 'trollop'
+## opts = Trollop::options do
+## opt :monkey, "Use monkey mode" # a flag --monkey, defaulting to false
+## opt :goat, "Use goat mode", :default => true # a flag --goat, defaulting to true
+## opt :num_limbs, "Number of limbs", :default => 4 # an integer --num-limbs <i>, defaulting to 4
+## opt :num_thumbs, "Number of thumbs", :type => :int # an integer --num-thumbs <i>, defaulting to nil
+## end
+##
+## ## if called with no arguments
+## p opts # => { :monkey => false, :goat => true, :num_limbs => 4, :num_thumbs => nil }
+##
+## ## if called with --monkey
+## p opts # => {:monkey_given=>true, :monkey=>true, :goat=>true, :num_limbs=>4, :help=>false, :num_thumbs=>nil}
+##
+## See more examples at http://trollop.rubyforge.org.
+def options args=ARGV, *a, &b
+ @last_parser = Parser.new(*a, &b)
+ with_standard_exception_handling(@last_parser) { @last_parser.parse args }
+end
+
+## If Trollop::options doesn't do quite what you want, you can create a Parser
+## object and call Parser#parse on it. That method will throw CommandlineError,
+## HelpNeeded and VersionNeeded exceptions when necessary; if you want to
+## have these handled for you in the standard manner (e.g. show the help
+## and then exit upon an HelpNeeded exception), call your code from within
+## a block passed to this method.
+##
+## Note that this method will call System#exit after handling an exception!
+##
+## Usage example:
+##
+## require 'trollop'
+## p = Trollop::Parser.new do
+## opt :monkey, "Use monkey mode" # a flag --monkey, defaulting to false
+## opt :goat, "Use goat mode", :default => true # a flag --goat, defaulting to true
+## end
+##
+## opts = Trollop::with_standard_exception_handling p do
+## o = p.parse ARGV
+## raise Trollop::HelpNeeded if ARGV.empty? # show help screen
+## o
+## end
+##
+## Requires passing in the parser object.
+
+def with_standard_exception_handling parser
+ begin
+ yield
+ rescue CommandlineError => e
+ $stderr.puts "Error: #{e.message}."
+ $stderr.puts "Try --help for help."
+ exit(-1)
+ rescue HelpNeeded
+ parser.educate
+ exit
+ rescue VersionNeeded
+ puts parser.version
+ exit
+ end
+end
+
+## Informs the user that their usage of 'arg' was wrong, as detailed by
+## 'msg', and dies. Example:
+##
+## options do
+## opt :volume, :default => 0.0
+## end
+##
+## die :volume, "too loud" if opts[:volume] > 10.0
+## die :volume, "too soft" if opts[:volume] < 0.1
+##
+## In the one-argument case, simply print that message, a notice
+## about -h, and die. Example:
+##
+## options do
+## opt :whatever # ...
+## end
+##
+## Trollop::die "need at least one filename" if ARGV.empty?
+def die arg, msg=nil
+ if @last_parser
+ @last_parser.die arg, msg
+ else
+ raise ArgumentError, "Trollop::die can only be called after Trollop::options"
+ end
+end
+
+module_function :options, :die, :with_standard_exception_handling
+
+end # module