Created
February 4, 2012 07:03
-
-
Save i8degrees/1735982 to your computer and use it in GitHub Desktop.
OptParse Skel
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 | |
# | |
# ~/Projects/homnom/nomnom-v0.2/tmp/cmdline-args.rb:jeff | |
# | |
# Preliminary play with a new input arguments parser for much enhanced | |
# console-level control of our I/O scripts. | |
# | |
# Usage: | |
# | |
# http://ruby-doc.org/stdlib-1.9.3/libdoc/optparse/rdoc/OptionParser.html | |
# | |
# 2010-12/16: (c) Copyright 2010 Jeffrey Carpenter, All Rights Reserved | |
# jeffrey.carp at gmail dot com | |
# | |
# LICENSE: BSD | |
# | |
# TODO | |
# | |
# [+] ... | |
# | |
require 'optparse' | |
require 'ostruct' | |
require 'date' | |
class App | |
VERSION = '0.0.1' | |
attr_reader :options | |
# Incoming input materializes our instance | |
def initialize(arguments, stdin) | |
@arguments = arguments | |
@stdin = stdin | |
# Initialize our defaults for passing parameters | |
@options = OpenStruct.new | |
@options.verbose = false | |
@options.quiet = false | |
# ... | |
end | |
# Final loop | |
def run | |
if parsed_options? && arguments_valid? | |
puts "Start at #{DateTime.now}\n\n" if @options.verbose | |
output_options if @options.verbose | |
process_arguments | |
process_command | |
puts "\nFinished at #{DateTime.now}" if @options.verbose | |
else | |
output_usage | |
end | |
end | |
protected | |
def parsed_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 } | |
# ... | |
opts.parse!(@arguments) rescue return false | |
process_options | |
true | |
end | |
def process_options | |
if @options.quiet | |
@options.verbose = false | |
end | |
end | |
def output_options | |
puts "Options:\n" | |
#@options.each do |name, value| | |
# pp " #{name} = #{val}" | |
#end | |
@options.marshal_dump.each do |name, value| | |
puts " #{name} = #{val}" | |
end | |
end | |
def arguments_valid? | |
if @arguments.length == 1 | |
return true | |
end | |
end | |
def process_arguments | |
# Setup the intended functionality of program arguments | |
# ... | |
end | |
def output_help | |
output_version | |
puts "Help: stub" | |
end | |
def output_usage | |
puts "Usage: stub" | |
end | |
def output_version | |
puts "#{File.basename(__FILE__)} version #{VERSION}" | |
end | |
def process_command | |
# ...what are we tasked with doing again?? | |
end | |
def process_standard_input | |
input = @stdin.read | |
# Process input | |
@stdin.each do |line| | |
# ... | |
end | |
end | |
end | |
# Main executing loop; we start and end here. | |
def main | |
app = App.new(ARGV, STDIN) | |
app.run | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment