Created
October 10, 2010 08:25
-
-
Save kiy0taka/619085 to your computer and use it in GitHub Desktop.
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
// g100pon #46 wc | |
cli = new CliBuilder(usage:"wc [-clmw] [file ...]") | |
cli.with { | |
c longOpt:'bytes', 'The number of bytes in each input file is written to the standard output. This will cancel out any prior usage of the -m option.' | |
l longOpt:'length', 'The number of lines in each input file is written to the standard output.' | |
m longOpt:'characters', 'The number of characters in each input file is written to the standard output. If the current locale does not support multibyte characters, this is equivalent to the -c option. This will cancel out any prior usage of the -c option.' | |
w longOpt:'words', 'The number of words in each input file is written to the standard output.' | |
} | |
opt = cli.parse(args) | |
if (!opt.arguments().size()) { | |
cli.usage() | |
System.exit 0 | |
} | |
def result = [] | |
opt.arguments().each { | |
def list = [] | |
if (opt.l) list << (new File(it).text =~ /(?m)(^.*$)/).size().toString().padLeft(6) | |
if (opt.w) list << (new File(it).text =~ /(\S+\s*)/).size().toString().padLeft(6) | |
if (opt.c || opt.m) list << new File(it).size().toString().padLeft(6) | |
list << ' ' + it | |
result << list.join('') | |
} | |
println result.join('\n') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment