Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Start adding command-line options

AuthorDaveJarvis <email>
Date2021-12-02 23:51:20 GMT-0800
Commitde88463c6ca5a91da14b86841264e72d1087e696
Parentfccf923
Delta112 lines added, 0 lines removed, 112-line increase
src/main/java/com/keenwrite/cmdline/Arguments.java
+package com.keenwrite.cmdline;
+
+import com.keenwrite.MainApp;
+import picocli.CommandLine;
+
+import java.util.concurrent.Callable;
+import java.util.logging.LogManager;
+
+@CommandLine.Command(
+ name = "KeenWrite",
+ mixinStandardHelpOptions = true,
+ description = "Plain text editor that offers interpolated variables."
+)
+@SuppressWarnings( "unused" )
+public final class Arguments implements Callable<Integer> {
+ @CommandLine.Option(
+ names = {"-d", "--debug"},
+ description =
+ "Enable logging to the console (${DEFAULT-VALUE}).",
+ arity = "1",
+ paramLabel = "Boolean",
+ defaultValue = "false"
+ )
+ private boolean mDebug;
+
+ @CommandLine.Option(
+ names = {"-i", "--input"},
+ description =
+ "Set the file name to read.",
+ paramLabel = "FILE",
+ defaultValue = "stdin"
+ )
+ private String mFileInput;
+
+ @CommandLine.Option(
+ names = {"-o", "--output"},
+ description =
+ "Set the file name to write.",
+ paramLabel = "FILE",
+ defaultValue = "stdout"
+ )
+ private String mFileOutput;
+
+ @CommandLine.Option(
+ names = {"-m", "--meta-data"},
+ description =
+ "Set a meta-data value.",
+ paramLabel = "Pair"
+ )
+ private String mMetaData;
+
+ @CommandLine.Option(
+ names = {"-v", "--variables"},
+ description =
+ "Set the file name containing variable definitions (${DEFAULT-VALUE}).",
+ paramLabel = "FILE",
+ defaultValue = "variables.yaml"
+ )
+ private String mFileVariables;
+
+ private final String[] mArgs;
+
+ public Arguments( final String[] args ) {
+ mArgs = args;
+ }
+
+ /**
+ * Launches the main application window. This is called when not running
+ * in headless mode.
+ *
+ * @return {@code 0}
+ * @throws Exception The application encountered an unrecoverable error.
+ */
+ @Override
+ public Integer call() throws Exception {
+ if( !mDebug ) {
+ disableLogging();
+ }
+
+ MainApp.main( mArgs );
+ return 0;
+ }
+
+ /**
+ * Suppress logging to standard output and standard error.
+ */
+ private static void disableLogging() {
+ LogManager.getLogManager().reset();
+ System.err.close();
+ }
+}
src/main/java/com/keenwrite/cmdline/ColourScheme.java
+package com.keenwrite.cmdline;
+
+import static picocli.CommandLine.Help.Ansi.Style.*;
+import static picocli.CommandLine.Help.ColorScheme;
+import static picocli.CommandLine.Help.ColorScheme.Builder;
+
+/**
+ * Responsible for creating the command-line parser's colour scheme.
+ */
+public class ColourScheme {
+ public static ColorScheme create() {
+ return new Builder()
+ .commands( bold )
+ .options( fg_blue, bold )
+ .parameters( fg_blue )
+ .optionParams( italic )
+ .errors( fg_red, bold )
+ .stackTraces( italic )
+ .build();
+ }
+}