From 4de7092225adf3c5f3cfc748bdfb8b433e2ed069 Mon Sep 17 00:00:00 2001 From: Noel Date: Wed, 25 May 2022 14:37:38 -0700 Subject: [PATCH] feat(v1.1-beta): add integer, bool, and file flag support! :D --- build.gradle.kts | 2 +- .../-ai-command/-ai-command.html | 6 +- .../-ai-command/-companion/index.html | 6 +- .../-ai-command/add-subcommand.html | 6 +- .../-ai-command/add-subcommands.html | 6 +- docs/ai/org.noelware.ai/-ai-command/arg.html | 8 +- .../org.noelware.ai/-ai-command/context.html | 6 +- docs/ai/org.noelware.ai/-ai-command/flag.html | 8 +- .../ai/org.noelware.ai/-ai-command/index.html | 78 ++++++++-- docs/ai/org.noelware.ai/-ai-command/main.html | 12 +- docs/ai/org.noelware.ai/-ai-command/run.html | 8 +- .../-ai-command/set-context.html | 6 +- .../org.noelware.ai/-ai-context/command.html | 2 +- .../ai/org.noelware.ai/-ai-context/index.html | 2 +- .../-ai-phase/-f-a-i-l/index.html | 111 ++++++++++++++ .../-ai-phase/-f-i-n-i-s-h-e-d/index.html | 21 ++- .../-p-r-i-n-t_-u-s-a-g-e/index.html | 21 ++- .../org.noelware.ai/-ai-phase/exit-code.html | 72 +++++++++ docs/ai/org.noelware.ai/-ai-phase/index.html | 40 ++++- .../-default-ai-context.html | 2 +- .../-default-ai-context/command.html | 2 +- .../-default-ai-context/index.html | 2 +- docs/ai/org.noelware.ai/bool.html | 72 +++++++++ docs/ai/org.noelware.ai/file.html | 72 +++++++++ docs/ai/org.noelware.ai/index.html | 68 ++++++++- docs/ai/org.noelware.ai/int-or-null.html | 72 +++++++++ docs/ai/org.noelware.ai/int.html | 72 +++++++++ docs/ai/org.noelware.ai/string-or-null.html | 8 +- docs/ai/org.noelware.ai/string.html | 8 +- docs/ai/package-list | 6 + docs/index.html | 2 +- docs/navigation.html | 25 +++- docs/scripts/pages.json | 2 +- .../org/noelware/ai/example/ExampleCli.kt | 2 +- src/main/kotlin/org/noelware/ai/AiCommand.kt | 141 +++++++++++++++++- src/main/kotlin/org/noelware/ai/AiPhase.kt | 11 +- 36 files changed, 897 insertions(+), 91 deletions(-) create mode 100644 docs/ai/org.noelware.ai/-ai-phase/-f-a-i-l/index.html create mode 100644 docs/ai/org.noelware.ai/-ai-phase/exit-code.html create mode 100644 docs/ai/org.noelware.ai/bool.html create mode 100644 docs/ai/org.noelware.ai/file.html create mode 100644 docs/ai/org.noelware.ai/int-or-null.html create mode 100644 docs/ai/org.noelware.ai/int.html diff --git a/build.gradle.kts b/build.gradle.kts index 91dc5a7..dff72f2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -56,7 +56,7 @@ plugins { kotlin("jvm") version "1.6.21" } -val VERSION = Version(1, 0, 0, 0, dev.floofy.utils.gradle.ReleaseType.Beta) +val VERSION = Version(1, 1, 0, 0, dev.floofy.utils.gradle.ReleaseType.Beta) val DOKKA_OUTPUT = "${rootProject.projectDir}/docs" val JAVA_VERSION = JavaVersion.VERSION_17 diff --git a/docs/ai/org.noelware.ai/-ai-command/-ai-command.html b/docs/ai/org.noelware.ai/-ai-command/-ai-command.html index 7af6712..3578228 100644 --- a/docs/ai/org.noelware.ai/-ai-command/-ai-command.html +++ b/docs/ai/org.noelware.ai/-ai-command/-ai-command.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -49,11 +49,11 @@

AiCommand
fun AiCommand(    name: String = inferName(),     help: String = "",     usage: String? = null,     printUsageIfError: Boolean = false)

Parameters

name

The name of the command, or it'll be inferred by the class name.

help

The documentation of this command.

usage

The usage of the command, can be left null to be automatically generated.

printUsageIfError

If the command should print the usage if an exception had occurred.

Sources

-
+
- +
Link copied to clipboard
diff --git a/docs/ai/org.noelware.ai/-ai-command/-companion/index.html b/docs/ai/org.noelware.ai/-ai-command/-companion/index.html index 94fc68f..a0270a9 100644 --- a/docs/ai/org.noelware.ai/-ai-command/-companion/index.html +++ b/docs/ai/org.noelware.ai/-ai-command/-companion/index.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -52,11 +52,11 @@

Companion

Sources

-
+
- +
Link copied to clipboard
diff --git a/docs/ai/org.noelware.ai/-ai-command/add-subcommand.html b/docs/ai/org.noelware.ai/-ai-command/add-subcommand.html index bf67e5d..fc6740d 100644 --- a/docs/ai/org.noelware.ai/-ai-command/add-subcommand.html +++ b/docs/ai/org.noelware.ai/-ai-command/add-subcommand.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -49,11 +49,11 @@

addSubcommand
fun addSubcommand(command: AiCommand)

Adds a subcommand to this AiCommand, making the subcommand's parent this command object.

Parameters

command

The subcommand to add.

Sources

-
+
- +
Link copied to clipboard
diff --git a/docs/ai/org.noelware.ai/-ai-command/add-subcommands.html b/docs/ai/org.noelware.ai/-ai-command/add-subcommands.html index 743f040..8552e80 100644 --- a/docs/ai/org.noelware.ai/-ai-command/add-subcommands.html +++ b/docs/ai/org.noelware.ai/-ai-command/add-subcommands.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -49,11 +49,11 @@

addSubcommands
fun addSubcommands(vararg commands: AiCommand)

Adds more than one subcommands to this AiCommand, making the subcommand's parent this command object.

Parameters

commands

The list of subcommands to add.

Sources

-
+
- +
Link copied to clipboard
diff --git a/docs/ai/org.noelware.ai/-ai-command/arg.html b/docs/ai/org.noelware.ai/-ai-command/arg.html index 3d9203d..51a2d98 100644 --- a/docs/ai/org.noelware.ai/-ai-command/arg.html +++ b/docs/ai/org.noelware.ai/-ai-command/arg.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -47,13 +47,13 @@

arg

-
fun arg(    name: String,     help: String = "",     longName: String? = null,     required: Boolean = false)
+
fun arg(    name: String,     help: String = "",     longName: String? = null,     required: Boolean = false)

