miniparse 0.3.0
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.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/FAQ.md +38 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +76 -0
- data/Rakefile +18 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/ex01_simple.rb +22 -0
- data/examples/ex02_options.rb +27 -0
- data/examples/ex03_commands.rb +24 -0
- data/examples/ex04_mixed.rb +37 -0
- data/examples/ex05_block.rb +31 -0
- data/examples/ex06_task_app.rb +36 -0
- data/examples/ex07_controls.rb +63 -0
- data/lib/miniparse.rb +51 -0
- data/lib/miniparse/app.rb +51 -0
- data/lib/miniparse/command.rb +210 -0
- data/lib/miniparse/commander.rb +143 -0
- data/lib/miniparse/control.rb +67 -0
- data/lib/miniparse/option_broker.rb +113 -0
- data/lib/miniparse/parser.rb +136 -0
- data/lib/miniparse/version.rb +9 -0
- data/lib/miniparse/word_wrap.rb +59 -0
- data/miniparse.gemspec +34 -0
- metadata +129 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: da37b3c087a37bccf31e7bbd7d5e45004da9bddf
|
|
4
|
+
data.tar.gz: 252751025abef7a40242c19649b99c76f402442c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f13f8964d343a8c70515e99bfcc6d8c14531c9dcff8f03be7ad052cc019eaa00c7491b2491d869e89cf0b959e513c68a17f5857d928fc5e56de2aa7243ff89f1
|
|
7
|
+
data.tar.gz: 59e8323b94edab1f83633dd37641bdfc30dff30cc5df688c3171f09020adb60d4f0182a6eaca703cdbdab70b1e943abaae4d475f2aeb84adf47573f1a503c3e0
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/FAQ.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
|
|
2
|
+
# Frequently Asked Questions
|
|
3
|
+
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
### What's the meaning of x.y.z in your version numbers?
|
|
7
|
+
|
|
8
|
+
We use a [Semantic Versioning](http://semver.org/) like scheme of MAJOR.MINOR.PATCH:
|
|
9
|
+
|
|
10
|
+
The MAJOR version numbers will only change when backwards incompatible changes are made to the public interface.
|
|
11
|
+
|
|
12
|
+
The MINOR version number will change when adding new functionality or some extensive changes in code. But in any case the public interface will be fully backwards compatible.
|
|
13
|
+
|
|
14
|
+
The PATCH number will change when fixing bugs or implementing internal or minor improvements. No changes will be made to the public interface.
|
|
15
|
+
|
|
16
|
+
Additionally a `b` will be added for beta (i.e. pre-release) versions.
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
### Why have you `protected` methods in your classes?
|
|
21
|
+
|
|
22
|
+
It is the tersest way we found of defining a public interface. Please note that the non-public interface can be changed at any time, even when just releasing minor patches.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
### Nevertheless, I wish I could use that protected methods.
|
|
27
|
+
|
|
28
|
+
You can. Just use `object.send(:desired_method)` or make the method public adding to your code the following:
|
|
29
|
+
|
|
30
|
+
module Miniparse
|
|
31
|
+
class DesiredClass
|
|
32
|
+
public :desired_method
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 Juanma Rodriguez
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
|
|
2
|
+
# Miniparse
|
|
3
|
+
|
|
4
|
+
**Miniparse is an easy to use yet flexible and powerful ruby library for parsing command-line options.**
|
|
5
|
+
|
|
6
|
+
The main objetive of this implementation is minimun boiler plate with ease of use but keeping a self documenting specification.
|
|
7
|
+
|
|
8
|
+
Additionally the library is quite flexible and allows a lot of customization but always with sane defaults so **you don't need to learn nothing to start using it**.
|
|
9
|
+
|
|
10
|
+
## How to use
|
|
11
|
+
|
|
12
|
+
Please find below a short but meaningful example, then you can **[get more examples](https://github.com/jmrod4/miniparse/tree/master/examples)** at [Github miniparse repository](https://github.com/jmrod4/miniparse).
|
|
13
|
+
|
|
14
|
+
Let's try putting the following code in `myprogram.rb`
|
|
15
|
+
|
|
16
|
+
require 'miniparse'
|
|
17
|
+
|
|
18
|
+
parser = Miniparse::Parser.new("my program does something wonderful")
|
|
19
|
+
parser.add_option "--debug", "activate debugging"
|
|
20
|
+
parser.parse ARGV
|
|
21
|
+
|
|
22
|
+
if parser.options[:debug]
|
|
23
|
+
puts "DEBUG ACTIVATED!"
|
|
24
|
+
else
|
|
25
|
+
puts "run silently"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
Now you can run
|
|
29
|
+
|
|
30
|
+
$ ruby myprogram.rb
|
|
31
|
+
|
|
32
|
+
run silently
|
|
33
|
+
|
|
34
|
+
or run
|
|
35
|
+
|
|
36
|
+
$ ruby myprogram.rb --debug
|
|
37
|
+
|
|
38
|
+
DEBUG ACTIVATED!
|
|
39
|
+
|
|
40
|
+
or even get the auto generated help
|
|
41
|
+
|
|
42
|
+
$ ruby yourprogram.rb --help
|
|
43
|
+
|
|
44
|
+
my program does something wonderful
|
|
45
|
+
usage: ex01_readme.rb [--help] [--debug] <args>
|
|
46
|
+
|
|
47
|
+
Options:
|
|
48
|
+
--debug activate debugging
|
|
49
|
+
|
|
50
|
+
## Installation
|
|
51
|
+
|
|
52
|
+
You can install it as an standard ruby gem with
|
|
53
|
+
|
|
54
|
+
$ gem install miniparse
|
|
55
|
+
|
|
56
|
+
then to use you can require it adding the following to the top of your ruby source file
|
|
57
|
+
|
|
58
|
+
require 'miniparse'
|
|
59
|
+
|
|
60
|
+
## Contributing
|
|
61
|
+
|
|
62
|
+
Bug reports and pull requests are welcome at https://github.com/jmrod4/miniparse.
|
|
63
|
+
|
|
64
|
+
After checking out the repo you can:
|
|
65
|
+
|
|
66
|
+
* run `bin/setup` to install dependencies
|
|
67
|
+
* then run `rake test` to run the tests
|
|
68
|
+
* you can also run `bin/console` for an interactive prompt that will allow you to experiment
|
|
69
|
+
* run `bundle exec miniparse` to use the gem in this directory, ignoring other installed copies of this gem
|
|
70
|
+
* run `rake -T` to see the rake tasks defined
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
This library is copyrighted software and it's available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
|
75
|
+
|
|
76
|
+
You can find the source code at https://github.com/jmrod4/miniparse.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
require "rake/testtask"
|
|
5
|
+
|
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
|
7
|
+
t.libs << "test"
|
|
8
|
+
t.libs << "lib"
|
|
9
|
+
t.test_files = FileList['test/**/*_test.rb']
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
#task :default => :test
|
|
13
|
+
|
|
14
|
+
require "rspec/core/rake_task"
|
|
15
|
+
|
|
16
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
17
|
+
|
|
18
|
+
task :default => :spec
|
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "miniparse"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# example from the project readme
|
|
2
|
+
|
|
3
|
+
require 'miniparse'
|
|
4
|
+
|
|
5
|
+
parser = Miniparse::Parser.new("my program does something wonderful")
|
|
6
|
+
parser.add_option "--debug", "activate debugging"
|
|
7
|
+
parser.parse ARGV
|
|
8
|
+
|
|
9
|
+
if parser.options[:debug]
|
|
10
|
+
puts "DEBUG ACTIVATED!"
|
|
11
|
+
else
|
|
12
|
+
puts "run silently"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# show results with the current used command line
|
|
16
|
+
# for global options
|
|
17
|
+
puts "args #{parser.args.inspect}"
|
|
18
|
+
puts "options #{parser.options.inspect}"
|
|
19
|
+
# for commands
|
|
20
|
+
puts "parsed command #{parser.command.inspect}"
|
|
21
|
+
puts "command args #{parser.command_args.inspect}"
|
|
22
|
+
puts "command options #{parser.command_options.inspect}"
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require 'miniparse'
|
|
2
|
+
|
|
3
|
+
parser = Miniparse::Parser.new
|
|
4
|
+
|
|
5
|
+
parser.add_option("--sort", "always sort the output", shortable: true)
|
|
6
|
+
parser.add_option("--pill", "just a silly option", shortable: true)
|
|
7
|
+
|
|
8
|
+
# if description is nil, then the option won't appear in the help description, only in the usage string
|
|
9
|
+
parser.add_option("--debug", nil)
|
|
10
|
+
|
|
11
|
+
# for switches the default will be evaluated to true or false
|
|
12
|
+
# for switch the default will be always false unless specified
|
|
13
|
+
parser.add_option("--normal", "run normally", default: true)
|
|
14
|
+
|
|
15
|
+
# for flags the typical default could be a string
|
|
16
|
+
parser.add_option("--verbose LEVEL", nil, default: "0")
|
|
17
|
+
|
|
18
|
+
parser.parse(ARGV)
|
|
19
|
+
|
|
20
|
+
# show results with the current used command line
|
|
21
|
+
# for global options
|
|
22
|
+
puts "args #{parser.args.inspect}"
|
|
23
|
+
puts "options #{parser.options.inspect}"
|
|
24
|
+
# for commands
|
|
25
|
+
puts "parsed command #{parser.command.inspect}"
|
|
26
|
+
puts "command args #{parser.command_args.inspect}"
|
|
27
|
+
puts "command options #{parser.command_options.inspect}"
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'miniparse'
|
|
2
|
+
|
|
3
|
+
parser = Miniparse::Parser.new
|
|
4
|
+
|
|
5
|
+
# if description is nil the command will appear in the help as "other commands"
|
|
6
|
+
parser.add_command("list", nil)
|
|
7
|
+
|
|
8
|
+
# you can use symbol or string to specify a command
|
|
9
|
+
parser.add_command(:fly, "don't actually fly, just do an incredible approximate simulation")
|
|
10
|
+
|
|
11
|
+
# this option is NOT global, but only applicable to the last specified command
|
|
12
|
+
# ('fly' in this case)
|
|
13
|
+
parser.add_option("--high", "set your flying level high")
|
|
14
|
+
|
|
15
|
+
parser.parse(ARGV)
|
|
16
|
+
|
|
17
|
+
# show results with the current used command line
|
|
18
|
+
# for global options
|
|
19
|
+
puts "args #{parser.args.inspect}"
|
|
20
|
+
puts "options #{parser.options.inspect}"
|
|
21
|
+
# for commands
|
|
22
|
+
puts "parsed command #{parser.command.inspect}"
|
|
23
|
+
puts "command args #{parser.command_args.inspect}"
|
|
24
|
+
puts "command options #{parser.command_options.inspect}"
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require 'miniparse'
|
|
2
|
+
|
|
3
|
+
parser = Miniparse::Parser.new
|
|
4
|
+
|
|
5
|
+
parser.add_option("--sort", "always sort the output", shortable: true)
|
|
6
|
+
parser.add_option("--pilly", "just a silly option", shortable: true)
|
|
7
|
+
|
|
8
|
+
# if description is nil, then the option won't appear in the help description, only in the usage string
|
|
9
|
+
parser.add_option("--debug", nil)
|
|
10
|
+
|
|
11
|
+
# for switches the default will be evaluated to true or false
|
|
12
|
+
# for switch the default will be always false unless specified
|
|
13
|
+
parser.add_option("--normal", "run normally", default: true)
|
|
14
|
+
|
|
15
|
+
# for flags the typical default could be a string
|
|
16
|
+
parser.add_option("--verbose LEVEL", nil, default: "0")
|
|
17
|
+
|
|
18
|
+
# if description is nil the command will appear in the help as "other commands"
|
|
19
|
+
parser.add_command("list", nil)
|
|
20
|
+
|
|
21
|
+
# you can use symbol or string to specify a command
|
|
22
|
+
parser.add_command(:fly, "don't actually fly, just do an incredible approximate simulation")
|
|
23
|
+
|
|
24
|
+
# this option is NOT global, but only applicable to the last specified command
|
|
25
|
+
# ('fly' in this case)
|
|
26
|
+
parser.add_option("--high", "set your flying level high")
|
|
27
|
+
|
|
28
|
+
parser.parse(ARGV)
|
|
29
|
+
|
|
30
|
+
# show results with the current used command line
|
|
31
|
+
# for global options
|
|
32
|
+
puts "args #{parser.args.inspect}"
|
|
33
|
+
puts "options #{parser.options.inspect}"
|
|
34
|
+
# for commands
|
|
35
|
+
puts "parsed command #{parser.command.inspect}"
|
|
36
|
+
puts "command args #{parser.command_args.inspect}"
|
|
37
|
+
puts "command options #{parser.command_options.inspect}"
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require 'miniparse'
|
|
2
|
+
|
|
3
|
+
parser = Miniparse::Parser.new
|
|
4
|
+
|
|
5
|
+
# if `add_option` has a block it will execute (when method `parse` called) only if the
|
|
6
|
+
# option is specified in the parsed command-line (i.e. argv)
|
|
7
|
+
#
|
|
8
|
+
# the block will receive the option user specified value (a string) as argument
|
|
9
|
+
parser.add_option("--kill THING", "kill something") do |val|
|
|
10
|
+
puts "Die #{val}! DIE!"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
parser.add_command("list", nil)
|
|
14
|
+
|
|
15
|
+
parser.add_command(:fly, "do an incredible approximate simulation of flying") do
|
|
16
|
+
puts "Look Ma! No hands!"
|
|
17
|
+
end
|
|
18
|
+
parser.add_option("--high", "set your flying level high")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# the blocks for the options or commands specified in ARGV will be executed now
|
|
22
|
+
parser.parse(ARGV)
|
|
23
|
+
|
|
24
|
+
# show results with the current used command line
|
|
25
|
+
# for global options
|
|
26
|
+
puts "args #{parser.args.inspect}"
|
|
27
|
+
puts "options #{parser.options.inspect}"
|
|
28
|
+
# for commands
|
|
29
|
+
puts "parsed command #{parser.command.inspect}"
|
|
30
|
+
puts "command args #{parser.command_args.inspect}"
|
|
31
|
+
puts "command options #{parser.command_options.inspect}"
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
# example of a supposed task application,
|
|
4
|
+
# just demonstrate the self documenting feature
|
|
5
|
+
|
|
6
|
+
require 'miniparse'
|
|
7
|
+
|
|
8
|
+
parser = Miniparse::Parser.new("Provides an easy way to manage tasks.")
|
|
9
|
+
|
|
10
|
+
parser.add_option("--debug", nil) do
|
|
11
|
+
puts "Debug activated!"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
parser.add_option("--file FILE", "file for storing the tasks",
|
|
15
|
+
default: "~/todos.txt")
|
|
16
|
+
|
|
17
|
+
parser.add_command(:new, "creates a new todo in the default location")
|
|
18
|
+
|
|
19
|
+
parser.add_command(:done, "complete a task")
|
|
20
|
+
|
|
21
|
+
parser.add_command(:list, "list tasks")
|
|
22
|
+
parser.add_option("--sort", "order everything!")
|
|
23
|
+
|
|
24
|
+
parser.parse ARGV
|
|
25
|
+
|
|
26
|
+
# show results with the current used command line
|
|
27
|
+
# for global options
|
|
28
|
+
puts "args #{parser.args.inspect}"
|
|
29
|
+
puts "options #{parser.options.inspect}"
|
|
30
|
+
# for commands
|
|
31
|
+
puts "parsed command #{parser.command.inspect}"
|
|
32
|
+
puts "command args #{parser.command_args.inspect}"
|
|
33
|
+
puts "command options #{parser.command_options.inspect}"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# example from the project readme
|
|
2
|
+
|
|
3
|
+
require 'miniparse'
|
|
4
|
+
|
|
5
|
+
# following is just a copy of the controls defined in control.rb
|
|
6
|
+
# you don't need to specify all, only the ones you want to change (maybe none)
|
|
7
|
+
|
|
8
|
+
Miniparse.set_control( {
|
|
9
|
+
# gives an error if there is an unrecognized option either short or long
|
|
10
|
+
# (if not then passes them as arguments)
|
|
11
|
+
raise_on_unrecognized: true,
|
|
12
|
+
|
|
13
|
+
# intercepts .parse ArgumentError (i.e. the commandline user introduced
|
|
14
|
+
# wrong or invalid options) and exits with a helpful msg
|
|
15
|
+
rescue_argument_error: true,
|
|
16
|
+
|
|
17
|
+
# gives usage help and exits if commandline is empty
|
|
18
|
+
# useful if your app always needs args or options to work
|
|
19
|
+
help_cmdline_empty: false,
|
|
20
|
+
|
|
21
|
+
# raises an ArgumentError if there are global args
|
|
22
|
+
# (after parsing options and commands)
|
|
23
|
+
# useful if you don't expect any args
|
|
24
|
+
raise_global_args: false,
|
|
25
|
+
|
|
26
|
+
# formats help output with the width_... controls
|
|
27
|
+
formatted_help: true,
|
|
28
|
+
|
|
29
|
+
width_display: 79,
|
|
30
|
+
width_indent: 3,
|
|
31
|
+
width_left: 18,
|
|
32
|
+
|
|
33
|
+
# use a detailed options help usage msg or a generic one
|
|
34
|
+
detailed_usage: true,
|
|
35
|
+
|
|
36
|
+
# uses --no-... options for all options
|
|
37
|
+
# useful if you want all your options to be negatable by default
|
|
38
|
+
autonegatable: false,
|
|
39
|
+
|
|
40
|
+
# uses short options (besides long ones) for all options
|
|
41
|
+
# useful if you want all your options to be shortable by default
|
|
42
|
+
autoshortable: false,
|
|
43
|
+
} )
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
parser = Miniparse::Parser.new("does something wonderful")
|
|
47
|
+
parser.add_option "--debug", "activate debugging"
|
|
48
|
+
parser.parse ARGV
|
|
49
|
+
|
|
50
|
+
if parser.options[:debug]
|
|
51
|
+
puts "DEBUG ACTIVATED!"
|
|
52
|
+
else
|
|
53
|
+
puts "run silently"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# show results with the current used command line
|
|
57
|
+
# for global options
|
|
58
|
+
puts "args #{parser.args.inspect}"
|
|
59
|
+
puts "options #{parser.options.inspect}"
|
|
60
|
+
# for commands
|
|
61
|
+
puts "parsed command #{parser.command.inspect}"
|
|
62
|
+
puts "command args #{parser.command_args.inspect}"
|
|
63
|
+
puts "command options #{parser.command_options.inspect}"
|