ruby_cli 0.0.3 → 0.0.4
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/README.rdoc +50 -39
- data/lib/ruby_cli.rb +4 -30
- metadata +6 -9
- /data/{bin → examples}/hello_world +0 -0
data/README.rdoc
CHANGED
@@ -7,7 +7,7 @@ License:: Distributed under the same terms as Ruby
|
|
7
7
|
= Description
|
8
8
|
|
9
9
|
"RubyCLI" is a Ruby library which factors out the code needed to create Ruby
|
10
|
-
|
10
|
+
programs with a command line interface (CLI) and that follow the Unix Philosophy
|
11
11
|
design method outlined in http://www.faqs.org/docs/artu/ch01s06.html.
|
12
12
|
|
13
13
|
Currently, RubyCLI is short and simple. It uses Ruby's core
|
@@ -22,7 +22,20 @@ What does a command line application library need to do?
|
|
22
22
|
2. Pass options and arguments as parameters to other functions defined in libraries or other executables.
|
23
23
|
|
24
24
|
What does a command line application library need not do?
|
25
|
-
|
25
|
+
1. A command line application does not need to validate options or arguments.
|
26
|
+
* Libraries or other executables should do this.
|
27
|
+
|
28
|
+
This is the core algorithm of any Ruby CLI application
|
29
|
+
def run
|
30
|
+
if parse_options? && arguments_Valid?
|
31
|
+
process_options
|
32
|
+
process_arguments
|
33
|
+
output_options_and_arguments# def run
|
34
|
+
command
|
35
|
+
else
|
36
|
+
output_help(1)
|
37
|
+
end
|
38
|
+
end
|
26
39
|
|
27
40
|
= Installation
|
28
41
|
|
@@ -33,57 +46,55 @@ gem install ruby_cli
|
|
33
46
|
There are other tools out there which can be used to write command line
|
34
47
|
applications.
|
35
48
|
|
36
|
-
1. clamp[http://github.com/mdub/clamp]
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
3. Thor[http://github.com/wycats/thor]
|
41
|
-
* It does not try to follow the Unix Philosophy.
|
42
|
-
4. Clip[http://clip.rubyforge.org/]
|
43
|
-
* OptionParser already exists.
|
49
|
+
1. clamp[http://github.com/mdub/clamp] - I don't like to learn new DSLs
|
50
|
+
2. optparse[http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html] - This library uses this to parse options.
|
51
|
+
3. Thor[http://github.com/wycats/thor] - It does not try to follow the Unix Philosophy.
|
52
|
+
4. Clip[http://clip.rubyforge.org/] - OptionParser already exists.
|
44
53
|
|
45
54
|
= Usage
|
46
55
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
= Usage Example
|
56
|
+
1. New File
|
57
|
+
2. Require the ruby_cli gem.
|
58
|
+
3. Create a Ruby class.
|
59
|
+
4. Call it "App", for example.
|
60
|
+
5. Include the RubyCLI module.
|
61
|
+
6. Define the command method.
|
62
|
+
* This is where your program does the actual work.
|
63
|
+
* At this point you have options and arguments available.
|
64
|
+
* Pass them as parameters to library functions or as options/arguments to other executables.
|
65
|
+
* Be smart! Have libraries and other executables do the heavy work.
|
66
|
+
* Be smart! Fewer lines of code (LOC) here is an indication that your code will be easy to maintain.
|
67
|
+
7. Define command options and defaults (optional)
|
68
|
+
* This is where you define a hash for your options and set the default values.
|
69
|
+
* Remember, options by definition are optional.
|
70
|
+
8. Define command arguments and defaults (optional)
|
71
|
+
|
72
|
+
= Usage Example 1
|
64
73
|
|
65
74
|
This example demonstrates how to use RubyCLI to create a
|
66
|
-
#!/usr/bin/ruby
|
67
75
|
|
68
|
-
|
76
|
+
#!/usr/bin/ruby
|
69
77
|
|
70
|
-
|
71
|
-
include RubyCLI
|
78
|
+
require 'ruby_cli'
|
72
79
|
|
73
|
-
|
74
|
-
|
75
|
-
end
|
80
|
+
class App
|
81
|
+
include RubyCLI
|
76
82
|
|
77
|
-
|
83
|
+
def command
|
84
|
+
puts "hello world"
|
85
|
+
end
|
78
86
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
87
|
+
end
|
88
|
+
|
89
|
+
if __FILE__ == $0
|
90
|
+
app = App.new(ARGV)
|
91
|
+
app.run
|
92
|
+
end
|
83
93
|
|
84
94
|
= Dependencies
|
85
95
|
|
86
96
|
* Ruby 1.8.7 or greater
|
97
|
+
* None other
|
87
98
|
|
88
99
|
= Acknowledgements
|
89
100
|
|
data/lib/ruby_cli.rb
CHANGED
@@ -4,32 +4,7 @@ require 'optparse'
|
|
4
4
|
# command line applications which follow the Unix Philosophy design method
|
5
5
|
# (http://www.faqs.org/docs/artu/ch01s06.html).
|
6
6
|
#
|
7
|
-
#
|
8
|
-
# 1. Provide a UI
|
9
|
-
# a. Process options (use Ruby's Option Parser)
|
10
|
-
# b. Process arguments
|
11
|
-
# 2. Pass options and arguments as parameters to other functions defined in
|
12
|
-
# libraries or other executables.
|
13
|
-
#
|
14
|
-
# What does a command line application library need not do?
|
15
|
-
# 1. A command line application does not need to validate options or arguments.
|
16
|
-
# Libraries or other executables should do this.
|
17
|
-
#
|
18
|
-
# This module serves as a mixin for Ruby Command Line Applications (CLI).
|
19
|
-
# Ruby commands can be written much easier by including this class and following
|
20
|
-
# the convention that I have outlined here.
|
21
|
-
#
|
22
|
-
# This is the core algorithm of any Ruby CLI application.
|
23
|
-
# def run
|
24
|
-
# if parse_options? && arguments_valid?
|
25
|
-
# process_options
|
26
|
-
# process_arguments
|
27
|
-
# output_options_and_arguments if @default_options[:verbose]
|
28
|
-
# command
|
29
|
-
# else
|
30
|
-
# output_help(1)
|
31
|
-
# end
|
32
|
-
# end
|
7
|
+
# See README.rdoc for more information.
|
33
8
|
module RubyCLI
|
34
9
|
|
35
10
|
# Initialization of this application requires the command line arguments.
|
@@ -62,7 +37,6 @@ module RubyCLI
|
|
62
37
|
end
|
63
38
|
end
|
64
39
|
|
65
|
-
|
66
40
|
# Parse the options
|
67
41
|
# Redefine this method if you want to add command specific options
|
68
42
|
def parse_options?
|
@@ -78,17 +52,17 @@ module RubyCLI
|
|
78
52
|
opts.on('-V','--verbose','Run verbosely') do
|
79
53
|
@default_options[:verbose] = true
|
80
54
|
end
|
81
|
-
#
|
55
|
+
# If you redefine, you can add command specific options here!
|
82
56
|
end
|
83
57
|
@opt_parser.parse!(@default_argv) rescue return false
|
84
|
-
true
|
58
|
+
return true
|
85
59
|
end
|
86
60
|
|
87
61
|
# Check if the required number of arguments remains in the
|
88
62
|
# argv array after it has been processed by the option parser
|
89
63
|
def arguments_valid?()
|
90
64
|
return true if @arguments.size == 0
|
91
|
-
@default_argv.size == @arguments.size
|
65
|
+
return @default_argv.size == @arguments.size
|
92
66
|
end
|
93
67
|
|
94
68
|
def output_options_and_arguments
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 25
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Martin Velez
|
@@ -15,14 +14,14 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2011-11-
|
17
|
+
date: 2011-11-27 00:00:00 -08:00
|
19
18
|
default_executable:
|
20
19
|
dependencies: []
|
21
20
|
|
22
21
|
description: Factors out code needed to create Ruby command line applications
|
23
22
|
email: mvelez999@gmail.com
|
24
|
-
executables:
|
25
|
-
|
23
|
+
executables: []
|
24
|
+
|
26
25
|
extensions: []
|
27
26
|
|
28
27
|
extra_rdoc_files: []
|
@@ -30,7 +29,7 @@ extra_rdoc_files: []
|
|
30
29
|
files:
|
31
30
|
- lib/ruby_cli.rb
|
32
31
|
- README.rdoc
|
33
|
-
-
|
32
|
+
- examples/hello_world
|
34
33
|
- test/test_ruby_cli.rb
|
35
34
|
- Rakefile
|
36
35
|
has_rdoc: true
|
@@ -47,7 +46,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
46
|
requirements:
|
48
47
|
- - ">="
|
49
48
|
- !ruby/object:Gem::Version
|
50
|
-
hash: 3
|
51
49
|
segments:
|
52
50
|
- 0
|
53
51
|
version: "0"
|
@@ -56,7 +54,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
54
|
requirements:
|
57
55
|
- - ">="
|
58
56
|
- !ruby/object:Gem::Version
|
59
|
-
hash: 3
|
60
57
|
segments:
|
61
58
|
- 0
|
62
59
|
version: "0"
|
File without changes
|