Skip to content

Commit

Permalink
feat: Merge selection and option commands to arg groups
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Aug 12, 2024
1 parent d4cfaed commit a3fb678
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 212 deletions.
10 changes: 5 additions & 5 deletions docs/1_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,25 @@ java -jar revanced-cli.jar patch -b revanced-patches.rvp --exclusive -i "Patch n
> adb install input.apk
> ```
Patches can have options you can set using the option `--set-options`.
Patches can have options you can set using the option `-O` alongside the option to include the patch by name or index.
To know the options of a patch, use the option `--with-options` when listing patches:
```bash
java -jar revanced-cli.jar list-patches --with-options revanced-patches.rvp
```
Each patch can have multiple options. You can set them using the option `--set-options`.
Each patch can have multiple options. You can set them using the option `-O`.
For example, to set the options for the patch with the name `Patch name`
with the key `key1` and `key2` to `value1` and `value2` respectively, use the following command:

```bash
java -jar revanced-cli.jar patch -b revanced-patches.rvp --set-options "Patch name" -Okey1=value1 -Okey2=value2 input.apk
java -jar revanced-cli.jar patch -b revanced-patches.rvp -i "Patch name" -Okey1=value1 -Okey2=value2 input.apk
```

If you want to set a value to `null`, you can omit the value:

```bash
java -jar revanced-cli.jar patch -b revanced-patches.rvp --set-options "Patch name" -Okey1 input.apk
java -jar revanced-cli.jar patch -b revanced-patches.rvp -i "Patch name" -Okey1 input.apk
```

> [!WARNING]
Expand Down Expand Up @@ -131,7 +131,7 @@ java -jar revanced-cli.jar patch -b revanced-patches.rvp --set-options "Patch na
> Example command with an escaped integer as a string:
>
> ```bash
> java -jar revanced-cli.jar -b revanced-patches.rvp --set-options "Patch name" -OstringKey=\'1\' input.apk
> java -jar revanced-cli.jar -b revanced-patches.rvp -i "Patch name" -OstringKey=\'1\' input.apk
> ```
## 📦 Install an app manually
Expand Down
105 changes: 105 additions & 0 deletions src/main/kotlin/app/revanced/cli/command/CommandUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package app.revanced.cli.command

import picocli.CommandLine

class OptionKeyConverter : CommandLine.ITypeConverter<String> {
override fun convert(value: String): String = value
}

class OptionValueConverter : CommandLine.ITypeConverter<Any?> {
override fun convert(value: String?): Any? {
value ?: return null

return when {
value.startsWith("[") && value.endsWith("]") -> {
val innerValue = value.substring(1, value.length - 1)

buildList {
var nestLevel = 0
var insideQuote = false
var escaped = false

val item = buildString {
for (char in innerValue) {
when (char) {
'\\' -> {
if (escaped || nestLevel != 0) {
append(char)
}

escaped = !escaped
}

'"', '\'' -> {
if (!escaped) {
insideQuote = !insideQuote
} else {
escaped = false
}

append(char)
}

'[' -> {
if (!insideQuote) {
nestLevel++
}

append(char)
}

']' -> {
if (!insideQuote) {
nestLevel--

if (nestLevel == -1) {
return value
}
}

append(char)
}

',' -> if (nestLevel == 0) {
if (insideQuote) {
append(char)
} else {
add(convert(toString()))
setLength(0)
}
} else {
append(char)
}

else -> append(char)
}
}
}

if (item.isNotEmpty()) {
add(convert(item))
}
}
}

value.startsWith("\"") && value.endsWith("\"") -> value.substring(1, value.length - 1)
value.startsWith("'") && value.endsWith("'") -> value.substring(1, value.length - 1)
value.endsWith("f") -> value.dropLast(1).toFloat()
value.endsWith("L") -> value.dropLast(1).toLong()
value.equals("true", ignoreCase = true) -> true
value.equals("false", ignoreCase = true) -> false
value.toIntOrNull() != null -> value.toInt()
value.toLongOrNull() != null -> value.toLong()
value.toDoubleOrNull() != null -> value.toDouble()
value.toFloatOrNull() != null -> value.toFloat()
value == "null" -> null
value == "int[]" -> emptyList<Int>()
value == "long[]" -> emptyList<Long>()
value == "double[]" -> emptyList<Double>()
value == "float[]" -> emptyList<Float>()
value == "boolean[]" -> emptyList<Boolean>()
value == "string[]" -> emptyList<String>()
else -> value
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import java.util.logging.Logger
],
)
internal class ListCompatibleVersions : Runnable {
private val logger = Logger.getLogger(ListCompatibleVersions::class.java.name)
private val logger = Logger.getLogger(this::class.java.name)

@CommandLine.Parameters(
description = ["Paths to patch bundles."],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import app.revanced.patcher.patch.Option as PatchOption
description = ["List patches from supplied patch bundles."],
)
internal object ListPatchesCommand : Runnable {
private val logger = Logger.getLogger(ListPatchesCommand::class.java.name)
private val logger = Logger.getLogger(this::class.java.name)

@Parameters(
description = ["Paths to patch bundles."],
Expand Down
Loading

0 comments on commit a3fb678

Please sign in to comment.