mirror of
https://github.com/jlengrand/picocli.git
synced 2026-03-10 08:41:17 +00:00
Updated Groovy examples for the Groovy Script article.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user