From 1edc6f56729d2366b678dabbcf07a0ed8d4f2255 Mon Sep 17 00:00:00 2001 From: felixlohmeier Date: Mon, 14 Nov 2022 22:46:18 +0000 Subject: [PATCH] add quiet flag --- orcli | 89 +++++++++++++++++++++++++++---------- src/bashly.yml | 18 +++++--- src/lib/logging.sh | 2 +- src/lib/send_completions.sh | 10 ++--- src/run_command.sh | 5 +++ 5 files changed, 90 insertions(+), 34 deletions(-) diff --git a/orcli b/orcli index 6495723..2440e18 100755 --- a/orcli +++ b/orcli @@ -28,7 +28,7 @@ orcli_usage() { fi printf "Usage:\n" - printf " orcli [OPTIONS] COMMAND\n" + printf " orcli COMMAND\n" printf " orcli [COMMAND] --help | -h\n" printf " orcli --version | -v\n" echo @@ -55,12 +55,6 @@ orcli_usage() { printf " Show version number\n" echo - # :command.usage_flags - # :flag.usage - echo " --quiet, -q" - printf " suppress log output, print errors only\n" - echo - # :command.usage_environment_variables printf "Environment Variables:\n" @@ -202,6 +196,11 @@ orcli_import_csv_usage() { printf " set a name for the OpenRefine project\n" echo + # :flag.usage + echo " --quiet, -q" + printf " suppress log output, print errors only\n" + echo + # :command.usage_args printf "Arguments:\n" @@ -308,7 +307,7 @@ orcli_transform_usage() { fi printf "Usage:\n" - printf " orcli transform PROJECT [FILE...]\n" + printf " orcli transform PROJECT [FILE...] [OPTIONS]\n" printf " orcli transform --help | -h\n" echo @@ -321,6 +320,12 @@ orcli_transform_usage() { printf " Show this help\n" echo + # :command.usage_flags + # :flag.usage + echo " --quiet, -q" + printf " suppress log output, print errors only\n" + echo + # :command.usage_args printf "Arguments:\n" @@ -417,6 +422,11 @@ orcli_export_tsv_usage() { printf " Default: UTF-8\n" echo + # :flag.usage + echo " --quiet, -q" + printf " suppress log output, print errors only\n" + echo + # :command.usage_args printf "Arguments:\n" @@ -478,6 +488,11 @@ orcli_run_usage() { printf " do not exit on error and keep bash shell open\n" echo + # :flag.usage + echo " --quiet, -q" + printf " suppress log output, print errors only\n" + echo + # :command.usage_args printf "Arguments:\n" @@ -664,7 +679,7 @@ function error() { exit 1 } function log() { - if ! [[ ${args[--quiet]} ]]; then + if ! [[ ${args[--quiet]} || $ORCLI_QUIET ]]; then echo >&2 "[$(date +'%Y-%m-%dT%H:%M:%S')] $1" shift for msg in "$@"; do echo >&2 " $msg"; done @@ -739,15 +754,15 @@ send_completions() { echo $' ;;' echo $'' echo $' \'import csv\'*)' - echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--encoding --help --projectName --separator --trimStrings -h")" -- "$cur" )' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--encoding --help --projectName --quiet --separator --trimStrings -h -q")" -- "$cur" )' echo $' ;;' echo $'' echo $' \'export tsv\'*)' - echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--encoding --help --output -h")" -- "$cur" )' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--encoding --help --output --quiet -h -q")" -- "$cur" )' echo $' ;;' echo $'' echo $' \'transform\'*)' - 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 --quiet -h -q")" -- "$cur" )' echo $' ;;' echo $'' echo $' \'import\'*)' @@ -767,11 +782,11 @@ send_completions() { echo $' ;;' echo $'' echo $' \'run\'*)' - echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --interactive --memory --port -h")" -- "$cur" )' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --interactive --memory --port --quiet -h -q")" -- "$cur" )' echo $' ;;' echo $'' echo $' *)' - echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --quiet --version -h -q -v completions export import info list run transform")" -- "$cur" )' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --version -h -v completions export import info list run transform")" -- "$cur" )' echo $' ;;' echo $'' echo $' esac' @@ -1004,6 +1019,11 @@ orcli_run_command() { fi fi + # assume that quiet flag shall suppress log output generally in batch mode + if [[ ${args[--quiet]} ]]; then + export ORCLI_QUIET=1 + fi + # update OPENREFINE_URL env OPENREFINE_URL="http://localhost:${args[--port]}" @@ -1099,14 +1119,6 @@ parse_requirements() { exit ;; - # :flag.case - --quiet | -q ) - - # :flag.case_no_arg - args[--quiet]=1 - shift - ;; - esac # :command.environment_variables_filter @@ -1387,6 +1399,14 @@ orcli_import_csv_parse_requirements() { fi ;; + # :flag.case + --quiet | -q ) + + # :flag.case_no_arg + args[--quiet]=1 + shift + ;; + -?* ) printf "invalid option: %s\n" "$key" >&2 exit 1 @@ -1522,6 +1542,13 @@ orcli_transform_parse_requirements() { while [[ $# -gt 0 ]]; do key="$1" case "$key" in + # :flag.case + --quiet | -q ) + + # :flag.case_no_arg + args[--quiet]=1 + shift + ;; -?* ) printf "invalid option: %s\n" "$key" >&2 @@ -1551,7 +1578,7 @@ orcli_transform_parse_requirements() { # :command.required_args_filter if [[ -z ${args[project]+x} ]]; then - printf "missing required argument: PROJECT\nusage: orcli transform PROJECT [FILE...]\n" >&2 + printf "missing required argument: PROJECT\nusage: orcli transform PROJECT [FILE...] [OPTIONS]\n" >&2 exit 1 fi @@ -1671,6 +1698,14 @@ orcli_export_tsv_parse_requirements() { fi ;; + # :flag.case + --quiet | -q ) + + # :flag.case_no_arg + args[--quiet]=1 + shift + ;; + -?* ) printf "invalid option: %s\n" "$key" >&2 exit 1 @@ -1761,6 +1796,14 @@ orcli_run_parse_requirements() { shift ;; + # :flag.case + --quiet | -q ) + + # :flag.case_no_arg + args[--quiet]=1 + shift + ;; + -?* ) printf "invalid option: %s\n" "$key" >&2 exit 1 diff --git a/src/bashly.yml b/src/bashly.yml index 4f05d8b..78ac33d 100644 --- a/src/bashly.yml +++ b/src/bashly.yml @@ -27,11 +27,6 @@ examples: orcli export tsv "duplicates" EOF -flags: - - long: --quiet - short: -q - help: suppress log output, print errors only - commands: - name: completions help: |- @@ -62,6 +57,9 @@ commands: - long: --projectName arg: projectName help: set a name for the OpenRefine project + - long: --quiet + short: -q + help: suppress log output, print errors only examples: - orcli import csv "file" - orcli import csv "file1" "file2" @@ -97,6 +95,10 @@ commands: help: Path to one or more files or URLs containing OpenRefine's undo/redo operation history in JSON format. When FILE is -, read standard input. default: "-" repeatable: true + flags: + - long: --quiet + short: -q + help: suppress log output, print errors only examples: - orcli transform "duplicates" "history.json" - cat "history.json" | orcli transform "duplicates" @@ -121,6 +123,9 @@ commands: help: set character encoding arg: encoding default: "UTF-8" + - long: --quiet + short: -q + help: suppress log output, print errors only examples: - orcli export tsv "duplicates" - orcli export tsv "duplicates" --output "duplicates.tsv" @@ -143,6 +148,9 @@ commands: default: "3333" - long: --interactive help: do not exit on error and keep bash shell open + - long: --quiet + short: -q + help: suppress log output, print errors only examples: - orcli run --interactive - |- diff --git a/src/lib/logging.sh b/src/lib/logging.sh index a2a0a1f..33ac77f 100644 --- a/src/lib/logging.sh +++ b/src/lib/logging.sh @@ -7,7 +7,7 @@ function error() { exit 1 } function log() { - if ! [[ ${args[--quiet]} ]]; then + if ! [[ ${args[--quiet]} || $ORCLI_QUIET ]]; then echo >&2 "[$(date +'%Y-%m-%dT%H:%M:%S')] $1" shift for msg in "$@"; do echo >&2 " $msg"; done diff --git a/src/lib/send_completions.sh b/src/lib/send_completions.sh index 3771a63..7877132 100644 --- a/src/lib/send_completions.sh +++ b/src/lib/send_completions.sh @@ -35,15 +35,15 @@ send_completions() { echo $' ;;' echo $'' echo $' \'import csv\'*)' - echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--encoding --help --projectName --separator --trimStrings -h")" -- "$cur" )' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--encoding --help --projectName --quiet --separator --trimStrings -h -q")" -- "$cur" )' echo $' ;;' echo $'' echo $' \'export tsv\'*)' - echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--encoding --help --output -h")" -- "$cur" )' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--encoding --help --output --quiet -h -q")" -- "$cur" )' echo $' ;;' echo $'' echo $' \'transform\'*)' - 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 --quiet -h -q")" -- "$cur" )' echo $' ;;' echo $'' echo $' \'import\'*)' @@ -63,11 +63,11 @@ send_completions() { echo $' ;;' echo $'' echo $' \'run\'*)' - echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --interactive --memory --port -h")" -- "$cur" )' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --interactive --memory --port --quiet -h -q")" -- "$cur" )' echo $' ;;' echo $'' echo $' *)' - echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --quiet --version -h -q -v completions export import info list run transform")" -- "$cur" )' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --version -h -v completions export import info list run transform")" -- "$cur" )' echo $' ;;' echo $'' echo $' esac' diff --git a/src/run_command.sh b/src/run_command.sh index 41c1dd0..a008213 100644 --- a/src/run_command.sh +++ b/src/run_command.sh @@ -14,6 +14,11 @@ if ! [[ ${args[--interactive]} ]]; then fi fi +# assume that quiet flag shall suppress log output generally in batch mode +if [[ ${args[--quiet]} ]]; then + export ORCLI_QUIET=1 +fi + # update OPENREFINE_URL env OPENREFINE_URL="http://localhost:${args[--port]}"