Adds an argument flag, that must require a value (if needed).

Parameters

name

The name of the argument flag.

help

The help message of this argument flag.

longName

The long name to use.

required

If the argument flag is required or not.

Sources

-
+
- +
Link copied to clipboard
diff --git a/docs/ai/org.noelware.ai/-ai-command/context.html b/docs/ai/org.noelware.ai/-ai-command/context.html index 01dd8d9..32a1688 100644 --- a/docs/ai/org.noelware.ai/-ai-command/context.html +++ b/docs/ai/org.noelware.ai/-ai-command/context.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -49,11 +49,11 @@

context

val context: AiContext

Returns the current context object that this command has access to. Use the setContext function to use a different context.

Sources

-
+
- +
Link copied to clipboard
diff --git a/docs/ai/org.noelware.ai/-ai-command/flag.html b/docs/ai/org.noelware.ai/-ai-command/flag.html index 41a7096..f252958 100644 --- a/docs/ai/org.noelware.ai/-ai-command/flag.html +++ b/docs/ai/org.noelware.ai/-ai-command/flag.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -47,13 +47,13 @@

flag

-
fun flag(    name: String,     help: String = "",     longName: String? = null,     required: Boolean = false)
+
fun flag(    name: String,     help: String = "",     longName: String? = null,     required: Boolean = false)

Adds a boolean flag, that must require a value (if needed).

Parameters

name

The name of the argument flag.

help

The help message of this argument flag.

longName

The long name to use.

required

If the argument flag is required or not.

Sources

-
+
- +
Link copied to clipboard
diff --git a/docs/ai/org.noelware.ai/-ai-command/index.html b/docs/ai/org.noelware.ai/-ai-command/index.html index d21ffdb..ac62e65 100644 --- a/docs/ai/org.noelware.ai/-ai-command/index.html +++ b/docs/ai/org.noelware.ai/-ai-command/index.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -130,7 +130,7 @@

Functions

-
fun arg(    name: String,     help: String = "",     longName: String? = null,     required: Boolean = false)
+
fun arg(    name: String,     help: String = "",     longName: String? = null,     required: Boolean = false)

Adds an argument flag, that must require a value (if needed).

@@ -145,7 +145,7 @@

Functions

-
fun flag(    name: String,     help: String = "",     longName: String? = null,     required: Boolean = false)
+
fun flag(    name: String,     help: String = "",     longName: String? = null,     required: Boolean = false)

Adds a boolean flag, that must require a value (if needed).

@@ -160,7 +160,7 @@

Functions

-
fun main(args: Array<out String>)
fun main(args: List<String>)
+
fun main(args: Array<out String>)
fun main(args: List<String>)

Executes the command with the arguments used.

@@ -214,7 +214,67 @@

Properties

Extensions

-
+
+
+
+
+ + +
Link copied to clipboard
+
+
+
+
fun AiCommand.bool(name: String): ReadOnlyProperty<Any?, Boolean>

Returns the option as a boolean, if it has the option, or false.

+
+
+
+
+ +
+
+
+ + +
Link copied to clipboard
+
+
+
+
fun AiCommand.file(    name: String,     mustBeDir: Boolean = false,     mustBeFile: Boolean = false,     mustExist: Boolean = true): ReadOnlyProperty<Any?, File>

Returns the option as a file, if the name is an absolute path.

+
+
+
+
+ +
+
+
+ + +
Link copied to clipboard
+
+
+
+
fun AiCommand.int(name: String): ReadOnlyProperty<Any?, Int>

Returns the option as a string, if it wasn't found, then it throws an IllegalStateException

+
+
+
+
+ +
+
+
+ + +
Link copied to clipboard
+
+
+
+
fun AiCommand.intOrNull(name: String): ReadOnlyProperty<Any?, Int?>

Returns the option as an integer, if it has the option, or null.

+
+
+
+
+
@@ -224,7 +284,7 @@

Extensions

-
fun AiCommand.string(name: String): String
+
fun AiCommand.string(name: String): ReadOnlyProperty<Any?, String>

Returns the option as a string, if it wasn't found, then it throws an IllegalStateException

@@ -239,18 +299,18 @@

Extensions

-
fun AiCommand.stringOrNull(name: String): String?
+
fun AiCommand.stringOrNull(name: String): ReadOnlyProperty<Any?, String?>

Returns the option as a string, if it has the option or null.

Sources

-
+
- +
Link copied to clipboard
diff --git a/docs/ai/org.noelware.ai/-ai-command/main.html b/docs/ai/org.noelware.ai/-ai-command/main.html index b1ace0b..c378be7 100644 --- a/docs/ai/org.noelware.ai/-ai-command/main.html +++ b/docs/ai/org.noelware.ai/-ai-command/main.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -47,24 +47,24 @@

main

-
fun main(args: Array<out String>)
fun main(args: List<String>)
+
fun main(args: Array<out String>)
fun main(args: List<String>)

Executes the command with the arguments used.

Example

object MyCommand: AiCommand("owo", "uwu!") {
override fun run(args: List<String>): AiPhase = AiPhase.PRINT_USAGE
}

fun main(args: Array<String>) = MyCommand.main(args)

Sources

-
+
- +
Link copied to clipboard
- +
- +
Link copied to clipboard
diff --git a/docs/ai/org.noelware.ai/-ai-command/run.html b/docs/ai/org.noelware.ai/-ai-command/run.html index 0df6cf0..cb4d3bd 100644 --- a/docs/ai/org.noelware.ai/-ai-command/run.html +++ b/docs/ai/org.noelware.ai/-ai-command/run.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -47,13 +47,13 @@

run

-
abstract fun run(args: List<String>): AiPhase

Executes the command and returns the phase of the command as the return type.

The AiPhase refers to what we need to do next once we have ran the command. The following phases are:

Parameters

args

The left-over, positional arguments available.

+
abstract fun run(args: List<String>): AiPhase

Executes the command and returns the phase of the command as the return type.

The AiPhase refers to what we need to do next once we have ran the command. The following phases are:

Parameters

args

The left-over, positional arguments available.

Sources

-
+
- +
Link copied to clipboard
diff --git a/docs/ai/org.noelware.ai/-ai-command/set-context.html b/docs/ai/org.noelware.ai/-ai-command/set-context.html index 6154d33..43cf30c 100644 --- a/docs/ai/org.noelware.ai/-ai-command/set-context.html +++ b/docs/ai/org.noelware.ai/-ai-command/set-context.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -49,11 +49,11 @@

setContext
fun <T : AiContext> setContext(ctx: T)

Sets the current context of this AiCommand.

Parameters

ctx

The context object to set.

T

The AiContext object to use.

Sources

