Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Mar 12, 2020
1 parent 4543cb7 commit d693d08
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
16 changes: 12 additions & 4 deletions docs/arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ functions like [`pair`][pair] and [`triple`][triple]. Unlike options, arguments
variable (or unlimited) number of values. This is common with file path arguments, since
they are frequently expanded with a glob pattern on the command line.

You can declare any number of arguments with fixed numbers of values,
but only one variadic argument.
Variadic arguments are declared with [`multiple`][multiple]. You can declare any number of arguments
with fixed numbers of values, but only one variadic argument in a command.

```kotlin tab="Example"
class Copy : CliktCommand() {
val source by argument().path(mustExist = true).multiple()
val dest by argument().path(canBeFile = false)
val source: List<Path> by argument().path(mustExist = true).multiple()
val dest: Path by argument().path(canBeFile = false)
override fun run() {
echo("Copying files $source to $dest")
}
Expand All @@ -74,6 +74,12 @@ $ ./copy file.* out/
Copying files [file.txt, file.md] to out/
```

You can also use [`unique`][unique] to discard duplicates:

```kotlin
val source: Set<Path> by argument().path(mustExist = true).multiple().unique()
```

## Option-Like Arguments (Using `--`)

Clikt normally parses any value that starts with punctuation as an
Expand Down Expand Up @@ -114,3 +120,5 @@ bar.txt
[argument]: api/clikt/com.github.ajalt.clikt.parameters.arguments/argument.md
[pair]: api/clikt/com.github.ajalt.clikt.parameters.arguments/pair.md
[triple]: api/clikt/com.github.ajalt.clikt.parameters.arguments/triple.md
[unique]: api/clikt/com.github.ajalt.clikt.parameters.arguments/unique.md
[multiple]: api/clikt/com.github.ajalt.clikt.parameters.arguments/multiple.md
28 changes: 24 additions & 4 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ is never given, the list will be empty (or you can specify a default to use).

```kotlin tab="Example"
class Commit : CliktCommand() {
val message by option("-m").multiple()
val message: List<String> by option("-m").multiple()
override fun run() {
echo(message.joinToString("\n"))
}
Expand All @@ -224,22 +224,41 @@ bar
```

You can combine [`multiple`][multiple] with item type conversions and multiple values.
For example:

```kotlin
val opt: List<Pair<Int, Int>> by option().int().pair().multiple()
```

### Default values for option().multiple()

You can also supply a default value to [`multiple`][multiple] or require at least one value be present
on the command line. These are specified as arguments rather than with separate extension functions
since they don't change the type of the delegate.

```kotlin tab="Required"
val opt by option().multiple(required=true)
val opt: List<String> by option().multiple(required=true)
```

```kotlin tab="Default"
val opt by option().multiple(default=listOf("default message"))
val opt: List<String> by option().multiple(default=listOf("default message"))
```

### Deduplicating option().multiple() into a unique set

You can discard duplicate values from a `multiple` option with [`unique`][unique].

```kotlin tab="Example"
class Build : CliktCommand() {
val platforms: Set<String> by option("-p").multiple().unique()
override fun run() {
echo("Building for platforms: $platforms")
}
}
```

```text tab="Usage"
$ ./build -p android -p ios -p android
Building for platforms: [android, ios]
```

## Boolean Flag Options
Expand Down Expand Up @@ -918,6 +937,7 @@ val opt: Pair<Int, Int> by option("-o", "--opt")
[transformValues]: api/clikt/com.github.ajalt.clikt.parameters.options/transform-values.md
[default]: api/clikt/com.github.ajalt.clikt.parameters.options/default.md
[multiple]: api/clikt/com.github.ajalt.clikt.parameters.options/multiple.md
[unique]: api/clikt/com.github.ajalt.clikt.parameters.options/unique.md
[defaultLazy]: api/clikt/com.github.ajalt.clikt.parameters.options/default-lazy.md
[split]: api/clikt/com.github.ajalt.clikt.parameters.options/split.md
[flag]: api/clikt/com.github.ajalt.clikt.parameters.options/flag.md
Expand Down

0 comments on commit d693d08

Please sign in to comment.