forked from remkop/picocli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Examples for initializing locale (Java, Kotlin) remkop#1326
- Loading branch information
1 parent
c84fcde
commit a974f94
Showing
6 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
picocli-examples/src/main/java/picocli/examples/i18n/localecontrol/LocaleControl.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package picocli.examples.i18n.localecontrol; | ||
|
||
import picocli.CommandLine; | ||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.Option; | ||
import picocli.CommandLine.Parameters; | ||
import picocli.CommandLine.Unmatched; | ||
|
||
import java.io.File; | ||
import java.math.BigInteger; | ||
import java.nio.file.Files; | ||
import java.security.MessageDigest; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.ResourceBundle; | ||
import java.util.concurrent.Callable; | ||
|
||
class InitLocale { | ||
@Option(names = { "-l", "--locale" }, description = "locale used for message texts (phase 1)") | ||
void setLocale(String locale) { | ||
Locale.setDefault(new Locale(locale)); | ||
} | ||
|
||
@Unmatched | ||
List<String> remainder; // ignore any other parameters and options in the first parsing phase | ||
} | ||
|
||
@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0", | ||
resourceBundle = "picocli.examples.i18n.localecontrol.bundle", | ||
sortOptions = false) | ||
public class LocaleControl implements Callable { | ||
|
||
ResourceBundle bundle = ResourceBundle.getBundle("picocli.examples.i18n.localecontrol.bundle"); | ||
|
||
@Option(names = { "-l", "--locale" }, descriptionKey = "Locale", paramLabel = "<locale>", order = 1) | ||
private String ignored; | ||
|
||
@Parameters(index = "0", descriptionKey = "File") | ||
private File file; | ||
|
||
@Option(names = {"-a", "--algorithm"}, descriptionKey = "Algorithms", order = 2) | ||
private String algorithm = "MD5"; | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
byte[] fileContents = Files.readAllBytes(file.toPath()); | ||
byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents); | ||
System.out.printf("%s: %s%n", bundle.getString("Label_File"), file); | ||
System.out.printf("%s: %s%n", bundle.getString("Label_Algorithm"), algorithm); | ||
System.out.printf("%s: %0" + (digest.length*2) + "x", bundle.getString("Label_Checksum"), new BigInteger(1, digest)); | ||
return 0; | ||
} | ||
|
||
public static void main(String... args) { | ||
// first phase: configure locale | ||
new CommandLine(new InitLocale()).parseArgs(args); | ||
|
||
// second phase: parse all args (ignoring --locale) and run the app | ||
int exitCode = new CommandLine(new LocaleControl()).execute(args); | ||
System.exit(exitCode); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
picocli-examples/src/main/java/picocli/examples/i18n/localecontrol/bundle.properties
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Labels | ||
Label_File = File | ||
Label_Algorithm = Algorithm | ||
Label_Checksum = Checksum | ||
|
||
# Header | ||
usage.headerHeading = Demo: Controlling the locale of message texts%n | ||
usage.header = This application prints the checksum (MD5 by default) | ||
usage.header.0 = of a given file to STDOUT.%n | ||
|
||
# Description | ||
usage.descriptionHeading = %nSynopsis:%n | ||
usage.description = This demo app exemplifies how to control the output | ||
usage.description.0 = of localised messages text from a picocli based command | ||
usage.description.1 = line application. Depending on the specified command | ||
usage.description.2 = line option '--locale', either German or English message | ||
usage.description.3 = texts will be printed out. Specify '--locale=de' as option | ||
usage.description.4 = in order to enforce output of German message texts.%n | ||
|
||
# Parameters | ||
usage.parameterListHeading=Parameters:%n | ||
|
||
# Options | ||
usage.optionListHeading=Options:%n | ||
Locale = Locale for message texts | ||
File = The file whose checksum to calculate. | ||
Algorithms = MD5, SHA-1, SHA-256, ... | ||
|
33 changes: 33 additions & 0 deletions
33
picocli-examples/src/main/java/picocli/examples/i18n/localecontrol/bundle_de.properties
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Labels | ||
Label_File = Datei | ||
Label_Algorithm = Algorithmus | ||
Label_Checksum = Prüfsumme | ||
|
||
# Header | ||
usage.headerHeading = Demo: Kontrolle der Lokale von Meldungstexten%n | ||
usage.header = Diese Anwendung gibt die Prüfsumme (Voreinstellung: MD5) | ||
usage.header.0 = einer Datei auf STDOUT aus.%n | ||
|
||
# Description | ||
usage.descriptionHeading = %nSynopsis:%n | ||
usage.description = Diese Demo-Anwendung veranschaulicht die Kontrolle der Ausgabe | ||
usage.description.0 = von lokalisierten Meldungstexten innerhalb eines picocli-basierten | ||
usage.description.1 = Kommandozeilenprogramms. In Abhängigkeit von der spezifizierten | ||
usage.description.2 = Kommandozeilenoption '--locale' können deutsche oder englische Texte | ||
usage.description.3 = ausgegeben werden. Geben Sie '-locale=en' als Kommandozeilenoption | ||
usage.description.4 = an, um die Ausgabe von englischen Meldungstexten zu erzwingen.%n | ||
|
||
# Synopsis | ||
usage.synopsisHeading=Aufruf:\u0020 | ||
|
||
# Parameters | ||
usage.parameterListHeading=Parameter:%n | ||
|
||
# Options | ||
usage.optionListHeading=Optionen:%n | ||
File = Die Datei, deren Prüfsumme berechnet wird. | ||
Locale = Locale für Meldungstexte ('de' oder 'en') | ||
|
||
# Standard help mixin options: | ||
help = Zeige Hilfemeldung für die Anwendung | ||
version = Zeige Versionsinformation für die Anwendung |
60 changes: 60 additions & 0 deletions
60
picocli-examples/src/main/kotlin/picocli/examples/kotlin/i18n/localecontrol/LocaleControl.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package picocli.examples.kotlin.i18n.localecontrol | ||
|
||
import picocli.CommandLine | ||
import picocli.CommandLine.Command | ||
import picocli.CommandLine.Option | ||
import picocli.CommandLine.Parameters | ||
import picocli.CommandLine.Unmatched | ||
|
||
import java.io.File | ||
import java.lang.Exception | ||
import java.math.BigInteger | ||
import java.nio.file.Files | ||
import java.security.MessageDigest | ||
import java.util.* | ||
import java.util.concurrent.Callable | ||
import kotlin.system.exitProcess | ||
|
||
class InitLocale { | ||
@Option(names = ["-l", "--locale"], description = ["locale used for message texts (phase 1)"]) | ||
fun setLocale(locale: String?) { | ||
Locale.setDefault(Locale(locale)) | ||
} | ||
|
||
// ignore any other parameters and options in the first parsing phase | ||
@Unmatched | ||
var remainder : List<String>? = null | ||
} | ||
|
||
@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"], | ||
resourceBundle = "picocli.examples.i18n.localecontrol.bundle", sortOptions = false) | ||
class LocaleControl : Callable<Int> { | ||
private var bundle: ResourceBundle = ResourceBundle.getBundle("picocli.examples.kotlin.i18n.localecontrol.bundle") | ||
|
||
@Option(names = ["-l", "--locale"], descriptionKey = "Locale", paramLabel = "<locale>", order = 1) | ||
lateinit var ignored: String | ||
|
||
@Parameters(index = "0", descriptionKey = "File") | ||
lateinit var file: File | ||
|
||
@Option(names = ["-a", "--algorithm"], descriptionKey = "Algorithms", order = 2) | ||
var algorithm = "MD5" | ||
|
||
@Throws(Exception::class) | ||
override fun call(): Int { | ||
val fileContents = Files.readAllBytes(file.toPath()) | ||
val digest = MessageDigest.getInstance(algorithm).digest(fileContents) | ||
println("%s: %s".format(bundle.getString("Label_File"), file)) | ||
println("%s: %s".format(bundle.getString("Label_Algorithm"), algorithm)) | ||
println(("%s: %0" + digest.size * 2 + "x").format(bundle.getString("Label_Checksum"), BigInteger(1, digest))) | ||
return 0 | ||
} | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
// first phase: configure locale | ||
CommandLine(InitLocale()).parseArgs(*args) | ||
|
||
// second phase: parse all args (ignoring --locale) and run the app | ||
exitProcess(CommandLine(LocaleControl()).execute(*args)) | ||
} |
28 changes: 28 additions & 0 deletions
28
...cli-examples/src/main/kotlin/picocli/examples/kotlin/i18n/localecontrol/bundle.properties
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Labels | ||
Label_File = File | ||
Label_Algorithm = Algorithm | ||
Label_Checksum = Checksum | ||
|
||
# Header | ||
usage.headerHeading = Demo: Controlling the locale of message texts%n | ||
usage.header = This application prints the checksum (MD5 by default) | ||
usage.header.0 = of a given file to STDOUT.%n | ||
|
||
# Description | ||
usage.descriptionHeading = %nSynopsis:%n | ||
usage.description = This demo app exemplifies how to control the output | ||
usage.description.0 = of localised messages text from a picocli based command | ||
usage.description.1 = line application. Depending on the specified command | ||
usage.description.2 = line option '--locale', either German or English message | ||
usage.description.3 = texts will be printed out. Specify '--locale=de' as option | ||
usage.description.4 = in order to enforce output of German message texts.%n | ||
|
||
# Parameters | ||
usage.parameterListHeading=Parameters:%n | ||
|
||
# Options | ||
usage.optionListHeading=Options:%n | ||
Locale = Locale for message texts | ||
File = The file whose checksum to calculate. | ||
Algorithms = MD5, SHA-1, SHA-256, ... | ||
|
33 changes: 33 additions & 0 deletions
33
...-examples/src/main/kotlin/picocli/examples/kotlin/i18n/localecontrol/bundle_de.properties
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Labels | ||
Label_File = Datei | ||
Label_Algorithm = Algorithmus | ||
Label_Checksum = Prüfsumme | ||
|
||
# Header | ||
usage.headerHeading = Demo: Kontrolle der Lokale von Meldungstexten%n | ||
usage.header = Diese Anwendung gibt die Prüfsumme (Voreinstellung: MD5) | ||
usage.header.0 = einer Datei auf STDOUT aus.%n | ||
|
||
# Description | ||
usage.descriptionHeading = %nSynopsis:%n | ||
usage.description = Diese Demo-Anwendung veranschaulicht die Kontrolle der Ausgabe | ||
usage.description.0 = von lokalisierten Meldungstexten innerhalb eines picocli-basierten | ||
usage.description.1 = Kommandozeilenprogramms. In Abhängigkeit von der spezifizierten | ||
usage.description.2 = Kommandozeilenoption '--locale' können deutsche oder englische Texte | ||
usage.description.3 = ausgegeben werden. Geben Sie '-locale=en' als Kommandozeilenoption | ||
usage.description.4 = an, um die Ausgabe von englischen Meldungstexten zu erzwingen.%n | ||
|
||
# Synopsis | ||
usage.synopsisHeading=Aufruf:\u0020 | ||
|
||
# Parameters | ||
usage.parameterListHeading=Parameter:%n | ||
|
||
# Options | ||
usage.optionListHeading=Optionen:%n | ||
File = Die Datei, deren Prüfsumme berechnet wird. | ||
Locale = Locale für Meldungstexte ('de' oder 'en') | ||
|
||
# Standard help mixin options: | ||
help = Zeige Hilfemeldung für die Anwendung | ||
version = Zeige Versionsinformation für die Anwendung |