Skip to content

hql on the command line

With source and sink, hql is a structured-data tool for the shell. A first argument that names no command is a program, so hql '<program>' is hql eval '<program>', and hql -c '<program>' is the same, as -c is in Python and Bash. A first argument written as a file, report.hql or ./report, runs that file instead (scripts):

$ cat examples/io/sightings.jsonl | hql 'source(stdin, Json) | filter(s => s.seen == "morning") | map(s => s.species)'
"wren"
"kingfisher"

The result is a Stream<String>, so the host writes each value as it arrives, in HQL, the format a result is written in unless --to names another (writing results out).

Options of hql come before the program. Every word after the program is the program's own, read with io.env.argv(), as the words after a Python or Bash script are (Scripts).

--to and --compact

--to json presents each document of the result pretty-printed, as jq . does, and --compact gives one line each, as jq -c does:

$ head -1 examples/io/sightings.jsonl | hql --to json 'source(stdin, Json)'
{
  "seen": "dusk",
  "site": "meadow",
  "species": "barn owl"
}
$ head -2 examples/io/sightings.jsonl | hql --to json --compact 'source(stdin, Json)'
{"seen":"dusk","site":"meadow","species":"barn owl"}
{"seen":"morning","site":"hedge","species":"wren"}

Concatenated JSON values are what source(…, Json) reads, so the output of one hql is the input of the next. sink(e, Json) writes one document per line whatever the flags: a program writing a file says what the file is, and --compact is about the terminal.

Converting between formats is a source and a --to:

$ hql --to yaml 'source(file("examples/io/sightings.jsonl")) | map(s => s.site)'
- meadow
- hedge
- barn
- river

--from

--from names the format of standard input once, on the command line, so the program reads stdin without naming one; a format the program names wins:

$ cat examples/io/sightings.jsonl | hql --from json 'source(stdin) | map(s => s.site)'
"meadow"
"hedge"
"barn"
"river"

Input and output stay in the program

The command line names no input and no output of a program's own. Input is source or tap in the program, output is target or sink in the program, and --to only chooses how the host writes the final result. A program whose result is Step prints nothing:

$ cat examples/io/sightings.jsonl | hql 'source(stdin, Json) | filter(s => s.seen == "morning") | map(s => s.species) | sink(stdout, Text)'
wren
kingfisher

hql run - reads the program from standard input, so such a program cannot also read stdin as data, and source(stdin, …) in it is a type error. hql with no program does the same when standard input is not a terminal, and opens the REPL when it is:

$ echo '1 + 1' | hql
2

--log-level sets every logger's threshold (logging).

A program in a file

hql FILE runs a file, the same as hql run FILE, when the word ends in .hql or contains a /. A file whose first line is #!/usr/bin/env hql runs on its own, and the exit status follows from its result. scripts covers arguments, the exit status, and where a script's relative paths resolve.