Skip to content

Commit

Permalink
Improve docopt example (#118)
Browse files Browse the repository at this point in the history
* Use kscript command options as an example of usage DocOpt
* Improve text of the Usage example
  • Loading branch information
C06A authored and holgerbrandl committed Apr 9, 2018
1 parent d3bcb13 commit df34204
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions examples/classpath_example.kts
@@ -1,4 +1,5 @@
#!/usr/bin/env kscript

//DEPS com.offbytwo:docopt:0.6.0.20150202,log4j:log4j:1.2.14

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


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

val doArgs = Docopt(usage).
parse(args.toList()).
map { it ->
it.key.removePrefix("--").replace("[<>]", "") to {
if (it.value == null) null else Objects.toString(it.value)
}
}
val doArgs = Docopt(usage).parse(args.toList())

println("parsed args are: \n$doArgs (${doArgs.javaClass.simpleName})\n")

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

println("Hello from Kotlin!")
println("\nHello from Kotlin!")
for (arg in args) {
println("arg: $arg")
}

0 comments on commit df34204

Please sign in to comment.