ruby_cli 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +21 -16
- data/examples/hello_world +1 -1
- data/lib/ruby_cli.rb +17 -14
- metadata +3 -6
data/README.rdoc
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
=
|
1
|
+
= RubyCLI
|
2
2
|
|
3
|
-
Author:: Martin Velez
|
3
|
+
Author:: {Martin Velez}[http://www.martinvelez.com]
|
4
4
|
Copyright:: Copyright (c) 2011 Martin Velez
|
5
5
|
License:: Distributed under the same terms as Ruby
|
6
6
|
|
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
|
@@ -13,7 +13,7 @@ design method outlined in http://www.faqs.org/docs/artu/ch01s06.html.
|
|
13
13
|
Currently, RubyCLI is short and simple. It uses Ruby's core
|
14
14
|
optparse[http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html] library.
|
15
15
|
|
16
|
-
|
16
|
+
== Design
|
17
17
|
|
18
18
|
What does a command line application library need to do?
|
19
19
|
1. Provide a user interface (UI)
|
@@ -22,7 +22,7 @@ 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
|
-
1.
|
25
|
+
1. Validate options or arguments.
|
26
26
|
* Libraries or other executables should do this.
|
27
27
|
|
28
28
|
This is the core algorithm of any Ruby CLI application
|
@@ -37,7 +37,7 @@ This is the core algorithm of any Ruby CLI application
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
|
40
|
+
== Installation
|
41
41
|
|
42
42
|
Rubygems:
|
43
43
|
gem install ruby_cli
|
@@ -46,7 +46,7 @@ Not Rubygems:
|
|
46
46
|
1. use only files in lib folder
|
47
47
|
2. Use the RubyCLI module as a mixin for your CLI application
|
48
48
|
|
49
|
-
|
49
|
+
== Alternative Tools
|
50
50
|
|
51
51
|
There are other tools out there which can be used to write command line
|
52
52
|
applications.
|
@@ -56,7 +56,7 @@ applications.
|
|
56
56
|
3. Thor[http://github.com/wycats/thor] - It does not try to follow the Unix Philosophy.
|
57
57
|
4. Clip[http://clip.rubyforge.org/] - OptionParser already exists.
|
58
58
|
|
59
|
-
|
59
|
+
== Usage
|
60
60
|
|
61
61
|
1. New File
|
62
62
|
2. Require the ruby_cli gem.
|
@@ -74,7 +74,7 @@ applications.
|
|
74
74
|
* Remember, options by definition are optional.
|
75
75
|
8. Define command arguments and defaults (optional)
|
76
76
|
|
77
|
-
|
77
|
+
== Usage Example 1
|
78
78
|
|
79
79
|
This example demonstrates how to use RubyCLI to create a command line application.
|
80
80
|
|
@@ -101,16 +101,21 @@ This example demonstrates how to use RubyCLI to create a command line applicatio
|
|
101
101
|
* Ruby 1.8.7 or greater
|
102
102
|
* None other
|
103
103
|
|
104
|
-
|
104
|
+
== Acknowledgements
|
105
105
|
|
106
|
-
Todd Werth[http://blog.toddwerth.com/entries/5]
|
107
|
-
* I used his Ruby command line application skeleton code. I borrowed
|
108
|
-
some ideas from there.
|
106
|
+
{Todd Werth}[http://blog.toddwerth.com/entries/5]
|
107
|
+
* I used his Ruby command line application skeleton code. I borrowed some ideas from there.
|
109
108
|
|
110
|
-
|
109
|
+
== TODO
|
111
110
|
|
112
111
|
* ?
|
113
112
|
|
114
|
-
|
113
|
+
== Development
|
115
114
|
|
116
|
-
|
115
|
+
=== Source Repository
|
116
|
+
ruby_cli is hosted on Github at:
|
117
|
+
https://github.com/martinvelez/ruby_cli
|
118
|
+
|
119
|
+
=== Issues and Bug Reports
|
120
|
+
Get help, request features, and reports bugs here:
|
121
|
+
https://github.com/martinvelez/ruby_cli/issues
|
data/examples/hello_world
CHANGED
data/lib/ruby_cli.rb
CHANGED
@@ -7,12 +7,13 @@ require 'optparse'
|
|
7
7
|
# See README.rdoc for more information.
|
8
8
|
module RubyCLI
|
9
9
|
|
10
|
+
|
10
11
|
# Initialization of this application requires the command line arguments.
|
11
12
|
def initialize(default_argv, command_name, usage = "[OPTIONS]... [ARGUMENTS]...")
|
12
13
|
@default_argv = default_argv
|
13
14
|
@default_options = {:help => false, :verbose => false}
|
14
|
-
|
15
|
-
|
15
|
+
initialize_command_options
|
16
|
+
initialize_command_arguments
|
16
17
|
@opt_parser = nil
|
17
18
|
@command_name = command_name
|
18
19
|
@usage = usage
|
@@ -20,11 +21,11 @@ module RubyCLI
|
|
20
21
|
|
21
22
|
# This method can be overwritten if you want to set defaults for your command
|
22
23
|
# specific options.
|
23
|
-
def
|
24
|
+
def initialize_command_options() @options = {} end
|
24
25
|
|
25
26
|
# This method can be overwritten if you want to set defaults for your command
|
26
27
|
# specific arguments.
|
27
|
-
def
|
28
|
+
def initialize_command_arguments() @arguments = {} end
|
28
29
|
|
29
30
|
# Run the application
|
30
31
|
def run
|
@@ -38,10 +39,9 @@ module RubyCLI
|
|
38
39
|
end
|
39
40
|
end
|
40
41
|
|
41
|
-
#
|
42
|
-
def
|
43
|
-
|
44
|
-
OptionParser.new do |opts|
|
42
|
+
# Parse the options
|
43
|
+
def parse_options?
|
44
|
+
@opt_parser = OptionParser.new do |opts|
|
45
45
|
opts.banner = "Usage: #{@command_name} #{@usage}"
|
46
46
|
opts.separator ""
|
47
47
|
opts.separator "Specific options:"
|
@@ -53,16 +53,19 @@ module RubyCLI
|
|
53
53
|
@default_options[:verbose] = true
|
54
54
|
end
|
55
55
|
# If you redefine, you can copy this method and add command specific options here!
|
56
|
-
end
|
57
|
-
|
58
|
-
|
59
|
-
# Parse the options
|
60
|
-
def parse_options?
|
61
|
-
@opt_parser = define_option_parser
|
56
|
+
end
|
57
|
+
define_command_option_parsing
|
62
58
|
@opt_parser.parse!(@default_argv) rescue return false
|
63
59
|
return true
|
64
60
|
end
|
65
61
|
|
62
|
+
# Redefine this method if you have command specific options to tell
|
63
|
+
# the OptionParser object how to parse and handle your options
|
64
|
+
# Introduced in versio 0.2.0 to reduce LOC in CLI application
|
65
|
+
# @opt_parser is available at this point
|
66
|
+
def define_command_option_parsing() end
|
67
|
+
|
68
|
+
|
66
69
|
# Check if the required number of arguments remains in the
|
67
70
|
# argv array after it has been processed by the option parser
|
68
71
|
def arguments_valid?()
|
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: 31
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
|
-
- 1
|
9
7
|
- 2
|
10
|
-
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Martin Velez
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2011-12-
|
17
|
+
date: 2011-12-13 00:00:00 -08:00
|
19
18
|
default_executable:
|
20
19
|
dependencies: []
|
21
20
|
|
@@ -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"
|