From e7a1c1e89bc174b4f3c3999a8924f190d04876ae Mon Sep 17 00:00:00 2001 From: rpopma Date: Sat, 4 Nov 2017 23:11:46 +0900 Subject: [PATCH] Updated Groovy examples for the Groovy Script article. --- .../examples/checksum-with-banner.groovy | 7 +--- .../examples/checksum-without-base.groovy | 35 +++++++++++++++++++ .../groovy/picocli/examples/checksum.groovy | 7 +--- 3 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 examples/src/main/groovy/picocli/examples/checksum-without-base.groovy diff --git a/examples/src/main/groovy/picocli/examples/checksum-with-banner.groovy b/examples/src/main/groovy/picocli/examples/checksum-with-banner.groovy index 38078c7d..89f512a4 100644 --- a/examples/src/main/groovy/picocli/examples/checksum-with-banner.groovy +++ b/examples/src/main/groovy/picocli/examples/checksum-with-banner.groovy @@ -17,10 +17,7 @@ package picocli.examples ) @picocli.groovy.PicocliScript import groovy.transform.Field - -import java.nio.file.Files import java.security.MessageDigest - import static picocli.CommandLine.* @Parameters(arity = "1", paramLabel = "FILE", description = "The file(s) whose checksum to calculate.") @@ -37,7 +34,5 @@ import static picocli.CommandLine.* @Field private boolean versionInfoRequested files.each { - byte[] fileContents = Files.readAllBytes(it.toPath()) - byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents) - println javax.xml.bind.DatatypeConverter.printHexBinary(digest) + "\t" + it + println MessageDigest.getInstance(algorithm).digest(it.bytes).encodeHex().toString() + "\t" + it } diff --git a/examples/src/main/groovy/picocli/examples/checksum-without-base.groovy b/examples/src/main/groovy/picocli/examples/checksum-without-base.groovy new file mode 100644 index 00000000..3f80f621 --- /dev/null +++ b/examples/src/main/groovy/picocli/examples/checksum-without-base.groovy @@ -0,0 +1,35 @@ +package picocli.examples + +@Grab('info.picocli:picocli:2.0.1') +@GrabExclude('org.codehaus.groovy:groovy-all') +import java.security.MessageDigest +import picocli.CommandLine +import static picocli.CommandLine.* + +class Checksum { + @Parameters(arity = "1", paramLabel = "FILE", description = "The file(s) whose checksum to calculate.") + File[] files + + @Option(names = ["-a", "--algorithm"], description = ["MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512,", + " or any other MessageDigest algorithm."]) + String algorithm = "MD5" + + @Option(names = ["-h", "--help"], usageHelp = true, description = "Show this help message and exit.") + boolean helpRequested +} +Checksum checksum = new Checksum() +CommandLine commandLine = new CommandLine(checksum) +try { + commandLine.parse(args) + if (commandLine.usageHelpRequested) { + commandLine.usage(System.out) + } else { + checksum.files.each { + byte[] digest = MessageDigest.getInstance(checksum.algorithm).digest(it.bytes) + println digest.encodeHex().toString() + "\t" + it + } + } +} catch (ParameterException ex) { + println ex.message + commandLine.usage(System.out) +} diff --git a/examples/src/main/groovy/picocli/examples/checksum.groovy b/examples/src/main/groovy/picocli/examples/checksum.groovy index 905e53d2..8f2f48e9 100644 --- a/examples/src/main/groovy/picocli/examples/checksum.groovy +++ b/examples/src/main/groovy/picocli/examples/checksum.groovy @@ -4,10 +4,7 @@ package picocli.examples @GrabExclude('org.codehaus.groovy:groovy-all') @picocli.groovy.PicocliScript import groovy.transform.Field - -import java.nio.file.Files import java.security.MessageDigest - import static picocli.CommandLine.* @Parameters(arity = "1", paramLabel = "FILE", description = "The file(s) whose checksum to calculate.") @@ -21,7 +18,5 @@ import static picocli.CommandLine.* @Field private boolean helpRequested files.each { - byte[] fileContents = Files.readAllBytes(it.toPath()) - byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents) - println javax.xml.bind.DatatypeConverter.printHexBinary(digest) + "\t" + it + println MessageDigest.getInstance(algorithm).digest(it.bytes).encodeHex().toString() + "\t" + it }