forked from pinterest/ktlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.kt
48 lines (41 loc) · 2.01 KB
/
Main.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@file:JvmName("Main")
package com.pinterest.ktlint
import com.pinterest.ktlint.internal.GenerateEditorConfigSubCommand
import com.pinterest.ktlint.internal.GitPreCommitHookSubCommand
import com.pinterest.ktlint.internal.GitPrePushHookSubCommand
import com.pinterest.ktlint.internal.KtlintCommandLine
import com.pinterest.ktlint.internal.PrintASTSubCommand
import com.pinterest.ktlint.internal.printCommandLineHelpOrVersionUsage
import picocli.CommandLine
public fun main(args: Array<String>) {
val ktlintCommand = KtlintCommandLine()
val commandLine = CommandLine(ktlintCommand)
.addSubcommand(GitPreCommitHookSubCommand.COMMAND_NAME, GitPreCommitHookSubCommand())
.addSubcommand(GitPrePushHookSubCommand.COMMAND_NAME, GitPrePushHookSubCommand())
.addSubcommand(PrintASTSubCommand.COMMAND_NAME, PrintASTSubCommand())
.addSubcommand(GenerateEditorConfigSubCommand.COMMAND_NAME, GenerateEditorConfigSubCommand())
// Keep setUsageHelpAutoWidth after all addSubcommands
.setUsageHelpAutoWidth(true)
val parseResult = commandLine.parseArgs(*args)
// The logger needs to be configured for the ktlintCommand and all subcommands. The logger can however not be configured before the
// commandline has been parsed as otherwise the loglevel conversion is not yet executed.
ktlintCommand.configureLogger()
commandLine.printCommandLineHelpOrVersionUsage()
if (parseResult.hasSubcommand()) {
handleSubCommand(commandLine, parseResult)
} else {
ktlintCommand.run()
}
}
private fun handleSubCommand(
commandLine: CommandLine,
parseResult: CommandLine.ParseResult,
) {
when (val subCommand = parseResult.subcommand().commandSpec().userObject()) {
is GitPreCommitHookSubCommand -> subCommand.run()
is GitPrePushHookSubCommand -> subCommand.run()
is PrintASTSubCommand -> subCommand.run()
is GenerateEditorConfigSubCommand -> subCommand.run()
else -> commandLine.usage(System.out, CommandLine.Help.Ansi.OFF)
}
}