-
+
- +
Link copied to clipboard
diff --git a/docs/ai/org.noelware.ai/-ai-context/command.html b/docs/ai/org.noelware.ai/-ai-context/command.html index b50fe20..ce676a3 100644 --- a/docs/ai/org.noelware.ai/-ai-context/command.html +++ b/docs/ai/org.noelware.ai/-ai-context/command.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
diff --git a/docs/ai/org.noelware.ai/-ai-context/index.html b/docs/ai/org.noelware.ai/-ai-context/index.html index 25a0664..938541f 100644 --- a/docs/ai/org.noelware.ai/-ai-context/index.html +++ b/docs/ai/org.noelware.ai/-ai-context/index.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
diff --git a/docs/ai/org.noelware.ai/-ai-phase/-f-a-i-l/index.html b/docs/ai/org.noelware.ai/-ai-phase/-f-a-i-l/index.html new file mode 100644 index 0000000..a8628a2 --- /dev/null +++ b/docs/ai/org.noelware.ai/-ai-phase/-f-a-i-l/index.html @@ -0,0 +1,111 @@ + + + + + FAIL + + + + + + + + + + + + + + + + + +
+
+
+
+
+
+ +
+

FAIL

+
FAIL(1)

That command has exited with a failed exit code.

+
+
+
+
+

Properties

+
+
+
+
+ + +
Link copied to clipboard
+
+
+
+
val exitCode: Int
+
+
+
+
+ +
+
+
+ + +
Link copied to clipboard
+
+
+
+
val name: String
+
+
+
+
+ +
+
+
+ + +
Link copied to clipboard
+
+
+
+
val ordinal: Int
+
+
+
+
+
+
+
+
+ +
+
+ + diff --git a/docs/ai/org.noelware.ai/-ai-phase/-f-i-n-i-s-h-e-d/index.html b/docs/ai/org.noelware.ai/-ai-phase/-f-i-n-i-s-h-e-d/index.html index 93f0dbc..fed8a9a 100644 --- a/docs/ai/org.noelware.ai/-ai-phase/-f-i-n-i-s-h-e-d/index.html +++ b/docs/ai/org.noelware.ai/-ai-phase/-f-i-n-i-s-h-e-d/index.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -46,13 +46,28 @@

FINISHED

-

The command has finished.

+

The command has finished.

Properties

-
+
+
+
+
+ + +
Link copied to clipboard
+
+
+
+
val exitCode: Int
+
+
+
+
+
diff --git a/docs/ai/org.noelware.ai/-ai-phase/-p-r-i-n-t_-u-s-a-g-e/index.html b/docs/ai/org.noelware.ai/-ai-phase/-p-r-i-n-t_-u-s-a-g-e/index.html index cd93ce3..ce6bcdd 100644 --- a/docs/ai/org.noelware.ai/-ai-phase/-p-r-i-n-t_-u-s-a-g-e/index.html +++ b/docs/ai/org.noelware.ai/-ai-phase/-p-r-i-n-t_-u-s-a-g-e/index.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -46,13 +46,28 @@

PRINT_USAGE

-

The command wants to print its usage.

+

The command wants to print its usage.

Properties

-
+
+
+
+
+ + +
Link copied to clipboard
+
+
+
+
val exitCode: Int
+
+
+
+
+
diff --git a/docs/ai/org.noelware.ai/-ai-phase/exit-code.html b/docs/ai/org.noelware.ai/-ai-phase/exit-code.html new file mode 100644 index 0000000..1b92bfb --- /dev/null +++ b/docs/ai/org.noelware.ai/-ai-phase/exit-code.html @@ -0,0 +1,72 @@ + + + + + exitCode + + + + + + + + + + + + + + + + + +
+
+
+
+
+
+ +
+

exitCode

+
+
val exitCode: Int
+

Sources

+
+
+
+
+ + +
Link copied to clipboard
+
+
+
+
+
+
+ +
+
+ + diff --git a/docs/ai/org.noelware.ai/-ai-phase/index.html b/docs/ai/org.noelware.ai/-ai-phase/index.html index 0d0f68c..71f3cfb 100644 --- a/docs/ai/org.noelware.ai/-ai-phase/index.html +++ b/docs/ai/org.noelware.ai/-ai-phase/index.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -52,7 +52,22 @@

AiPhase

Entries

-
+
+
+
+
+ + +
Link copied to clipboard
+
+
+
+
FAIL(1)

That command has exited with a failed exit code.

+
+
+
+
+
@@ -62,7 +77,7 @@

Entries

-

The command wants to print its usage.

+

The command wants to print its usage.

@@ -77,14 +92,29 @@

Entries

-

The command has finished.

+

The command has finished.

Properties

-
+
+
+
+
+ + +
Link copied to clipboard
+
+
+
+
val exitCode: Int
+
+
+
+
+
diff --git a/docs/ai/org.noelware.ai/-default-ai-context/-default-ai-context.html b/docs/ai/org.noelware.ai/-default-ai-context/-default-ai-context.html index ea88ed6..34393e8 100644 --- a/docs/ai/org.noelware.ai/-default-ai-context/-default-ai-context.html +++ b/docs/ai/org.noelware.ai/-default-ai-context/-default-ai-context.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
diff --git a/docs/ai/org.noelware.ai/-default-ai-context/command.html b/docs/ai/org.noelware.ai/-default-ai-context/command.html index a73279f..b182af9 100644 --- a/docs/ai/org.noelware.ai/-default-ai-context/command.html +++ b/docs/ai/org.noelware.ai/-default-ai-context/command.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
diff --git a/docs/ai/org.noelware.ai/-default-ai-context/index.html b/docs/ai/org.noelware.ai/-default-ai-context/index.html index 41fa1e3..594226e 100644 --- a/docs/ai/org.noelware.ai/-default-ai-context/index.html +++ b/docs/ai/org.noelware.ai/-default-ai-context/index.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
diff --git a/docs/ai/org.noelware.ai/bool.html b/docs/ai/org.noelware.ai/bool.html new file mode 100644 index 0000000..2fa221d --- /dev/null +++ b/docs/ai/org.noelware.ai/bool.html @@ -0,0 +1,72 @@ + + + + + bool + + + + + + + + + + + + + + + + + +
+
+
+
+
+
+ +
+

bool

+
+
fun AiCommand.bool(name: String): ReadOnlyProperty<Any?, Boolean>

Returns the option as a boolean, if it has the option, or false.

+

Sources

+
+
+
+
+ + +
Link copied to clipboard
+
+
+
+
+
+
+ +
+
+ + diff --git a/docs/ai/org.noelware.ai/file.html b/docs/ai/org.noelware.ai/file.html new file mode 100644 index 0000000..82e4bdb --- /dev/null +++ b/docs/ai/org.noelware.ai/file.html @@ -0,0 +1,72 @@ + + + + + file + + + + + + + + + + + + + + + + + +
+
+
+
+
+
+ +
+

file

