Skip to content

Added --version option in the same manner as --help. #70

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion src/main/kotlin/com/xenomachina/argparser/ArgParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import kotlin.reflect.KProperty
class ArgParser(
args: Array<out String>,
mode: Mode = Mode.GNU,
helpFormatter: HelpFormatter? = DefaultHelpFormatter()
helpFormatter: HelpFormatter? = DefaultHelpFormatter(),
version: String? = null
) {

enum class Mode {
Expand Down Expand Up @@ -619,6 +620,13 @@ class ArgParser(
throw ShowHelpException(helpFormatter, delegates.toList())
}.default(Unit).registerRoot()
}
if (version != null) {
option<Unit>("-v", "--version",
errorName = "VERSION", // This should never be used, but we need to say something
help = "show the version and exit") {
throw ShowVersionException(version)
}.default(Unit).registerRoot()
}
}

private val builtinDelegateCount = delegates.size
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/com/xenomachina/argparser/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ class ShowHelpException internal constructor(
}
}

/**
* Indicates that the user requested that the version should be shown (with the
* `--version` option, for example).
*/
class ShowVersionException internal constructor(
private val version: String
) : SystemExitException("version was requested", 0) {
override fun printUserMessage(writer: Writer, programName: String?, columns: Int) {
writer.write(version)
}
}

/**
* Indicates that an unrecognized option was supplied.
*
Expand Down
19 changes: 17 additions & 2 deletions src/test/kotlin/com/xenomachina/argparser/ArgParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,20 @@ This is the epilogue. Lorem ipsum dolor sit amet, consectetur adipiscing elit. D
}
}

test("Version") {
class Args(parser: ArgParser) {
val flag by parser.flagging(help = TEST_HELP)
}

shouldThrow<ShowVersionException> {
Args(parserOf("--version",
version = "1.0.0")).flag
}.run {
val help = StringWriter().apply { printUserMessage(this, "program_name", 60) }.toString()
help shouldBe "1.0.0"
}
}

test("Implicit long flag name") {
class Args(parser: ArgParser) {
val flag1 by parser.flagging(help = TEST_HELP)
Expand Down Expand Up @@ -1680,8 +1694,9 @@ class Circle : Shape()
fun parserOf(
vararg args: String,
mode: ArgParser.Mode = ArgParser.Mode.GNU,
helpFormatter: HelpFormatter? = DefaultHelpFormatter()
) = ArgParser(args, mode, helpFormatter)
helpFormatter: HelpFormatter? = DefaultHelpFormatter(),
version: String? = null
) = ArgParser(args, mode, helpFormatter, version)

/**
* Helper function for getting the static (not runtime) type of an expression. This is useful for verifying that the
Expand Down