micro-optparse 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in micro-optparse.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,19 @@
1
+ require 'micro-optparse/parser'
2
+ #
3
+ # options = Parser.new do |p|
4
+ # # p.banner = "test"
5
+ # p.version = "OptParseWrapper 0.8 (c) Florian Pilz 2011"
6
+ # p.option :severity, "set severity", :default => 4, :value_in_set => [4,5,6,7,8]
7
+ # p.option :verbose, "enable verbose output"
8
+ # p.option :mutation, "set mutation", :default => "MightyMutation", :value_matches => /Mutation/
9
+ # p.option :plus_selection, "use plus-selection if set", :default => true
10
+ # p.option :selection, "selection used", :default => "BestSelection"#, :short => "l"
11
+ # p.option :chance, "set mutation chance", :default => 0.8, :value_satisfies => lambda {|x| x >= 0.0 && x <= 1.0}
12
+ # end.process!
13
+ #
14
+ # puts options[:severity]
15
+ # puts options[:verbose]
16
+ # puts options[:mutation]
17
+ # puts options[:plus_selection]
18
+ # puts options[:selection]
19
+ # puts options[:chance]
@@ -0,0 +1,70 @@
1
+ require 'ostruct'
2
+ require 'optparse'
3
+
4
+ class Parser
5
+ attr_accessor :banner, :version
6
+
7
+ def initialize
8
+ @options = []
9
+ @used_short = []
10
+ yield self
11
+ end
12
+
13
+ def option(name, desc, settings = {})
14
+ @options << [name, desc, settings]
15
+ end
16
+
17
+ def short_from(name)
18
+ name.to_s.chars.each do |c|
19
+ next if @used_short.include?(c)
20
+ return c # returns from short_from method
21
+ end
22
+ end
23
+
24
+ def validate(options) # remove this method if you want fewer lines of code and don't need validations
25
+ options.each_pair do |key, value|
26
+ opt = nil
27
+ @options.each { |o| opt = o if o[0] == key }
28
+ unless opt[2][:value_in_set].nil? || opt[2][:value_in_set].include?(value)
29
+ puts "Parameter for " << key.to_s << " must be in [" << opt[2][:value_in_set].join(",") << "]" ; exit(1)
30
+ end
31
+ unless opt[2][:value_matches].nil? || opt[2][:value_matches] =~ value
32
+ puts "Parameter must match /" << opt[2][:value_matches].source << "/" ; exit(1)
33
+ end
34
+ unless opt[2][:value_satisfies].nil? || opt[2][:value_satisfies].call(value)
35
+ puts "Parameter must satisfy given conditions (see description)" ; exit(1)
36
+ end
37
+ end
38
+ end
39
+
40
+ def process!
41
+ options = {}
42
+ optionparser = OptionParser.new do |p|
43
+ @options.each do |o|
44
+ @used_short << short = o[2][:short] || short_from(o[0])
45
+ options[o[0]] = o[2][:default] || false # set default
46
+ klass = o[2][:default].class == Fixnum ? Integer : o[2][:default].class
47
+
48
+ if klass == TrueClass || klass == FalseClass || klass == NilClass # boolean switch
49
+ p.on("-" << short, "--[no-]" << o[0].to_s.gsub("_", "-"), o[1]) {|x| options[o[0]] = x}
50
+ else # argument with parameter
51
+ p.on("-" << short, "--" << o[0].to_s.gsub("_", "-") << " " << o[2][:default].to_s, klass, o[1]) {|x| options[o[0]] = x}
52
+ end
53
+ end
54
+
55
+ p.banner = @banner unless @banner.nil?
56
+ p.on_tail("-h", "--help", "Show this message") {puts p ; exit}
57
+ short = @used_short.include?("v") ? "-V" : "-v"
58
+ p.on_tail(short, "--version", "Print version") {puts @version ; exit} unless @version.nil?
59
+ end
60
+
61
+ begin
62
+ optionparser.parse!(ARGV)
63
+ rescue OptionParser::ParseError => e
64
+ puts e.message ; exit(1)
65
+ end
66
+
67
+ validate(options) if self.respond_to?("validate")
68
+ options
69
+ end
70
+ end
@@ -0,0 +1,5 @@
1
+ module Micro
2
+ module Optparse
3
+ VERSION = "0.8.0"
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "micro-optparse/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "micro-optparse"
7
+ s.version = Micro::Optparse::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Florian Pilz"]
10
+ s.email = ["fpilz87@googlemail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{A very small wrapper around optparse.}
13
+ s.description = %q{This gem wraps all the functionality of optparse into an easy to use, clear and short syntax. In addtion, strong validations are added. You can either use this gem as a lightweight alternative to trollop or copy all its 75 lines into your script to have an command-line parser without injecting a gem dependency.}
14
+
15
+ s.license = "MIT"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: micro-optparse
3
+ version: !ruby/object:Gem::Version
4
+ hash: 63
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 8
9
+ - 0
10
+ version: 0.8.0
11
+ platform: ruby
12
+ authors:
13
+ - Florian Pilz
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-26 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: This gem wraps all the functionality of optparse into an easy to use, clear and short syntax. In addtion, strong validations are added. You can either use this gem as a lightweight alternative to trollop or copy all its 75 lines into your script to have an command-line parser without injecting a gem dependency.
23
+ email:
24
+ - fpilz87@googlemail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - lib/micro-optparse.rb
36
+ - lib/micro-optparse/parser.rb
37
+ - lib/micro-optparse/version.rb
38
+ - micro-optparse.gemspec
39
+ has_rdoc: true
40
+ homepage: ""
41
+ licenses:
42
+ - MIT
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.4.2
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A very small wrapper around optparse.
73
+ test_files: []
74
+