micro-optparse 0.8.1 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +19 -0
- data/README.md +78 -0
- data/lib/micro-optparse/version.rb +1 -1
- data/micro-optparse.gemspec +1 -1
- metadata +7 -5
data/MIT-LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011 Florian Pilz
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
µ-optparse
|
2
|
+
==========
|
3
|
+
|
4
|
+
Why another command line parser?
|
5
|
+
--------------------------------
|
6
|
+
|
7
|
+
There are lots of command line parser out there, for example [trollop](http://trollop.rubyforge.org/) is a great alternative.
|
8
|
+
However, it is 800 lines long.
|
9
|
+
In addition, trollop sucks at validating the input.
|
10
|
+
So µ-optparse is for you if you are looking for
|
11
|
+
|
12
|
+
* a small command line parser to copy and paste it into your scripts to avoid injecting gem-dependencies
|
13
|
+
* a command line parser with powerful validations
|
14
|
+
* an easily usable, understandable and extendable command line parser
|
15
|
+
* a wrapper around optparse, which intelligently fills out most informations on its own
|
16
|
+
|
17
|
+
What is µ-optparse?
|
18
|
+
-------------------
|
19
|
+
|
20
|
+
µ-optparse is a small wrapper around [optparse](http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html), weighing **less than 75 lines of code**.
|
21
|
+
optparse (or OptionParser) on the other hand is a command line parser, which ships with ruby.
|
22
|
+
After you defined available options, it automatically creates a help page and is able to parse ARGV accordingly.
|
23
|
+
However, optparse requires you to repeat yourself quite often, which leads to many lines of code, just to configure the available options.
|
24
|
+
µ-optparse removes this obstacle by extracting the information contained in few lines of configuration.
|
25
|
+
In addition, µ-optparse extends optparse by some **powerful validations**, which where inspired by [OptiFlag](http://optiflag.rubyforge.org/quick.html).
|
26
|
+
|
27
|
+
Talk in code!
|
28
|
+
-------------
|
29
|
+
|
30
|
+
require 'rubygems' # necessary for ruby v1.8.*
|
31
|
+
require 'micro-optparse'
|
32
|
+
options = Parser.new do |p|
|
33
|
+
p.banner = "This is a fancy script, for usage see below"
|
34
|
+
p.version = "fancy script 0.0 alpha"
|
35
|
+
p.option :severity, "set severity", :default => 4, :value_in_set => [4,5,6,7,8]
|
36
|
+
p.option :verbose, "enable verbose output"
|
37
|
+
p.option :mutation, "set mutation", :default => "MightyMutation", :value_matches => /Mutation/
|
38
|
+
p.option :plus_selection, "use plus-selection if set", :default => true
|
39
|
+
p.option :selection, "selection used", :default => "BestSelection", :short => "l"
|
40
|
+
p.option :chance, "set mutation chance", :default => 0.8, :value_satisfies => lambda {|x| x >= 0.0 && x <= 1.0}
|
41
|
+
end.process!
|
42
|
+
|
43
|
+
What this piece of code does is the following:
|
44
|
+
|
45
|
+
* it creates a help message and help options, with the banner above the options
|
46
|
+
* it creates a version option, which displays the given text
|
47
|
+
* it creates a long accessor for each option, according to the symbol - for example `"--verbose"` for `:verbose`
|
48
|
+
* it crease a short accessor, which is the first character of the long accessor (automatically resolves duplicates)
|
49
|
+
* it checks if the class of the input and the default value match
|
50
|
+
* it creates a switch, if no default value exist or the default value is `true` or `false`
|
51
|
+
* when value\_in\_set is given, it validates if the input value is in the given array
|
52
|
+
* when value_matches is given, it validates if the input value matches the regular expression
|
53
|
+
* when value_satisfies is given, it validates if the lamda or Proc evaluates to `true`, when fed with the input value
|
54
|
+
* it stores all parsed arguments and default values in the options hash, i.e. to access the value of `:mutation` in your script, write `options[:mutation]`
|
55
|
+
|
56
|
+
The automatically generated help message looks like this:
|
57
|
+
|
58
|
+
This is a fancy script, for usage see below
|
59
|
+
-s, --severity 4 set severity
|
60
|
+
-v, --[no-]verbose enable verbose output
|
61
|
+
-m, --mutation MightyMutation set mutation
|
62
|
+
-p, --[no-]plus-selection use plus-selection if set
|
63
|
+
-l, --selection BestSelection selection used
|
64
|
+
-c, --chance 0.8 set mutation chance
|
65
|
+
-h, --help Show this message
|
66
|
+
-V, --version Print version
|
67
|
+
|
68
|
+
Where do I get µ-optparse?
|
69
|
+
--------------------------
|
70
|
+
|
71
|
+
You can either go and install the gem via `gem install micro-optparse` or grab it from [github](https://github.com/florianpilz/micro-optparse/blob/master/lib/micro-optparse/parser.rb) and paste it into your script.
|
72
|
+
If you choose the latter option, you may delete the `validate`-method to spare another 15 lines of code.
|
73
|
+
|
74
|
+
If you want to contribute, you can fork the [github repository](https://github.com/florianpilz/micro-optparse), make your changes and send me a pull request.
|
75
|
+
However, improvements must be one of the following:
|
76
|
+
|
77
|
+
* use fewer lines of code, without sacrificing readablity or functionality
|
78
|
+
* enhance readablity or functionality, without increasing the lines of code
|
data/micro-optparse.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Florian Pilz"]
|
10
10
|
s.email = ["fpilz87@googlemail.com"]
|
11
|
-
s.homepage = ""
|
11
|
+
s.homepage = "http://florianpilz.github.com/micro-optparse/"
|
12
12
|
s.summary = %q{A very small wrapper around optparse.}
|
13
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
14
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: micro-optparse
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 59
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 8
|
9
|
-
-
|
10
|
-
version: 0.8.
|
9
|
+
- 2
|
10
|
+
version: 0.8.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Florian Pilz
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-02-
|
18
|
+
date: 2011-02-27 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -31,13 +31,15 @@ extra_rdoc_files: []
|
|
31
31
|
files:
|
32
32
|
- .gitignore
|
33
33
|
- Gemfile
|
34
|
+
- MIT-LICENSE
|
35
|
+
- README.md
|
34
36
|
- Rakefile
|
35
37
|
- lib/micro-optparse.rb
|
36
38
|
- lib/micro-optparse/parser.rb
|
37
39
|
- lib/micro-optparse/version.rb
|
38
40
|
- micro-optparse.gemspec
|
39
41
|
has_rdoc: true
|
40
|
-
homepage:
|
42
|
+
homepage: http://florianpilz.github.com/micro-optparse/
|
41
43
|
licenses:
|
42
44
|
- MIT
|
43
45
|
post_install_message:
|