com.ossuminc.riddl.HelpCommand.scala Maven / Gradle / Ivy
/*
* Copyright 2019 Ossum, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
package com.ossuminc.riddl
import com.ossuminc.riddl.commands.CommandOptions.commandOptionsParser
import com.ossuminc.riddl.commands.CommonOptionsHelper.commonOptionsParser
import com.ossuminc.riddl.commands.CommandOptions
import com.ossuminc.riddl.commands.CommandPlugin
import com.ossuminc.riddl.language.CommonOptions
import com.ossuminc.riddl.language.Messages.Messages
import com.ossuminc.riddl.passes.PassesResult
import com.ossuminc.riddl.utils.Logger
import pureconfig.ConfigCursor
import pureconfig.ConfigReader
import scopt.OParser
import scopt.RenderingMode.OneColumn
import java.nio.file.Path
/** Unit Tests For FromCommand */
object HelpCommand {
case class Options(
command: String = "help",
inputFile: Option[Path] = None,
targetCommand: Option[String] = None)
extends CommandOptions
}
class HelpCommand extends CommandPlugin[HelpCommand.Options]("help") {
import HelpCommand.Options
override def getOptions: (OParser[Unit, Options], Options) = {
import builder.*
cmd(pluginName).action((_, c) => c.copy(command = pluginName))
.text("Print out how to use this program") -> HelpCommand.Options()
}
override def getConfigReader: ConfigReader[HelpCommand.Options] = {
(cur: ConfigCursor) =>
for
topCur <- cur.asObjectCursor
topRes <- topCur.atKey(pluginName)
cmd <- topRes.asObjectCursor
yield { Options(cmd.path) }
}
override def run(
options: HelpCommand.Options,
commonOptions: CommonOptions,
log: Logger,
outputDirOverride: Option[Path]
): Either[Messages, PassesResult] = {
if commonOptions.verbose || !commonOptions.quiet then {
val usage: String = {
val common = OParser.usage(commonOptionsParser, OneColumn)
val commands = OParser.usage(commandOptionsParser, OneColumn)
val improved_commands = commands.split(System.lineSeparator())
.flatMap { line =>
if line.isEmpty || line.forall(_.isWhitespace) then {
Seq.empty[String]
} else if line.startsWith("Command:") then {
Seq(System.lineSeparator() + line)
} else if line.startsWith("Usage:") then { Seq(line) }
else { Seq(" " + line) }
}.mkString(System.lineSeparator())
common ++ "\n\n" ++ improved_commands
}
println(usage)
}
Right(PassesResult())
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy