Skip to content

Commit 072654b

Browse files
committed
Add programName system property support
1 parent 9108461 commit 072654b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/main/kotlin/com/xenomachina/argparser/SystemExitException.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ open class SystemExitException(message: String, val returnCode: Int) : Exception
5656
}
5757
}
5858

59+
internal val PROGRAM_NAME_PROPERTY = "com.xenomachina.argparser.programName"
60+
5961
/**
6062
* Calls [SystemExitException.printAndExit] on any `SystemExitException` that
6163
* is caught.
6264
*
63-
* @param programName the name of the program, or null if not known
65+
* @param programName the name of the program. If null, the system property com.xenomachina.argparser.programName will
66+
* be used, if set.
6467
* @param columns the number of columns to wrap any caught
6568
* `SystemExitException` to. Specify null for reasonable defaults, or 0 to not
6669
* wrap at all.
@@ -70,6 +73,8 @@ fun <R> mainBody(programName: String? = null, columns: Int? = null, body: () ->
7073
try {
7174
return body()
7275
} catch (e: SystemExitException) {
73-
e.printAndExit(programName, columns ?: System.getenv("COLUMNS")?.toInt() ?: 80)
76+
e.printAndExit(
77+
programName ?: System.getProperty(PROGRAM_NAME_PROPERTY),
78+
columns ?: System.getenv("COLUMNS")?.toInt() ?: 80)
7479
}
7580
}

0 commit comments

Comments
 (0)