+
+
fun AiCommand.file(    name: String,     mustBeDir: Boolean = false,     mustBeFile: Boolean = false,     mustExist: Boolean = true): ReadOnlyProperty<Any?, File>

Returns the option as a file, if the name is an absolute path.

Parameters

name

The name of the argument

mustBeDir

If the file must be a directory, cannot be linked with mustBeFile.

mustBeFile

If the fist MUST be a file object, cannot be linked with mustBeDir.

mustExist

If the file MUST exist, if not, it'll bark.

+

Sources

+
+
+
+
+ + +
Link copied to clipboard
+
+
+
+
+
+
+ +
+
+ + diff --git a/docs/ai/org.noelware.ai/index.html b/docs/ai/org.noelware.ai/index.html index 476de17..3c70bef 100644 --- a/docs/ai/org.noelware.ai/index.html +++ b/docs/ai/org.noelware.ai/index.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -113,7 +113,67 @@

Types

Functions

-
+
+
+
+
+ + +
Link copied to clipboard
+
+
+
+
fun AiCommand.bool(name: String): ReadOnlyProperty<Any?, Boolean>

Returns the option as a boolean, if it has the option, or false.

+
+
+
+
+ +
+
+
+ + +
Link copied to clipboard
+
+
+
+
fun AiCommand.file(    name: String,     mustBeDir: Boolean = false,     mustBeFile: Boolean = false,     mustExist: Boolean = true): ReadOnlyProperty<Any?, File>

Returns the option as a file, if the name is an absolute path.

+
+
+
+
+ +
+
+
+ + +
Link copied to clipboard
+
+
+
+
fun AiCommand.int(name: String): ReadOnlyProperty<Any?, Int>

Returns the option as a string, if it wasn't found, then it throws an IllegalStateException

+
+
+
+
+ +
+
+
+ + +
Link copied to clipboard
+
+
+
+
fun AiCommand.intOrNull(name: String): ReadOnlyProperty<Any?, Int?>

Returns the option as an integer, if it has the option, or null.

+
+
+
+
+
@@ -123,7 +183,7 @@

Functions

-
fun AiCommand.string(name: String): String
+
fun AiCommand.string(name: String): ReadOnlyProperty<Any?, String>

Returns the option as a string, if it wasn't found, then it throws an IllegalStateException

@@ -138,7 +198,7 @@

Functions

-
fun AiCommand.stringOrNull(name: String): String?
+
fun AiCommand.stringOrNull(name: String): ReadOnlyProperty<Any?, String?>

Returns the option as a string, if it has the option or null.

diff --git a/docs/ai/org.noelware.ai/int-or-null.html b/docs/ai/org.noelware.ai/int-or-null.html new file mode 100644 index 0000000..3331f52 --- /dev/null +++ b/docs/ai/org.noelware.ai/int-or-null.html @@ -0,0 +1,72 @@ + + + + + intOrNull + + + + + + + + + + + + + + + + + +
+
+
+
+
+
+ +
+

intOrNull

+
+
fun AiCommand.intOrNull(name: String): ReadOnlyProperty<Any?, Int?>

Returns the option as an integer, if it has the option, or null.

+

Sources

+
+
+
+
+ + +
Link copied to clipboard
+
+
+
+
+
+
+ +
+
+ + diff --git a/docs/ai/org.noelware.ai/int.html b/docs/ai/org.noelware.ai/int.html new file mode 100644 index 0000000..8e7f309 --- /dev/null +++ b/docs/ai/org.noelware.ai/int.html @@ -0,0 +1,72 @@ + + + + + int + + + + + + + + + + + + + + + + + +
+
+
+
+
+
+ +
+

int

+
+
fun AiCommand.int(name: String): ReadOnlyProperty<Any?, Int>

Returns the option as a string, if it wasn't found, then it throws an IllegalStateException

Throws

If the option wasn't found.

+

Sources

+
+
+
+
+ + +
Link copied to clipboard
+
+
+
+
+
+
+ +
+
+ + diff --git a/docs/ai/org.noelware.ai/string-or-null.html b/docs/ai/org.noelware.ai/string-or-null.html index 9191ec3..cea1fc4 100644 --- a/docs/ai/org.noelware.ai/string-or-null.html +++ b/docs/ai/org.noelware.ai/string-or-null.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -47,13 +47,13 @@

stringOrNull

-
fun AiCommand.stringOrNull(name: String): String?
+
fun AiCommand.stringOrNull(name: String): ReadOnlyProperty<Any?, String?>

Returns the option as a string, if it has the option or null.

Sources

-
+
- +
Link copied to clipboard
diff --git a/docs/ai/org.noelware.ai/string.html b/docs/ai/org.noelware.ai/string.html index 78838a2..0112c13 100644 --- a/docs/ai/org.noelware.ai/string.html +++ b/docs/ai/org.noelware.ai/string.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
@@ -47,13 +47,13 @@

string

-
fun AiCommand.string(name: String): String
+
fun AiCommand.string(name: String): ReadOnlyProperty<Any?, String>

Returns the option as a string, if it wasn't found, then it throws an IllegalStateException

Throws

If the option wasn't found.

Sources

