search: flag --index, help text, test script

This commit is contained in:
Felix Lohmeier 2023-10-28 11:53:38 +00:00
parent 216991d212
commit ec7d77643d
6 changed files with 114 additions and 21 deletions

View File

@ -1,13 +1,19 @@
# orcli search
```
orcli search - apply regex to each column and print matches in flattened tsv format
orcli search
apply regex to each column and print matches in flattened tsv format
output: index column value
Usage:
orcli search PROJECT [REGEX]
orcli search PROJECT [REGEX] [OPTIONS]
orcli search --help | -h
Options:
--index COLUMN
print column values instead of row.index in the first column of the output
--help, -h
Show this help
@ -16,12 +22,13 @@ Arguments:
project name or id
REGEX
search
search term (regular expression, case-sensitive)
Examples:
orcli search "duplicates" "^Ben"
orcli search 1234567890123 "^Ben"
orcli search "duplicates" "^Ben" | column -t -s $' '
orcli search "duplicates" "^F$" --index "email"
orcli search "duplicates" | column -t -s $' '
```

52
orcli
View File

@ -568,7 +568,10 @@ orcli_info_usage() {
# :command.usage
orcli_search_usage() {
if [[ -n $long_usage ]]; then
printf "orcli search - apply regex to each column and print matches in flattened tsv format\n"
printf "orcli search\n"
echo
printf " apply regex to each column and print matches in flattened tsv format\n output: index column value\n"
echo
else
@ -578,7 +581,7 @@ orcli_search_usage() {
fi
printf "%s\n" "Usage:"
printf " orcli search PROJECT [REGEX]\n"
printf " orcli search PROJECT [REGEX] [OPTIONS]\n"
printf " orcli search --help | -h\n"
echo
@ -586,6 +589,12 @@ orcli_search_usage() {
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_flags
# :flag.usage
printf " %s\n" "--index COLUMN"
printf " print column values instead of row.index in the first column of the output\n"
echo
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
@ -601,14 +610,15 @@ orcli_search_usage() {
# :argument.usage
printf " %s\n" "REGEX"
printf " search\n"
printf " search term (regular expression, case-sensitive)\n"
echo
# :command.usage_examples
printf "%s\n" "Examples:"
printf " orcli search \"duplicates\" \"^Ben\"\n"
printf " orcli search 1234567890123 \"^Ben\"\n"
printf " orcli search \"duplicates\" \"^Ben\" | column -t -s \$'\t'\n"
printf " orcli search \"duplicates\" \"^F$\" --index \"email\"\n"
printf " orcli search \"duplicates\" | column -t -s \$'\t'\n"
echo
fi
@ -1294,7 +1304,7 @@ send_completions() {
echo $' ;;'
echo $''
echo $' \'search\'*)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help -h")" -- "$cur" )'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --index -h")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' \'export\'*)'
@ -1595,14 +1605,23 @@ orcli_search_command() {
projectid="$(get_id "${args[project]}")"
# set facets config
args[facets]="[ { \"type\": \"list\", \"expression\": \"grel:filter(row.columnNames,cn,cells[cn].value.find(/${args[--regex]}/).length()>0).length()>0\", \"columnName\": \"\", \"selection\": [ { \"v\": { \"v\": true } } ] } ]"
args['--facets']='[ { "type": "list", "expression": "grel:filter(row.columnNames,cn,cells[cn].value.find(/'
args['--facets']+="${args[regex]}"
args['--facets']+='/).length()>0).length()>0", "columnName": "", "selection": [ { "v": { "v": true } } ] } ]'
# set template
template='{{'
template+='forEach(filter(row.columnNames, cn, cells[cn].value.find(/'
template+="${args[regex]}"
template+='/).length()>0), cn, '
template+='(row.record.fromRowIndex + 1) + "\t" + cn + "\t" + '
template+='/).length()>0), cn,'
if [[ ${args[--index]} ]]; then
template+='cells["'
template+="${args[--index]}"
template+='"].value'
else
template+='(row.index + 1)'
fi
template+='+ "\t" + cn + "\t" +'
template+='forNonBlank(cells[cn].value, v, if(v.contains(" "), if(v.contains('\''"'\''), '\''"'\'' + v.replace('\''"'\'','\''""'\'') + '\''"'\'', '\''"'\'' + v + '\''"'\''), v),"")'
template+='+ "\n")'
template+='}}'
@ -1614,6 +1633,7 @@ orcli_search_command() {
# call post_export function to post data and validate results
post_export "${data[@]}"
}
# :command.function
@ -3017,6 +3037,20 @@ orcli_search_parse_requirements() {
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in
# :flag.case
--index)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
args['--index']="$2"
shift
shift
else
printf "%s\n" "--index requires an argument: --index COLUMN" >&2
exit 1
fi
;;
-?*)
printf "invalid option: %s\n" "$key" >&2
@ -3046,7 +3080,7 @@ orcli_search_parse_requirements() {
# :command.required_args_filter
if [[ -z ${args['project']+x} ]]; then
printf "missing required argument: PROJECT\nusage: orcli search PROJECT [REGEX]\n" >&2
printf "missing required argument: PROJECT\nusage: orcli search PROJECT [REGEX] [OPTIONS]\n" >&2
exit 1
fi

View File

@ -208,16 +208,23 @@ commands:
- orcli info "duplicates" | jq -r .columns[]
- name: search
help: apply regex to each column and print matches in flattened tsv format
help: |-
apply regex to each column and print matches in flattened tsv format
output: index column value
args:
- *project
- &regex
name: regex
help: search
help: search term (regular expression, case-sensitive)
flags:
- long: --index
help: print column values instead of row.index in the first column of the output
arg: column
examples:
- orcli search "duplicates" "^Ben"
- orcli search 1234567890123 "^Ben"
- orcli search "duplicates" "^Ben" | column -t -s \$'\t'
- orcli search "duplicates" "^F$" --index "email"
- orcli search "duplicates" | column -t -s \$'\t'
- name: test
help: run functional tests on tmp OpenRefine workspace

View File

@ -67,7 +67,7 @@ send_completions() {
echo $' ;;'
echo $''
echo $' \'search\'*)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help -h")" -- "$cur" )'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --index -h")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' \'export\'*)'

View File

@ -4,14 +4,23 @@
projectid="$(get_id "${args[project]}")"
# set facets config
args[facets]="[ { \"type\": \"list\", \"expression\": \"grel:filter(row.columnNames,cn,cells[cn].value.find(/${args[--regex]}/).length()>0).length()>0\", \"columnName\": \"\", \"selection\": [ { \"v\": { \"v\": true } } ] } ]"
args['--facets']='[ { "type": "list", "expression": "grel:filter(row.columnNames,cn,cells[cn].value.find(/'
args['--facets']+="${args[regex]}"
args['--facets']+='/).length()>0).length()>0", "columnName": "", "selection": [ { "v": { "v": true } } ] } ]'
# set template
template='{{'
template+='forEach(filter(row.columnNames, cn, cells[cn].value.find(/'
template+="${args[regex]}"
template+='/).length()>0), cn, '
template+='(row.record.fromRowIndex + 1) + "\t" + cn + "\t" + '
template+='/).length()>0), cn,'
if [[ ${args[--index]} ]]; then
template+='cells["'
template+="${args[--index]}"
template+='"].value'
else
template+='(row.index + 1)'
fi
template+='+ "\t" + cn + "\t" +'
template+='forNonBlank(cells[cn].value, v, if(v.contains(" "), if(v.contains('\''"'\''), '\''"'\'' + v.replace('\''"'\'','\''""'\'') + '\''"'\'', '\''"'\'' + v + '\''"'\''), v),"")'
template+='+ "\n")'
template+='}}'
@ -22,4 +31,4 @@ data+=("format=template")
data+=("template=${template}")
# call post_export function to post data and validate results
post_export "${data[@]}"
post_export "${data[@]}"

36
tests/search.sh Normal file
View File

@ -0,0 +1,36 @@
#!/bin/bash
t="search"
# create tmp directory
tmpdir="$(mktemp -d)"
trap '{ rm -rf "${tmpdir}"; }' 0 2 3 15
# input
cat << "DATA" > "${tmpdir}/${t}.csv"
email,name,state,gender,purchase
danny.baron@example1.com,Danny Baron,CA,M,TV
melanie.white@example2.edu,Melanie White,NC,F,iPhone
danny.baron@example1.com,D. Baron,CA,M,Winter jacket
ben.tyler@example3.org,Ben Tyler,NV,M,Flashlight
arthur.duff@example4.com,Arthur Duff,OR,M,Dining table
danny.baron@example1.com,Daniel Baron,CA,M,Bike
jean.griffith@example5.org,Jean Griffith,WA,F,Power drill
melanie.white@example2.edu,Melanie White,NC,F,iPad
ben.morisson@example6.org,Ben Morisson,FL,M,Amplifier
arthur.duff@example4.com,Arthur Duff,OR,M,Night table
DATA
# assertion
cat << "DATA" > "${tmpdir}/${t}.assert"
melanie.white@example2.edu name Melanie White
melanie.white@example2.edu name "Melanie White"
DATA
# action
cd "${tmpdir}" || exit 1
orcli import csv "${t}.csv" --projectName "${t}"
orcli search "${t}" "^Mel" --index "email" > "${t}.output"
# test
diff -u "${t}.assert" "${t}.output"