Hi
To help the quiz writers focus on the 'the quiz' and not the administrative
part of the code and to help promote CommandLine, here is a little snippet
that some may find useful to get started.
#!/usr/bin/env ruby
require 'rubygems'
require 'commandline'
class NDiffApp < CommandLine::Application
def initialize
author "Jim Freeze"
copyright "2005 Jim Freeze"
synopsis "[-hqs] [-d INT] [-t DBL] file1 file2"
short_description "Numerically compare files line by line, "+
"numerical field by numerical field."
option :names => %w(--digits -d),
:opt_description => "Maximum number of significant digits that "+
"should match. (default: 0)",
:arg_description => "INT",
:opt_found => get_arg,
:opt_not_found => "0"
option :names => %w(--tolerance -t),
:opt_description => "Tolerate <= DBL distance between numbers. "+
"(default: 0.0)",
:arg_description => "DBL",
:opt_found => get_arg,
:opt_not_found => "0.0"
option :flag,
:names => %w(--statistics -s),
:opt_description => "Provide comparison statistics only.
(extra credit)"
option :flag,
:names => %w(--quiet -q),
:opt_description => "No output printed to screen. just exit code.??"
option :help
expected_args :file1, :file2
end
def main
NDiff.new(@file1, @file2,
@option_data["--digits"].to_i,
@option_data["--tolerance"].to_f,
@option_data["--statistics"],
@option_data["--quiet"])
end
end#class NDiffApp
class NDiff
# Your code here
def initialize(file1, file2,
digits=0,
tolerance=0.0,
statistics=false,
quiet=false)
p file1
p file2
p digits
p tolerance
p statistics
p quiet
end
end#class NDiff
The printout is quit nice, and it takes no extr effort:
% ./ndiff
Usage: ndiff [-hqs] [-d INT] [-t DBL] file1 file2
% ./ndiff file1
ERROR: Missing expected arguments. Found 1 but expected 2.
Usage: ndiff [-hqs] [-d INT] [-t DBL] file1 file2
% ./ndiff -h
NAME
ndiff - Numerically compare files line by line, numerical field by
numerical field.
SYNOPSIS
ndiff [-hqs] [-d INT] [-t DBL] file1 file2
OPTIONS
--digits,-d INT
Maximum number of significant digits that should match.
(default: 0)
--tolerance,-t DBL
Tolerate <= DBL distance between numbers. (default: 0.0)
--statistics,-s
Provide comparison statistics only. (extra credit)
--quiet,-q
No output printed to screen. just exit code.??
--help,-h
Displays help page.
AUTHOR: Jim Freeze
COPYRIGHT (c) 2005 Jim Freeze
A little note, I did not understand "just exit code" for -q. What is
'-q' supposed
to do. No output at all?
It also appears that the ability to type cast input to floats or ints or
whatever should be added to CommandLine. All comments are appreciated.
--
Jim Freeze