-
+
- +
Link copied to clipboard
diff --git a/docs/ai/package-list b/docs/ai/package-list index 6a4d9c8..bca3b8e 100644 --- a/docs/ai/package-list +++ b/docs/ai/package-list @@ -1,6 +1,10 @@ $dokka.format:html-v1 $dokka.linkExtension:html $dokka.location:org.noelware.ai////PointingToDeclaration/ai/org.noelware.ai/index.html +$dokka.location:org.noelware.ai//bool/org.noelware.ai.AiCommand#kotlin.String/PointingToDeclaration/ai/org.noelware.ai/bool.html +$dokka.location:org.noelware.ai//file/org.noelware.ai.AiCommand#kotlin.String#kotlin.Boolean#kotlin.Boolean#kotlin.Boolean/PointingToDeclaration/ai/org.noelware.ai/file.html +$dokka.location:org.noelware.ai//int/org.noelware.ai.AiCommand#kotlin.String/PointingToDeclaration/ai/org.noelware.ai/int.html +$dokka.location:org.noelware.ai//intOrNull/org.noelware.ai.AiCommand#kotlin.String/PointingToDeclaration/ai/org.noelware.ai/int-or-null.html $dokka.location:org.noelware.ai//string/org.noelware.ai.AiCommand#kotlin.String/PointingToDeclaration/ai/org.noelware.ai/string.html $dokka.location:org.noelware.ai//stringOrNull/org.noelware.ai.AiCommand#kotlin.String/PointingToDeclaration/ai/org.noelware.ai/string-or-null.html $dokka.location:org.noelware.ai/AiCommand.Companion///PointingToDeclaration/ai/org.noelware.ai/-ai-command/-companion/index.html @@ -17,9 +21,11 @@ $dokka.location:org.noelware.ai/AiCommand/run/#kotlin.collections.List[kotlin.St $dokka.location:org.noelware.ai/AiCommand/setContext/#TypeParam(bounds=[org.noelware.ai.AiContext])/PointingToDeclaration/ai/org.noelware.ai/-ai-command/set-context.html $dokka.location:org.noelware.ai/AiContext///PointingToDeclaration/ai/org.noelware.ai/-ai-context/index.html $dokka.location:org.noelware.ai/AiContext/command/#/PointingToDeclaration/ai/org.noelware.ai/-ai-context/command.html +$dokka.location:org.noelware.ai/AiPhase.FAIL///PointingToDeclaration/{"org.jetbrains.dokka.links.EnumEntryDRIExtra":{"key":"org.jetbrains.dokka.links.EnumEntryDRIExtra"}}ai/org.noelware.ai/-ai-phase/-f-a-i-l/index.html $dokka.location:org.noelware.ai/AiPhase.FINISHED///PointingToDeclaration/{"org.jetbrains.dokka.links.EnumEntryDRIExtra":{"key":"org.jetbrains.dokka.links.EnumEntryDRIExtra"}}ai/org.noelware.ai/-ai-phase/-f-i-n-i-s-h-e-d/index.html $dokka.location:org.noelware.ai/AiPhase.PRINT_USAGE///PointingToDeclaration/{"org.jetbrains.dokka.links.EnumEntryDRIExtra":{"key":"org.jetbrains.dokka.links.EnumEntryDRIExtra"}}ai/org.noelware.ai/-ai-phase/-p-r-i-n-t_-u-s-a-g-e/index.html $dokka.location:org.noelware.ai/AiPhase///PointingToDeclaration/ai/org.noelware.ai/-ai-phase/index.html +$dokka.location:org.noelware.ai/AiPhase/exitCode/#/PointingToDeclaration/ai/org.noelware.ai/-ai-phase/exit-code.html $dokka.location:org.noelware.ai/DefaultAiContext///PointingToDeclaration/ai/org.noelware.ai/-default-ai-context/index.html $dokka.location:org.noelware.ai/DefaultAiContext/DefaultAiContext/#org.noelware.ai.AiCommand/PointingToDeclaration/ai/org.noelware.ai/-default-ai-context/-default-ai-context.html $dokka.location:org.noelware.ai/DefaultAiContext/command/#/PointingToDeclaration/ai/org.noelware.ai/-default-ai-context/command.html diff --git a/docs/index.html b/docs/index.html index ced44ff..8e5f2f8 100644 --- a/docs/index.html +++ b/docs/index.html @@ -31,7 +31,7 @@
-1.0-beta
+1.1-beta
diff --git a/docs/navigation.html b/docs/navigation.html index 1495f49..db72643 100644 --- a/docs/navigation.html +++ b/docs/navigation.html @@ -10,20 +10,35 @@
-
+
+ +
+ - -
+
+ +
+ -
+
+ +
+
+ +
+ + - diff --git a/docs/scripts/pages.json b/docs/scripts/pages.json index 03129e9..9469b81 100644 --- a/docs/scripts/pages.json +++ b/docs/scripts/pages.json @@ -1 +1 @@ -[{"name":"FINISHED()","description":"org.noelware.ai.AiPhase.FINISHED","location":"ai/org.noelware.ai/-ai-phase/-f-i-n-i-s-h-e-d/index.html","searchKeys":["FINISHED","FINISHED()","org.noelware.ai.AiPhase.FINISHED"]},{"name":"PRINT_USAGE()","description":"org.noelware.ai.AiPhase.PRINT_USAGE","location":"ai/org.noelware.ai/-ai-phase/-p-r-i-n-t_-u-s-a-g-e/index.html","searchKeys":["PRINT_USAGE","PRINT_USAGE()","org.noelware.ai.AiPhase.PRINT_USAGE"]},{"name":"abstract class AiCommand(name: String = inferName(), help: String = \"\", usage: String? = null, printUsageIfError: Boolean = false)","description":"org.noelware.ai.AiCommand","location":"ai/org.noelware.ai/-ai-command/index.html","searchKeys":["AiCommand","abstract class AiCommand(name: String = inferName(), help: String = \"\", usage: String? = null, printUsageIfError: Boolean = false)","org.noelware.ai.AiCommand"]},{"name":"abstract fun run(args: List): AiPhase","description":"org.noelware.ai.AiCommand.run","location":"ai/org.noelware.ai/-ai-command/run.html","searchKeys":["run","abstract fun run(args: List): AiPhase","org.noelware.ai.AiCommand.run"]},{"name":"abstract val command: AiCommand","description":"org.noelware.ai.AiContext.command","location":"ai/org.noelware.ai/-ai-context/command.html","searchKeys":["command","abstract val command: AiCommand","org.noelware.ai.AiContext.command"]},{"name":"class DefaultAiContext(val command: AiCommand) : AiContext","description":"org.noelware.ai.DefaultAiContext","location":"ai/org.noelware.ai/-default-ai-context/index.html","searchKeys":["DefaultAiContext","class DefaultAiContext(val command: AiCommand) : AiContext","org.noelware.ai.DefaultAiContext"]},{"name":"enum AiPhase : Enum ","description":"org.noelware.ai.AiPhase","location":"ai/org.noelware.ai/-ai-phase/index.html","searchKeys":["AiPhase","enum AiPhase : Enum ","org.noelware.ai.AiPhase"]},{"name":"fun setContext(ctx: T)","description":"org.noelware.ai.AiCommand.setContext","location":"ai/org.noelware.ai/-ai-command/set-context.html","searchKeys":["setContext","fun setContext(ctx: T)","org.noelware.ai.AiCommand.setContext"]},{"name":"fun AiCommand(name: String = inferName(), help: String = \"\", usage: String? = null, printUsageIfError: Boolean = false)","description":"org.noelware.ai.AiCommand.AiCommand","location":"ai/org.noelware.ai/-ai-command/-ai-command.html","searchKeys":["AiCommand","fun AiCommand(name: String = inferName(), help: String = \"\", usage: String? = null, printUsageIfError: Boolean = false)","org.noelware.ai.AiCommand.AiCommand"]},{"name":"fun AiCommand.string(name: String): String","description":"org.noelware.ai.string","location":"ai/org.noelware.ai/string.html","searchKeys":["string","fun AiCommand.string(name: String): String","org.noelware.ai.string"]},{"name":"fun AiCommand.stringOrNull(name: String): String?","description":"org.noelware.ai.stringOrNull","location":"ai/org.noelware.ai/string-or-null.html","searchKeys":["stringOrNull","fun AiCommand.stringOrNull(name: String): String?","org.noelware.ai.stringOrNull"]},{"name":"fun DefaultAiContext(command: AiCommand)","description":"org.noelware.ai.DefaultAiContext.DefaultAiContext","location":"ai/org.noelware.ai/-default-ai-context/-default-ai-context.html","searchKeys":["DefaultAiContext","fun DefaultAiContext(command: AiCommand)","org.noelware.ai.DefaultAiContext.DefaultAiContext"]},{"name":"fun addSubcommand(command: AiCommand)","description":"org.noelware.ai.AiCommand.addSubcommand","location":"ai/org.noelware.ai/-ai-command/add-subcommand.html","searchKeys":["addSubcommand","fun addSubcommand(command: AiCommand)","org.noelware.ai.AiCommand.addSubcommand"]},{"name":"fun addSubcommands(vararg commands: AiCommand)","description":"org.noelware.ai.AiCommand.addSubcommands","location":"ai/org.noelware.ai/-ai-command/add-subcommands.html","searchKeys":["addSubcommands","fun addSubcommands(vararg commands: AiCommand)","org.noelware.ai.AiCommand.addSubcommands"]},{"name":"fun arg(name: String, help: String = \"\", longName: String? = null, required: Boolean = false)","description":"org.noelware.ai.AiCommand.arg","location":"ai/org.noelware.ai/-ai-command/arg.html","searchKeys":["arg","fun arg(name: String, help: String = \"\", longName: String? = null, required: Boolean = false)","org.noelware.ai.AiCommand.arg"]},{"name":"fun flag(name: String, help: String = \"\", longName: String? = null, required: Boolean = false)","description":"org.noelware.ai.AiCommand.flag","location":"ai/org.noelware.ai/-ai-command/flag.html","searchKeys":["flag","fun flag(name: String, help: String = \"\", longName: String? = null, required: Boolean = false)","org.noelware.ai.AiCommand.flag"]},{"name":"fun main(args: Array)","description":"org.noelware.ai.AiCommand.main","location":"ai/org.noelware.ai/-ai-command/main.html","searchKeys":["main","fun main(args: Array)","org.noelware.ai.AiCommand.main"]},{"name":"fun main(args: List)","description":"org.noelware.ai.AiCommand.main","location":"ai/org.noelware.ai/-ai-command/main.html","searchKeys":["main","fun main(args: List)","org.noelware.ai.AiCommand.main"]},{"name":"interface AiContext","description":"org.noelware.ai.AiContext","location":"ai/org.noelware.ai/-ai-context/index.html","searchKeys":["AiContext","interface AiContext","org.noelware.ai.AiContext"]},{"name":"object Companion","description":"org.noelware.ai.AiCommand.Companion","location":"ai/org.noelware.ai/-ai-command/-companion/index.html","searchKeys":["Companion","object Companion","org.noelware.ai.AiCommand.Companion"]},{"name":"open override val command: AiCommand","description":"org.noelware.ai.DefaultAiContext.command","location":"ai/org.noelware.ai/-default-ai-context/command.html","searchKeys":["command","open override val command: AiCommand","org.noelware.ai.DefaultAiContext.command"]},{"name":"val context: AiContext","description":"org.noelware.ai.AiCommand.context","location":"ai/org.noelware.ai/-ai-command/context.html","searchKeys":["context","val context: AiContext","org.noelware.ai.AiCommand.context"]}] +[{"name":"FAIL(1)","description":"org.noelware.ai.AiPhase.FAIL","location":"ai/org.noelware.ai/-ai-phase/-f-a-i-l/index.html","searchKeys":["FAIL","FAIL(1)","org.noelware.ai.AiPhase.FAIL"]},{"name":"FINISHED(0)","description":"org.noelware.ai.AiPhase.FINISHED","location":"ai/org.noelware.ai/-ai-phase/-f-i-n-i-s-h-e-d/index.html","searchKeys":["FINISHED","FINISHED(0)","org.noelware.ai.AiPhase.FINISHED"]},{"name":"PRINT_USAGE(130)","description":"org.noelware.ai.AiPhase.PRINT_USAGE","location":"ai/org.noelware.ai/-ai-phase/-p-r-i-n-t_-u-s-a-g-e/index.html","searchKeys":["PRINT_USAGE","PRINT_USAGE(130)","org.noelware.ai.AiPhase.PRINT_USAGE"]},{"name":"abstract class AiCommand(name: String = inferName(), help: String = \"\", usage: String? = null, printUsageIfError: Boolean = false)","description":"org.noelware.ai.AiCommand","location":"ai/org.noelware.ai/-ai-command/index.html","searchKeys":["AiCommand","abstract class AiCommand(name: String = inferName(), help: String = \"\", usage: String? = null, printUsageIfError: Boolean = false)","org.noelware.ai.AiCommand"]},{"name":"abstract fun run(args: List): AiPhase","description":"org.noelware.ai.AiCommand.run","location":"ai/org.noelware.ai/-ai-command/run.html","searchKeys":["run","abstract fun run(args: List): AiPhase","org.noelware.ai.AiCommand.run"]},{"name":"abstract val command: AiCommand","description":"org.noelware.ai.AiContext.command","location":"ai/org.noelware.ai/-ai-context/command.html","searchKeys":["command","abstract val command: AiCommand","org.noelware.ai.AiContext.command"]},{"name":"class DefaultAiContext(val command: AiCommand) : AiContext","description":"org.noelware.ai.DefaultAiContext","location":"ai/org.noelware.ai/-default-ai-context/index.html","searchKeys":["DefaultAiContext","class DefaultAiContext(val command: AiCommand) : AiContext","org.noelware.ai.DefaultAiContext"]},{"name":"enum AiPhase : Enum ","description":"org.noelware.ai.AiPhase","location":"ai/org.noelware.ai/-ai-phase/index.html","searchKeys":["AiPhase","enum AiPhase : Enum ","org.noelware.ai.AiPhase"]},{"name":"fun setContext(ctx: T)","description":"org.noelware.ai.AiCommand.setContext","location":"ai/org.noelware.ai/-ai-command/set-context.html","searchKeys":["setContext","fun setContext(ctx: T)","org.noelware.ai.AiCommand.setContext"]},{"name":"fun AiCommand(name: String = inferName(), help: String = \"\", usage: String? = null, printUsageIfError: Boolean = false)","description":"org.noelware.ai.AiCommand.AiCommand","location":"ai/org.noelware.ai/-ai-command/-ai-command.html","searchKeys":["AiCommand","fun AiCommand(name: String = inferName(), help: String = \"\", usage: String? = null, printUsageIfError: Boolean = false)","org.noelware.ai.AiCommand.AiCommand"]},{"name":"fun AiCommand.bool(name: String): ReadOnlyProperty","description":"org.noelware.ai.bool","location":"ai/org.noelware.ai/bool.html","searchKeys":["bool","fun AiCommand.bool(name: String): ReadOnlyProperty","org.noelware.ai.bool"]},{"name":"fun AiCommand.file(name: String, mustBeDir: Boolean = false, mustBeFile: Boolean = false, mustExist: Boolean = true): ReadOnlyProperty","description":"org.noelware.ai.file","location":"ai/org.noelware.ai/file.html","searchKeys":["file","fun AiCommand.file(name: String, mustBeDir: Boolean = false, mustBeFile: Boolean = false, mustExist: Boolean = true): ReadOnlyProperty","org.noelware.ai.file"]},{"name":"fun AiCommand.int(name: String): ReadOnlyProperty","description":"org.noelware.ai.int","location":"ai/org.noelware.ai/int.html","searchKeys":["int","fun AiCommand.int(name: String): ReadOnlyProperty","org.noelware.ai.int"]},{"name":"fun AiCommand.intOrNull(name: String): ReadOnlyProperty","description":"org.noelware.ai.intOrNull","location":"ai/org.noelware.ai/int-or-null.html","searchKeys":["intOrNull","fun AiCommand.intOrNull(name: String): ReadOnlyProperty","org.noelware.ai.intOrNull"]},{"name":"fun AiCommand.string(name: String): ReadOnlyProperty","description":"org.noelware.ai.string","location":"ai/org.noelware.ai/string.html","searchKeys":["string","fun AiCommand.string(name: String): ReadOnlyProperty","org.noelware.ai.string"]},{"name":"fun AiCommand.stringOrNull(name: String): ReadOnlyProperty","description":"org.noelware.ai.stringOrNull","location":"ai/org.noelware.ai/string-or-null.html","searchKeys":["stringOrNull","fun AiCommand.stringOrNull(name: String): ReadOnlyProperty","org.noelware.ai.stringOrNull"]},{"name":"fun DefaultAiContext(command: AiCommand)","description":"org.noelware.ai.DefaultAiContext.DefaultAiContext","location":"ai/org.noelware.ai/-default-ai-context/-default-ai-context.html","searchKeys":["DefaultAiContext","fun DefaultAiContext(command: AiCommand)","org.noelware.ai.DefaultAiContext.DefaultAiContext"]},{"name":"fun addSubcommand(command: AiCommand)","description":"org.noelware.ai.AiCommand.addSubcommand","location":"ai/org.noelware.ai/-ai-command/add-subcommand.html","searchKeys":["addSubcommand","fun addSubcommand(command: AiCommand)","org.noelware.ai.AiCommand.addSubcommand"]},{"name":"fun addSubcommands(vararg commands: AiCommand)","description":"org.noelware.ai.AiCommand.addSubcommands","location":"ai/org.noelware.ai/-ai-command/add-subcommands.html","searchKeys":["addSubcommands","fun addSubcommands(vararg commands: AiCommand)","org.noelware.ai.AiCommand.addSubcommands"]},{"name":"fun arg(name: String, help: String = \"\", longName: String? = null, required: Boolean = false)","description":"org.noelware.ai.AiCommand.arg","location":"ai/org.noelware.ai/-ai-command/arg.html","searchKeys":["arg","fun arg(name: String, help: String = \"\", longName: String? = null, required: Boolean = false)","org.noelware.ai.AiCommand.arg"]},{"name":"fun flag(name: String, help: String = \"\", longName: String? = null, required: Boolean = false)","description":"org.noelware.ai.AiCommand.flag","location":"ai/org.noelware.ai/-ai-command/flag.html","searchKeys":["flag","fun flag(name: String, help: String = \"\", longName: String? = null, required: Boolean = false)","org.noelware.ai.AiCommand.flag"]},{"name":"fun main(args: Array)","description":"org.noelware.ai.AiCommand.main","location":"ai/org.noelware.ai/-ai-command/main.html","searchKeys":["main","fun main(args: Array)","org.noelware.ai.AiCommand.main"]},{"name":"fun main(args: List)","description":"org.noelware.ai.AiCommand.main","location":"ai/org.noelware.ai/-ai-command/main.html","searchKeys":["main","fun main(args: List)","org.noelware.ai.AiCommand.main"]},{"name":"interface AiContext","description":"org.noelware.ai.AiContext","location":"ai/org.noelware.ai/-ai-context/index.html","searchKeys":["AiContext","interface AiContext","org.noelware.ai.AiContext"]},{"name":"object Companion","description":"org.noelware.ai.AiCommand.Companion","location":"ai/org.noelware.ai/-ai-command/-companion/index.html","searchKeys":["Companion","object Companion","org.noelware.ai.AiCommand.Companion"]},{"name":"open override val command: AiCommand","description":"org.noelware.ai.DefaultAiContext.command","location":"ai/org.noelware.ai/-default-ai-context/command.html","searchKeys":["command","open override val command: AiCommand","org.noelware.ai.DefaultAiContext.command"]},{"name":"val context: AiContext","description":"org.noelware.ai.AiCommand.context","location":"ai/org.noelware.ai/-ai-command/context.html","searchKeys":["context","val context: AiContext","org.noelware.ai.AiCommand.context"]},{"name":"val exitCode: Int","description":"org.noelware.ai.AiPhase.exitCode","location":"ai/org.noelware.ai/-ai-phase/exit-code.html","searchKeys":["exitCode","val exitCode: Int","org.noelware.ai.AiPhase.exitCode"]}] diff --git a/example/src/main/kotlin/org/noelware/ai/example/ExampleCli.kt b/example/src/main/kotlin/org/noelware/ai/example/ExampleCli.kt index 70f682f..60f863b 100644 --- a/example/src/main/kotlin/org/noelware/ai/example/ExampleCli.kt +++ b/example/src/main/kotlin/org/noelware/ai/example/ExampleCli.kt @@ -12,7 +12,7 @@ object ExampleCli: AiCommand("example", printUsageIfError = true) { } override fun run(args: List): AiPhase { - val arg = stringOrNull("a") ?: ":D" + val arg by stringOrNull("a") println("hi :DDDDD (arg=$arg)") return AiPhase.FINISHED diff --git a/src/main/kotlin/org/noelware/ai/AiCommand.kt b/src/main/kotlin/org/noelware/ai/AiCommand.kt index 442ed8a..3923a9e 100644 --- a/src/main/kotlin/org/noelware/ai/AiCommand.kt +++ b/src/main/kotlin/org/noelware/ai/AiCommand.kt @@ -21,6 +21,7 @@ * SOFTWARE. */ +@file:Suppress("UNUSED") package org.noelware.ai import org.apache.commons.cli.CommandLine @@ -28,8 +29,10 @@ import org.apache.commons.cli.DefaultParser import org.apache.commons.cli.HelpFormatter import org.apache.commons.cli.Option import org.apache.commons.cli.Options +import java.io.File import java.io.PrintWriter import java.io.StringWriter +import kotlin.properties.ReadOnlyProperty import kotlin.system.exitProcess /** @@ -50,7 +53,7 @@ abstract class AiCommand( private val subcommands = mutableListOf() // The options that this [AiCommand] can be executed with. - internal val options = Options() + private val options = Options() // The command line interface that was parsed. internal lateinit var cli: CommandLine @@ -103,6 +106,13 @@ abstract class AiCommand( } } + /** + * Adds a boolean flag, that must require a value (if needed). + * @param name The name of the argument flag. + * @param help The help message of this argument flag. + * @param longName The long name to use. + * @param required If the argument flag is required or not. + */ fun flag(name: String, help: String = "", longName: String? = null, required: Boolean = false) { val builder = Option.builder(name) .hasArg(false) @@ -116,6 +126,13 @@ abstract class AiCommand( options.addOption(builder.build()) } + /** + * Adds an argument flag, that must require a value (if needed). + * @param name The name of the argument flag. + * @param help The help message of this argument flag. + * @param longName The long name to use. + * @param required If the argument flag is required or not. + */ fun arg(name: String, help: String = "", longName: String? = null, required: Boolean = false) { val builder = Option.builder(name) .hasArg() @@ -137,13 +154,38 @@ abstract class AiCommand( * * - [AiPhase.PRINT_USAGE]: If we should print the usage to the standard output. * - [AiPhase.FINISHED]: The leading command has finished, and we do nothing. + * - [AiPhase.FAIL]: The command has failed to do a specific action. * * @param args The left-over, positional arguments available. */ abstract fun run(args: List): AiPhase + /** + * Executes the command with the arguments used. + * + * ## Example + * ```kotlin + * object MyCommand: AiCommand("owo", "uwu!") { + * override fun run(args: List): AiPhase = AiPhase.PRINT_USAGE + * } + * + * fun main(args: Array) = MyCommand.main(args) + * ``` + */ fun main(args: Array) = main(args.toList()) + /** + * Executes the command with the arguments used. + * + * ## Example + * ```kotlin + * object MyCommand: AiCommand("owo", "uwu!") { + * override fun run(args: List): AiPhase = AiPhase.PRINT_USAGE + * } + * + * fun main(args: Array) = MyCommand.main(args) + * ``` + */ fun main(args: List) { val parser = DefaultParser() var errored = false @@ -188,7 +230,7 @@ abstract class AiCommand( printUsageAndExit() } - val status = if (errored) 1 else 0 + val status = if (errored) 1 else phase.exitCode exitProcess(status) } @@ -232,11 +274,98 @@ abstract class AiCommand( // +=+ BUILT INS THINGIES :D +=+ -fun AiCommand.stringOrNull(name: String): String? { +/** + * Returns the option as a string, if it has the option or `null`. + */ +fun AiCommand.stringOrNull(name: String): ReadOnlyProperty = ReadOnlyProperty { _, _ -> if (cli.hasOption(name)) - return cli.getOptionValue(name) + cli.getOptionValue(name) + else + null +} - return null +/** + * Returns the option as a string, if it wasn't found, then it throws an [IllegalStateException] + * @throws IllegalStateException If the option wasn't found. + */ +fun AiCommand.string(name: String): ReadOnlyProperty = ReadOnlyProperty { _, _ -> + if (cli.hasOption(name)) + cli.getOptionValue(name) + else + throw IllegalStateException("Missing required flag: $name") } -fun AiCommand.string(name: String): String = stringOrNull(name) ?: error("Missing required flag: $name") +/** + * Returns the option as a boolean, if it has the option, or `false`. + */ +fun AiCommand.bool(name: String): ReadOnlyProperty = ReadOnlyProperty { _, _ -> + if (cli.hasOption(name)) { + val value = cli.getOptionValue(name) + if (value.matches("true|yes|0|t|si$".toRegex())) + return@ReadOnlyProperty true + + if (value.matches("false|no|1|f|nu*$".toRegex())) + return@ReadOnlyProperty false + } + + false +} + +/** + * Returns the option as an integer, if it has the option, or `null`. + */ +fun AiCommand.intOrNull(name: String): ReadOnlyProperty = ReadOnlyProperty { _, _ -> + if (cli.hasOption(name)) + Integer.parseInt(cli.getOptionValue(name)) + else + null +} + +/** + * Returns the option as a string, if it wasn't found, then it throws an [IllegalStateException] + * @throws IllegalStateException If the option wasn't found. + */ +fun AiCommand.int(name: String): ReadOnlyProperty = ReadOnlyProperty { _, _ -> + if (cli.hasOption(name)) + Integer.parseInt(cli.getOptionValue(name)) + else + throw IllegalStateException("Missing required flag: $name") +} + +/** + * Returns the option as a file, if the name is an absolute path. + * @param name The name of the argument + * @param mustBeDir If the file must be a directory, cannot be linked with [mustBeFile]. + * @param mustBeFile If the fist MUST be a file object, cannot be linked with [mustBeDir]. + * @param mustExist If the file MUST exist, if not, it'll bark. + */ +fun AiCommand.file( + name: String, + mustBeDir: Boolean = false, + mustBeFile: Boolean = false, + mustExist: Boolean = true +): ReadOnlyProperty = ReadOnlyProperty { _, _ -> + if (!cli.hasOption(name)) + throw IllegalArgumentException("You must have the flag $name.") + + val path = cli.getOptionValue(name) + val actualFilePath = when { + path.startsWith("./") -> System.getProperty("user.dir") + path.replaceFirstChar { "" } + path.startsWith("~/") -> System.getProperty("user.home") + path + File(path).isAbsolute -> path + else -> error("Path $path was not an absolute path.") + } + + val file = File(actualFilePath) + + if (mustExist && !file.exists()) + throw IllegalStateException("File or directory $actualFilePath doesn't exist.") + + if (mustBeDir && !file.isDirectory) + throw IllegalStateException("Directory $actualFilePath was not a directory.") + + if (mustBeFile && !file.isFile) + throw IllegalStateException("File $actualFilePath was not a file.") + + file +} diff --git a/src/main/kotlin/org/noelware/ai/AiPhase.kt b/src/main/kotlin/org/noelware/ai/AiPhase.kt index 4b65a0b..c390e94 100644 --- a/src/main/kotlin/org/noelware/ai/AiPhase.kt +++ b/src/main/kotlin/org/noelware/ai/AiPhase.kt @@ -26,14 +26,19 @@ package org.noelware.ai /** * Represents the phase of a [command][AiCommand]'s execution. */ -enum class AiPhase { +enum class AiPhase(val exitCode: Int) { /** * The command has finished. */ - FINISHED, + FINISHED(0), /** * The command wants to print its usage. */ - PRINT_USAGE; + PRINT_USAGE(130), + + /** + * That command has exited with a failed exit code. + */ + FAIL(1); }