2014-12-02 01:36:39 +01:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
# Encoding: UTF-8
|
|
|
|
|
|
|
|
## EasyOptions Example
|
|
|
|
## Copyright (C) Someone
|
|
|
|
## Licensed under XYZ
|
|
|
|
##
|
|
|
|
## This program is an example of EasyOptions. It just prints the options and
|
|
|
|
## arguments provided in command line. Usage:
|
|
|
|
##
|
|
|
|
## @script.name [option] ARGUMENTS...
|
|
|
|
##
|
|
|
|
## Options:
|
|
|
|
## -h, --help All client scripts have this, it can be omitted.
|
|
|
|
## -o, --some-option This is a boolean option. Long version is
|
|
|
|
## mandatory, and can be specified before or
|
|
|
|
## after short version.
|
|
|
|
## --some-boolean This is a boolean option without a short version.
|
|
|
|
## --some-value=VALUE This is a parameter option. When calling your script
|
|
|
|
## the equal sign is optional and blank space can be
|
|
|
|
## used instead. Short version is not available in this
|
|
|
|
## format.
|
|
|
|
|
2014-12-03 00:11:18 +01:00
|
|
|
require_relative 'easyoptions'
|
2014-12-02 01:48:25 +01:00
|
|
|
options, arguments = EasyOptions.all
|
2014-12-02 01:36:39 +01:00
|
|
|
|
|
|
|
# Boolean options
|
2014-12-04 13:35:58 +01:00
|
|
|
puts 'Option specified: --some-option' if options[:some_option]
|
2014-12-03 00:11:18 +01:00
|
|
|
puts 'Option specified: --some-boolean' if options[:some_boolean]
|
2014-12-02 01:36:39 +01:00
|
|
|
|
|
|
|
# Parameter option
|
2014-12-02 01:48:25 +01:00
|
|
|
value = options[:some_value]
|
2014-12-02 01:36:39 +01:00
|
|
|
if value
|
2014-12-03 00:22:35 +01:00
|
|
|
type = value.is_a?(Fixnum) ? 'number' : 'string'
|
2014-12-02 01:36:39 +01:00
|
|
|
puts "Option specified: --some-value is #{value} (a #{type})"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Arguments
|
2014-12-02 01:48:25 +01:00
|
|
|
arguments.each do |argument|
|
2014-12-02 01:36:39 +01:00
|
|
|
puts "Argument specified: #{argument}"
|
|
|
|
end
|