Skip to content

Instantly share code, notes, and snippets.

@manchunlam
Created March 16, 2012 15:20
Show Gist options
  • Save manchunlam/2050508 to your computer and use it in GitHub Desktop.
Save manchunlam/2050508 to your computer and use it in GitHub Desktop.
Unix-function-like Ruby Script
#!/usr/bin/env ruby
require 'getoptlong'
version = "0.0.1" # used by the --version or -v option handler
extract_f = false # set to true when --extract or -e are used
extract_args = [] # stores the list of arguments of --extract or -e
remove_f = false # set to true when --remove or -r are used
remove_args = [] # stores the list of arguments of --remove or -r
ex_options_n = 0 # used to store the number of mutually exclusive
# options, when > 1, the script will terminate
have_options_f = false # set to true when at least one option is used
def printusage(error_code)
print "csvt -- extract columns of data from a CSV (Comma-Separate Values) file\n"
print "Usage: csvt [POSIX or GNU style options] file ...\n\n"
print "POSIX options GNU long options\n"
print " -e col[,col][,col]... --extract col[,col][,col]...\n"
print " -r col[,col][,col]... --remove col[,col][,col]...\n"
print " -h --help\n"
print " -u --usage\n"
print " -v --version\n\n"
print "Examples: \n"
print "csvt -e 1,5,6 file print column 1,5 and 6 from file\n"
print "csvt --extract 4,1 file print column 4 and 1 from file\n"
print "csvt -r 2,7,1 file print all columns except 2,7 and 1 from file\n"
print "csvt --remove 6,0 file print all columns except 6 and 0 from file\n"
print "cat file | csvt --remove 6,0 print all columns except 6 and 0 from file\n\n"
print "Send bugs reports to bugs@foo.bar\n"
print "For licensing terms, see source code\n"
exit(error_code)
end
opts = GetoptLong.new(
[ "--extract", "-e", GetoptLong::REQUIRED_ARGUMENT ],
[ "--remove", "-r", GetoptLong::REQUIRED_ARGUMENT ],
[ "--help", "-h", GetoptLong::NO_ARGUMENT ],
[ "--usage", "-u", GetoptLong::NO_ARGUMENT ],
[ "--version", "-v", GetoptLong::NO_ARGUMENT ]
)
begin
opts.each do |opt, arg|
case opt
when "--extract"
extract_f = true
extract_args = arg.split(",")
tmp = 0
extract_args.each do |column|
begin
extract_args[tmp] = Integer(column)
tmp += 1
rescue
$stderr.print "csvt: non-integer column index\n"
printusage(1)
end
end
ex_options_n += 1
have_options_f = true
when "--remove"
remove_f = true
remove_args = arg.split(",")
tmp = 0
remove_args.each do |column|
begin
remove_args[tmp] = Integer(column)
tmp += 1
rescue
$stderr.print "csvt: non-integer column index\n"
printusage(1)
end
end
ex_options_n += 1
have_options_f = true
when "--help"
printusage(0)
when "--usage"
printusage(0)
when "--version"
print "csvt, version ", version, "\n"
exit(0)
end
end
#################################################################
# test for mutually exclusive options: --extract and --remove
if ex_options_n > 1
$stderr.print "csvt: cannot use --extract (-e) and --remove (-r) together\n"
printusage(1)
end
#################################################################
# test for missing options
if have_options_f == false
printusage(1)
end
rescue
printusage(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment