Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --help support for subcommands #178

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions mainargs/src/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ class ParserForMethods[B](val mains: MethodMains[B]) {
customNames: Map[String, String] = Map(),
customDocs: Map[String, String] = Map(),
sorted: Boolean = true,
nameMapper: String => Option[String] = Util.kebabCaseNameMapper
nameMapper: String => Option[String] = Util.kebabCaseNameMapper,
mainMethods: Seq[MainData[Any, B]] = mains.value
): String = {
Renderer.formatMainMethods(
mains.value,
mainMethods,
totalWidth,
docsOnNewLine,
customNames,
Expand All @@ -36,6 +37,33 @@ class ParserForMethods[B](val mains: MethodMains[B]) {
)
}

def handlePrintAndExit(
autoPrintHelpAndExit: Option[(Int, PrintStream)],
args: Seq[String],
totalWidth: Int,
docsOnNewLine: Boolean,
customNames: Map[String, String],
customDocs: Map[String, String],
sorted: Boolean,
nameMapper: String => Option[String]
): Unit = {
autoPrintHelpAndExit.foreach { case (exitCode, outputStream) =>
val mainHelp = args.headOption.contains("--help")
val subcommand = mains.value.find(main => args.headOption.contains(main.name(nameMapper)))
val subcommandHelp = subcommand.isDefined && args.lift(1).contains("--help")

if (mainHelp || subcommandHelp) {
val text = if (mainHelp) {
helpText(totalWidth, docsOnNewLine, customNames, customDocs, sorted, nameMapper)
} else {
helpText(totalWidth, docsOnNewLine, customNames, customDocs, sorted, nameMapper, subcommand.toSeq)
}
outputStream.println(text)
Compat.exit(exitCode)
}
}
}

@deprecated("Binary compatibility shim, use other overload instead", "mainargs after 0.3.0")
private[mainargs] def helpText(
totalWidth: Int,
Expand Down Expand Up @@ -171,11 +199,8 @@ class ParserForMethods[B](val mains: MethodMains[B]) {
sorted: Boolean = true,
nameMapper: String => Option[String] = Util.kebabCaseNameMapper
): Either[String, Any] = {
if (autoPrintHelpAndExit.nonEmpty && args.take(1) == Seq("--help")) {
val (exitCode, outputStream) = autoPrintHelpAndExit.get
outputStream.println(helpText(totalWidth, docsOnNewLine, customNames, customDocs, sorted, nameMapper))
Compat.exit(exitCode)
} else runRaw0(args, allowPositional, allowRepeats, nameMapper) match {
handlePrintAndExit(autoPrintHelpAndExit, args, totalWidth, docsOnNewLine, customNames, customDocs, sorted, nameMapper)
runRaw0(args, allowPositional, allowRepeats, nameMapper) match {
case Left(err) => Left(Renderer.renderEarlyError(err))
case Right((main, res)) =>
res match {
Expand Down