Skip to content

Instantly share code, notes, and snippets.

@kiy0taka
Created October 10, 2010 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiy0taka/619085 to your computer and use it in GitHub Desktop.
Save kiy0taka/619085 to your computer and use it in GitHub Desktop.
// 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