Created
November 7, 2013 13:51
-
-
Save RaphaelAudet/7354883 to your computer and use it in GitHub Desktop.
ruby skeleton script from http://blog.toddwerth.com/entries/5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# == Synopsis | |
# This is a sample description of the application. | |
# Blah blah blah. | |
# | |
# == Examples | |
# This command does blah blah blah. | |
# ruby_cl_skeleton foo.txt | |
# | |
# Other examples: | |
# ruby_cl_skeleton -q bar.doc | |
# ruby_cl_skeleton --verbose foo.html | |
# | |
# == Usage | |
# ruby_cl_skeleton [options] source_file | |
# | |
# For help use: ruby_cl_skeleton -h | |
# | |
# == Options | |
# -h, --help Displays help message | |
# -v, --version Display the version, then exit | |
# -q, --quiet Output as little as possible, overrides verbose | |
# -V, --verbose Verbose output | |
# TO DO - add additional options | |
# | |
# == Author | |
# YourName | |
# | |
# == Copyright | |
# Copyright (c) 2007 YourName. Licensed under the MIT License: | |
# http://www.opensource.org/licenses/mit-license.php | |
# TO DO - replace all ruby_cl_skeleton with your app name | |
# TO DO - replace all YourName with your actual name | |
# TO DO - update Synopsis, Examples, etc | |
# TO DO - change license if necessary | |
require 'optparse' | |
require 'rdoc/usage' | |
require 'ostruct' | |
require 'date' | |
class App | |
VERSION = '0.0.1' | |
attr_reader :options | |
def initialize(arguments, stdin) | |
@arguments = arguments | |
@stdin = stdin | |
# Set defaults | |
@options = OpenStruct.new | |
@options.verbose = false | |
@options.quiet = false | |
# TO DO - add additional defaults | |
end | |
# Parse options, check arguments, then process the command | |
def run | |
if parsed_options? && arguments_valid? | |
puts "Start at #{DateTime.now}\ | |
\ | |
" if @options.verbose | |
output_options if @options.verbose # [Optional] | |
process_arguments | |
process_command | |
puts "\ | |
Finished at #{DateTime.now}" if @options.verbose | |
else | |
output_usage | |
end | |
end | |
protected | |
def parsed_options? | |
# Specify options | |
opts = OptionParser.new | |
opts.on('-v', '--version') { output_version ; exit 0 } | |
opts.on('-h', '--help') { output_help } | |
opts.on('-V', '--verbose') { @options.verbose = true } | |
opts.on('-q', '--quiet') { @options.quiet = true } | |
# TO DO - add additional options | |
opts.parse!(@arguments) rescue return false | |
process_options | |
true | |
end | |
# Performs post-parse processing on options | |
def process_options | |
@options.verbose = false if @options.quiet | |
end | |
def output_options | |
puts "Options:\ | |
" | |
@options.marshal_dump.each do |name, val| | |
puts " #{name} = #{val}" | |
end | |
end | |
# True if required arguments were provided | |
def arguments_valid? | |
# TO DO - implement your real logic here | |
true if @arguments.length == 1 | |
end | |
# Setup the arguments | |
def process_arguments | |
# TO DO - place in local vars, etc | |
end | |
def output_help | |
output_version | |
RDoc::usage() #exits app | |
end | |
def output_usage | |
RDoc::usage('usage') # gets usage from comments above | |
end | |
def output_version | |
puts "#{File.basename(__FILE__)} version #{VERSION}" | |
end | |
def process_command | |
# TO DO - do whatever this app does | |
#process_standard_input # [Optional] | |
end | |
def process_standard_input | |
input = @stdin.read | |
# TO DO - process input | |
# [Optional] | |
# @stdin.each do |line| | |
# # TO DO - process each line | |
#end | |
end | |
end | |
# TO DO - Add your Modules, Classes, etc | |
# Create and run the application | |
app = App.new(ARGV, STDIN) | |
app.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment