Skip to content

Commit df34204

Browse files
C06Aholgerbrandl
authored andcommitted
Improve docopt example (#118)
* Use kscript command options as an example of usage DocOpt * Improve text of the Usage example
1 parent d3bcb13 commit df34204

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

examples/classpath_example.kts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env kscript
2+
23
//DEPS com.offbytwo:docopt:0.6.0.20150202,log4j:log4j:1.2.14
34

45
//#!/usr/bin/env kotlinc -script -classpath /Users/brandl/.m2/repository/org/docopt/docopt/0.6.0-SNAPSHOT/docopt-0.6.0-SNAPSHOT.jar
@@ -11,34 +12,35 @@ import java.util.*
1112
// woraround for https://youtrack.jetbrains.com/issue/KT-13347
1213
//val args = listOf("foo", "bar")
1314

14-
15-
var test = File("sdf")
16-
val usage = """
17-
Usage: jl <command> [options] [<joblist_file>]
18-
19-
Supported commands are
20-
submit Submits a job to the underlying queuing system and adds it to the list
21-
add Extracts job-ids from stdin and adds them to the list
22-
wait Wait for a list of jobs to finish
23-
status Prints various statistics and allows to create an html report for the list
24-
kill Removes all jobs of this list from the scheduler queue
25-
up Moves a list of jobs to the top of a queue (if supported by the underlying scheduler)
26-
reset Removes all information related to this joblist.
27-
28-
If no <joblist_file> is provided, jl will use '.jobs' as default
15+
val usage ="""
16+
kscript - Enhanced scripting support for Kotlin on *nix-based systems.
17+
18+
Usage:
19+
kscript ( -t | --text ) <version>
20+
kscript [ --interactive | --idea | --package ] [--] ( - | <file or URL> ) [<args>]...
21+
kscript (-h | --help)
22+
kscript ( --self-update | --clear-cache )
23+
24+
Options:
25+
-t, --text Enable stdin support API for more streamlined text processing [default: latest]
26+
--package Package script and dependencies into self-dependent binary
27+
--idea boostrap IDEA from a kscript
28+
-i, --interactive Create interactive shell with dependencies as declared in script
29+
- Read script from the STDIN
30+
-h, --help Print this text
31+
--self-update Update kscript to the latest version
32+
--clear-cache Wipe cached script jars and urls
2933
"""
3034

31-
val doArgs = Docopt(usage).
32-
parse(args.toList()).
33-
map { it ->
34-
it.key.removePrefix("--").replace("[<>]", "") to {
35-
if (it.value == null) null else Objects.toString(it.value)
36-
}
37-
}
35+
val doArgs = Docopt(usage).parse(args.toList())
36+
37+
println("parsed args are: \n$doArgs (${doArgs.javaClass.simpleName})\n")
3838

39-
println("parsed args are: \n" + doArgs)
39+
doArgs.forEach { (key: Any, value: Any) ->
40+
println("$key:\t$value\t(${value?.javaClass?.canonicalName})")
41+
}
4042

41-
println("Hello from Kotlin!")
43+
println("\nHello from Kotlin!")
4244
for (arg in args) {
4345
println("arg: $arg")
4446
}

0 commit comments

Comments
 (0)