Skip to content

Commit e7ef9d9

Browse files
committed
added basic usage help to CLI
1 parent 1377bb2 commit e7ef9d9

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,22 @@ Inline Usage
6666

6767
You can even inline `kscript` solutions into larger scripts, because `kscript` can read from stdin as well. So, depdending your preference you could simply pipe a kotlin snippet into `kscript`
6868

69-
```
69+
```{bash}
7070
echo '
7171
println("hello kotlin")
72-
' | kscript
72+
' | kscript -
7373
```
7474

7575

7676
or do the same using `heredoc` (preferred solution) which gives you some more flexibility to also use single quotes in your script:
77-
```
77+
```{bash}
7878
kscript - <<"EOF"
7979
println("hello kotlin and heredoc")
8080
EOF
8181
```
8282

8383
Since the piped content is considered as a regular script it can also have dependencies
84-
```
84+
```{bash}
8585
kscript - <<"EOF"
8686
//DEPS org.docopt:docopt:0.6.0-SNAPSHOT log4j:log4j:1.2.14
8787
@@ -92,6 +92,11 @@ println("hello again")
9292
EOF
9393
```
9494

95+
Finally (for sake of completeness) it also works with process substitution
96+
```{bash}
97+
kscript <(echo 'println("k-onliner")')
98+
```
99+
95100
Inlined _kscripts_ are also cached based on `md5` checksum, so running the same snippet again will use a cached jar (sitting in `$TMPDIR`).
96101

97102

examples/unit_tests.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ expandcp.kts org.docopt:docopt:0.6.0-SNAPSHOT
1919

2020
## (1) via pipe
2121
echo '
22-
println(1+1)
23-
' | kscript
22+
println(1+3)
23+
' | kscript -
2424

2525

2626
## (2) heredoc

kscript

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@
33
KSCRIPT_VERSION=1.0
44

55

6+
## Show basic CLI description if no arguments are provided
7+
if [ $# == 0 ] || [ $1 == "-v" ] || [ $1 == "--version" ] || [ $1 == "-h" ] || [ $1 == "--help" ]; then
8+
echo "Usage : kscript <scriptfile or - for stdin> [<script_args>]+" >&2
9+
echo "" >&2
10+
echo "Easy-to-use scripting support for Kotlin on *nix-based systems." >&2
11+
echo "" >&2
12+
echo "Copyright : 2016 Holger Brandl"
13+
echo "License : Simplified BSD" >&2
14+
echo "Version : v$KSCRIPT_VERSION" >&2
15+
echo "Website : https://github.com/holgerbrandl/kscript" >&2
16+
exit 1;
17+
fi
18+
19+
620
## ... or use a cached version of it if possible
721
#scriptFile="./test.kts"
822
scriptFile=$1
@@ -21,10 +35,11 @@ if [ "$scriptFile" == "-" ]; then scriptFile="/dev/stdin"; fi
2135
## Rather Test if script ends with kts to also support process substitution here. Wrap stdin
2236
# http://serverfault.com/questions/52034/what-is-the-difference-between-double-and-single-square-brackets-in-bash
2337
# https://viewsby.wordpress.com/2013/09/06/bash-string-ends-with/
38+
2439
if [[ "$scriptFile" != *kts ]]; then
2540
tmpScript=${TMPDIR=/tmp}/kscript_stdin_$(mktemp -d XXXXXXXXX).kts # odd but works on macos as well
2641
# tmpScript=/tmp/kscript_stdin_$(mktemp -d XXXXXXXXX).kts # odd but works on macos as well
27-
cat ${scriptFile} > ${tmpScript}
42+
cat "${scriptFile}" > ${tmpScript}
2843

2944
## rename to use checksum as name to allow for jar-caching also when using stdin
3045
stdinMD5=$(md5sum ${tmpScript} | cut -c1-6)

0 commit comments

Comments
 (0)