ruby_cli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +99 -0
  2. data/bin/hello_world +17 -0
  3. data/lib/ruby_cli.rb +121 -0
  4. metadata +69 -0
data/README.rdoc ADDED
@@ -0,0 +1,99 @@
1
+ = Ruby CLI
2
+
3
+ Author:: Martin Velez
4
+ Copyright:: Copyright (c) 2011 Martin Velez
5
+ License:: Distributed under the same terms as Ruby
6
+
7
+ = Description
8
+
9
+ "RubyCLI" is a Ruby library which factors out the code needed to create Ruby
10
+ programswith a command line interface (CLI) which follows the Unix Philosophy
11
+ design method outlined in http://www.faqs.org/docs/artu/ch01s06.html.
12
+
13
+ Currently, RubyCLI is short and simple. It uses Ruby's core Option Parser library.
14
+
15
+ = Design
16
+
17
+ What does a command line application library need to do?
18
+ 1. Provide a user interface (UI)
19
+ a. Process options (use Ruby's Option Parser)
20
+ b. Process arguments
21
+ 2. Pass options and arguments as parameters to other functions defined in libraries or other executables.
22
+
23
+ What does a command line application library need not do?
24
+ 1. A command line application does not need to validate options or arguments. Libraries or other executables should do this.
25
+
26
+ = Installation
27
+
28
+ gem install ruby_cli
29
+
30
+ = Alternative Tools
31
+
32
+ There are other tools out there which can be used to write command line
33
+ applications.
34
+
35
+ - clamp[http://github.com/mdub/clamp]
36
+ ** I don't like to learn new DSLs
37
+ - optparse[http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html]
38
+ ** This library uses this to parse options.
39
+ - Thor[http://github.com/wycats/thor]
40
+ ** It does not try to follow the Unix Philosophy.
41
+ - Clip[http://clip.rubyforge.org/]
42
+ ** OptionParser already exists.
43
+
44
+ = Usage
45
+
46
+ 0. New File
47
+ 1. Require the ruby_cli gem.
48
+ 2. Create a Ruby class.
49
+ 3. Call it "App", for example.
50
+ 4. Include the RubyCLI module.
51
+ 5. Define the command method.
52
+ * This is where your program does the actual work.
53
+ * At this point you have options and arguments available.
54
+ * Pass them as parameters to library functions or as options/arguments to other executables.
55
+ * Be smart! Have libraries and other executables do the heavy work.
56
+ * Be smart! Fewer lines of code (LOC) is good.
57
+ 6. Define command options and defaults (optional)
58
+ * This is where you define a hash for your options and set the default values.
59
+ * Remember, options by definition are optional.
60
+ 7. Define command arguments and defaults (optional)
61
+
62
+ = Usage Example
63
+
64
+ This example demonstrates how to use RubyCLI to create a
65
+ #!/usr/bin/ruby
66
+
67
+ require 'ruby_cli'
68
+
69
+ class App
70
+ include RubyCLI
71
+
72
+ def command
73
+ puts "hello world"
74
+ end
75
+
76
+ end
77
+
78
+ if __FILE__ == $0
79
+ app = App.new(ARGV)
80
+ app.run
81
+ end
82
+
83
+ = Dependencies
84
+
85
+ * Ruby 1.8.7 or greater
86
+
87
+ = Acknowledgements
88
+
89
+ Todd Werth[http://blog.toddwerth.com/entries/5]
90
+ * I used his Ruby command line application skeleton code. I borrowed
91
+ some ideas from there.
92
+
93
+ = TODO
94
+
95
+ * ?
96
+
97
+ = Source Code
98
+
99
+ https://github.com/martinvelez/ruby_cli
data/bin/hello_world ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'ruby_cli'
4
+
5
+ class App
6
+ include RubyCLI
7
+
8
+ def command
9
+ puts "Hello World"
10
+ end
11
+
12
+ end
13
+
14
+ if __FILE__ == $0
15
+ app = App.new(ARGV)
16
+ app.run
17
+ end
data/lib/ruby_cli.rb ADDED
@@ -0,0 +1,121 @@
1
+ require 'optparse'
2
+
3
+ # The goal of this library is to factor out code needed to create Ruby
4
+ # command line applications which follow the Unix Philosophy design method
5
+ # (http://www.faqs.org/docs/artu/ch01s06.html).
6
+ #
7
+ # What does a command line application library need to do?
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
33
+ module RubyCLI
34
+
35
+ # Initialization of this application requires the command line arguments.
36
+ def initialize(default_argv)
37
+ @default_argv = default_argv
38
+ @default_options = {:help => false, :verbose => false}
39
+ define_command_options
40
+ define_command_arguments
41
+ @opt_parser = nil
42
+ end
43
+
44
+ # This method can be overwritten if you want to set defaults for your command
45
+ # specific options.
46
+ def define_command_options() @options = {} end
47
+
48
+ # This method can be overwritten if you want to set defaults for your command
49
+ # specific arguments.
50
+ def define_command_arguments() @arguments = {} end
51
+
52
+ # Run the application
53
+ def run
54
+ if parse_options? && arguments_valid?
55
+ process_options
56
+ process_arguments
57
+ output_options_and_arguments if @default_options[:verbose]
58
+ command
59
+ else
60
+ output_help(1)
61
+ end
62
+ end
63
+
64
+
65
+ # Parse the options
66
+ # Redefine this method if you want to add command specific options
67
+ def parse_options?
68
+ #configure an OptionParser
69
+ @opt_parser = OptionParser.new do |opts|
70
+ opts.banner = "Usage: #{__FILE__} [OPTIONS]... [ARGUMENTS]..."
71
+ opts.separator ""
72
+ opts.separator "Specific options:"
73
+ opts.on('-h', '--help', 'displays help information') do
74
+ @default_options[:help] = true
75
+ exit output_help(0)
76
+ end
77
+ opts.on('-V','--verbose','Run verbosely') do
78
+ @default_options[:verbose] = true
79
+ end
80
+ # TODO: If you redefine, you can add command specific options here!
81
+ end
82
+ @opt_parser.parse!(@default_argv) rescue return false
83
+ true
84
+ end
85
+
86
+ # Check if the required number of arguments remains in the
87
+ # argv array after it has been processed by the option parser
88
+ def arguments_valid?()
89
+ return true if @arguments.size == 0
90
+ @default_argv.size == @arguments.size
91
+ end
92
+
93
+ def output_options_and_arguments
94
+ puts "OPTIONS:"
95
+ @default_options.each {|name, value| puts "#{name} = #{value}"}
96
+ @options.each {|name, value| puts "#{name} = #{value}"}
97
+ puts "No options" if @options.length == 0
98
+
99
+ puts "ARGUMENTS:"
100
+ @arguments.each {|name,value| puts "#{name} = #{value}"}
101
+ puts "No arguments" if @arguments.length == 0
102
+ end
103
+
104
+ # Performs post-parse processing on options
105
+ # For instance, some options may be mutually exclusive
106
+ # Redefine if you need to process options.
107
+ def process_options() true end
108
+
109
+ # Redefine if you need to process options.
110
+ def process_arguments() true end
111
+
112
+ # Application logic
113
+ def command() raise "This method should be overwritten." end
114
+
115
+ def output_help(exit_code)
116
+ puts @opt_parser
117
+ puts exit_code if @default_options[:verbose]
118
+ return exit_code
119
+ end
120
+
121
+ end # Application
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_cli
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Martin Velez
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-11 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Factors out code needed to create Ruby command line applications
23
+ email: mvelez999@gmail.com
24
+ executables:
25
+ - hello_world
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/ruby_cli.rb
32
+ - README.rdoc
33
+ - bin/hello_world
34
+ has_rdoc: true
35
+ homepage: http://github.com/martinvelez/ruby_cli
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ hash: 3
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.7
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: A command line application library
68
+ test_files: []
69
+