Skip to content

Commit

Permalink
Pass context to HelpFormatter constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Jun 20, 2023
1 parent 64a8b62 commit 7d33b41
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@ abstract class CliktCommand(
?: _context ?: createContext(emptyList(), null, emptyList())
val cmd = ctx.command
val programName = cmd.getCommandNameWithParents()
return ctx.helpFormatter.formatHelp(
ctx,
return ctx.helpFormatter(ctx).formatHelp(
error as? UsageError,
cmd.commandHelp,
cmd.commandHelpEpilog,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Context private constructor(
val autoEnvvarPrefix: String?,
val printExtraMessages: Boolean,
val helpOptionNames: Set<String>,
val helpFormatter: HelpFormatter,
val helpFormatter: (Context) -> HelpFormatter,
val tokenTransformer: Context.(String) -> String,
val terminal: Terminal,
var argumentFileReader: ((filename: String) -> String)?,
Expand Down Expand Up @@ -137,8 +137,8 @@ class Context private constructor(
*/
var helpOptionNames: Iterable<String> = parent?.helpOptionNames ?: setOf("-h", "--help")

/** The help formatter for this command, or null to use the default */
var helpFormatter: HelpFormatter? = parent?.helpFormatter
/** A lambda returning the help formatter for this command, or null to use the default */
var helpFormatter: ((Context) -> HelpFormatter)? = parent?.helpFormatter

/** An optional transformation function that is called to transform command line */
var tokenTransformer: Context.(String) -> String = parent?.tokenTransformer ?: { it }
Expand Down Expand Up @@ -247,7 +247,8 @@ class Context private constructor(
parent?.let { p ->
p.selfAndAncestors().any { it.command.allowMultipleSubcommands }
} != true
val formatter = helpFormatter ?: MordantHelpFormatter()
val formatter: ((Context) -> HelpFormatter) =
helpFormatter ?: { MordantHelpFormatter(it) }
return Context(
parent,
command,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ interface HelpFormatter {
* @param programName The name of the currently executing program
*/
fun formatHelp(
context: Context,
error: UsageError?,
prolog: String,
epilog: String,
Expand Down

0 comments on commit 7d33b41

Please sign in to comment.