diff --git a/.gitpod.yml b/.gitpod.yml index 2a9f822..c8a367f 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -8,6 +8,7 @@ tasks: rm openrefine.tar.gz command: | sudo ln -s "${PWD}/orcli" /usr/local/bin/ + eval "$(orcli completions)" ./refine ports: diff --git a/README.md b/README.md index 7e9705f..a2884e5 100644 --- a/README.md +++ b/README.md @@ -103,5 +103,5 @@ gem install bashly 3. Generate script ```sh -bashly generate +bashly generate --upgrade ``` diff --git a/orcli b/orcli index 383cf01..876605f 100755 --- a/orcli +++ b/orcli @@ -1,10 +1,10 @@ #!/usr/bin/env bash -# This script was generated by bashly 0.7.9 (https://bashly.dannyb.co) +# This script was generated by bashly 0.8.7 (https://bashly.dannyb.co) # Modifying it manually is not recommended # :wrapper.bash3_bouncer if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then - printf "bash version 4 or higher is required\n" + printf "bash version 4 or higher is required\n" >&2 exit 1 fi @@ -28,21 +28,24 @@ orcli_usage() { fi printf "Usage:\n" - printf " orcli [command]\n" - printf " orcli [command] --help | -h\n" + printf " orcli COMMAND\n" + printf " orcli [COMMAND] --help | -h\n" printf " orcli --version | -v\n" echo # :command.usage_commands printf "Commands:\n" - echo " batch start tmp OpenRefine workspace and run multiple orcli commands" - echo " import import commands" - echo " list list projects on OpenRefine server" - echo " info show project metadata" - echo " export export commands" + echo " completions Generate bash completions" + echo " batch start tmp OpenRefine workspace and run multiple orcli commands" + echo " import import commands" + echo " list list projects on OpenRefine server" + echo " info show project metadata" + echo " export export commands" echo + # :command.long_usage if [[ -n $long_usage ]]; then printf "Options:\n" + # :command.usage_fixed_flags echo " --help, -h" printf " Show this help\n" @@ -59,9 +62,9 @@ orcli_usage() { printf " URL to OpenRefine server\n" printf " Default: http://localhost:3333\n" echo + # :command.usage_examples printf "Examples:\n" - printf " orcli import csv \"https://git.io/fj5hF\" --projectName \"duplicates\"\n" printf " orcli list\n" printf " orcli info \"duplicates\"\n" @@ -69,6 +72,7 @@ orcli_usage() { printf " orcli export tsv \"duplicates\" --output \"duplicates.tsv\"\n" printf " orcli batch \\\\\n import csv \"https://git.io/fj5hF\" --projectName \"duplicates\" \\\\\n info \"duplicates\" \\\\\n export tsv \"duplicates\"\n" echo + # :command.footer printf "https://github.com/opencultureconsulting/orcli\n" echo @@ -76,6 +80,38 @@ orcli_usage() { fi } +# :command.usage +orcli_completions_usage() { + if [[ -n $long_usage ]]; then + printf "orcli completions\n" + echo + + printf " Generate bash completions\n Usage: eval \"\$(orcli completions)\"\n" + echo + + else + printf "orcli completions - Generate bash completions\n" + echo + + fi + + printf "Usage:\n" + printf " orcli completions\n" + printf " orcli completions --help | -h\n" + echo + + # :command.long_usage + if [[ -n $long_usage ]]; then + printf "Options:\n" + + # :command.usage_fixed_flags + echo " --help, -h" + printf " Show this help\n" + echo + + fi +} + # :command.usage orcli_batch_usage() { if [[ -n $long_usage ]]; then @@ -89,16 +125,19 @@ orcli_batch_usage() { fi printf "Usage:\n" - printf " orcli batch [options] ORCLI COMMANDS...\n" + printf " orcli batch [OPTIONS] ORCLI COMMANDS...\n" printf " orcli batch --help | -h\n" echo + # :command.long_usage if [[ -n $long_usage ]]; then printf "Options:\n" + # :command.usage_fixed_flags echo " --help, -h" printf " Show this help\n" echo + # :command.usage_flags # :flag.usage echo " --memory RAM" @@ -116,6 +155,7 @@ orcli_batch_usage() { echo " --quiet, -q" printf " suppress log output, print errors only\n" echo + # :command.usage_args printf "Arguments:\n" @@ -125,7 +165,6 @@ orcli_batch_usage() { # :command.usage_examples printf "Examples:\n" - printf " orcli batch \\\\\n import csv \"https://git.io/fj5hF\" --projectName \"duplicates\" \\\\\n info \"duplicates\" \\\\\n export tsv \"duplicates\"\n" printf " orcli batch --memory \"2000M\" --port \"3334\" \\\\\n import csv \"https://git.io/fj5hF\" --projectName \"duplicates\" \\\\\n export tsv \"duplicates\"\n" printf " orcli batch --quiet \\\\\n import csv \"https://git.io/fj5hF\" --projectName \"duplicates\" \\\\\n export tsv \"duplicates\" --output \"output/duplicates.tsv\" \\\\\n bash -c 'wc -l output/*; echo \"finished\" in \$SECONDS seconds'\n" @@ -147,16 +186,18 @@ orcli_import_usage() { fi printf "Usage:\n" - printf " orcli import [command]\n" - printf " orcli import [command] --help | -h\n" + printf " orcli import COMMAND\n" + printf " orcli import [COMMAND] --help | -h\n" echo # :command.usage_commands printf "Commands:\n" echo " csv import comma-separated values (CSV)" echo + # :command.long_usage if [[ -n $long_usage ]]; then printf "Options:\n" + # :command.usage_fixed_flags echo " --help, -h" printf " Show this help\n" @@ -178,16 +219,19 @@ orcli_import_csv_usage() { fi printf "Usage:\n" - printf " orcli import csv [FILE...] [options]\n" + printf " orcli import csv [FILE...] [OPTIONS]\n" printf " orcli import csv --help | -h\n" echo + # :command.long_usage if [[ -n $long_usage ]]; then printf "Options:\n" + # :command.usage_fixed_flags echo " --help, -h" printf " Show this help\n" echo + # :command.usage_flags # :flag.usage echo " --separator SEPARATOR" @@ -214,6 +258,7 @@ orcli_import_csv_usage() { echo " --quiet, -q" printf " suppress log output, print errors only\n" echo + # :command.usage_args printf "Arguments:\n" @@ -225,7 +270,6 @@ orcli_import_csv_usage() { # :command.usage_examples printf "Examples:\n" - printf " orcli import csv \"file\"\n" printf " orcli import csv \"file1\" \"file2\"\n" printf " cat \"file\" | orcli import csv\n" @@ -249,16 +293,19 @@ orcli_list_usage() { fi printf "Usage:\n" - printf " orcli list [options]\n" + printf " orcli list [OPTIONS]\n" printf " orcli list --help | -h\n" echo + # :command.long_usage if [[ -n $long_usage ]]; then printf "Options:\n" + # :command.usage_fixed_flags echo " --help, -h" printf " Show this help\n" echo + # :command.usage_flags # :flag.usage echo " --quiet, -q" @@ -281,21 +328,25 @@ orcli_info_usage() { fi printf "Usage:\n" - printf " orcli info PROJECT [options]\n" + printf " orcli info PROJECT [OPTIONS]\n" printf " orcli info --help | -h\n" echo + # :command.long_usage if [[ -n $long_usage ]]; then printf "Options:\n" + # :command.usage_fixed_flags echo " --help, -h" 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" @@ -306,7 +357,6 @@ orcli_info_usage() { # :command.usage_examples printf "Examples:\n" - printf " info \"duplicates\"\n" printf " info 1234567890123\n" echo @@ -327,16 +377,18 @@ orcli_export_usage() { fi printf "Usage:\n" - printf " orcli export [command]\n" - printf " orcli export [command] --help | -h\n" + printf " orcli export COMMAND\n" + printf " orcli export [COMMAND] --help | -h\n" echo # :command.usage_commands printf "Commands:\n" echo " tsv export tab-separated values (TSV)" echo + # :command.long_usage if [[ -n $long_usage ]]; then printf "Options:\n" + # :command.usage_fixed_flags echo " --help, -h" printf " Show this help\n" @@ -358,16 +410,19 @@ orcli_export_tsv_usage() { fi printf "Usage:\n" - printf " orcli export tsv PROJECT [options]\n" + printf " orcli export tsv PROJECT [OPTIONS]\n" printf " orcli export tsv --help | -h\n" echo + # :command.long_usage if [[ -n $long_usage ]]; then printf "Options:\n" + # :command.usage_fixed_flags echo " --help, -h" printf " Show this help\n" echo + # :command.usage_flags # :flag.usage echo " --output FILE" @@ -384,6 +439,7 @@ orcli_export_tsv_usage() { echo " --quiet, -q" printf " suppress log output, print errors only\n" echo + # :command.usage_args printf "Arguments:\n" @@ -394,7 +450,6 @@ orcli_export_tsv_usage() { # :command.usage_examples printf "Examples:\n" - printf " orcli export tsv \"duplicates\"\n" printf " orcli export tsv \"duplicates\" --output \"duplicates.tsv\"\n" echo @@ -591,7 +646,92 @@ function post_import() { fi } +# src/lib/send_completions.sh +send_completions() { + echo $'# orcli completion -*- shell-script -*-' + echo $'' + echo $'# This bash completions script was generated by' + echo $'# completely (https://github.com/dannyben/completely)' + echo $'# Modifying it manually is not recommended' + echo $'' + echo $'_orcli_completions_filter() {' + echo $' local words="$1"' + echo $' local cur=${COMP_WORDS[COMP_CWORD]}' + echo $' local result=()' + echo $'' + echo $' if [[ "${cur:0:1}" == "-" ]]; then' + echo $' echo "$words"' + echo $' ' + echo $' else' + echo $' for word in $words; do' + echo $' [[ "${word:0:1}" != "-" ]] && result+=("$word")' + echo $' done' + echo $'' + echo $' echo "${result[*]}"' + echo $'' + echo $' fi' + echo $'}' + echo $'' + echo $'_orcli_completions() {' + echo $' local cur=${COMP_WORDS[COMP_CWORD]}' + echo $' local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")' + echo $' local compline="${compwords[*]}"' + echo $'' + echo $' case "$compline" in' + echo $' \'completions\'*)' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help -h")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' \'import csv\'*)' + 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 --quiet -h -q")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' \'import\'*)' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help -h csv")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' \'export\'*)' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help -h tsv")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' \'batch\'*)' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --memory --port --quiet -h -q")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' \'list\'*)' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --quiet -h -q")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' \'info\'*)' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --quiet -h -q")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' *)' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --version -h -v batch completions export import info list")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' esac' + echo $'} &&' + echo $'complete -F _orcli_completions orcli' + echo $'' + echo $'# ex: filetype=sh' +} + # :command.command_functions +# :command.function +orcli_completions_command() { + # src/completions_command.sh + # Users can now enable bash completion for this script by running: + # + # $ eval "$(orcli completions)" + # + send_completions +} + # :command.function orcli_batch_command() { # src/batch_command.sh @@ -659,6 +799,7 @@ orcli_batch_command() { "$orcli" "${command[@]}" fi done + } # :command.function @@ -692,6 +833,7 @@ orcli_import_csv_command() { # call post_import function to post data and validate results post_import "${data[@]}" + } # :command.function @@ -708,6 +850,7 @@ orcli_list_command() { echo "$response" | jq -r '.projects | keys[] as $k | "\($k):\(.[$k] | .name)"' fi fi + } # :command.function @@ -715,6 +858,7 @@ orcli_info_command() { # src/info_command.sh # shellcheck shell=bash disable=SC2154 get_id "${args[project]}" + } # :command.function @@ -755,6 +899,7 @@ orcli_export_tsv_command() { log "exported ${args[project]}" "file: ${args[--output]}" "rows: $(wc -l <"${args[--output]}")" fi fi + } # :command.parse_requirements @@ -773,17 +918,21 @@ parse_requirements() { ;; esac + # :command.environment_variables_filter + # :command.environment_variables_default export OPENREFINE_URL="${OPENREFINE_URL:-http://localhost:3333}" + # :command.dependencies_filter if ! [[ -x "$(command -v curl)" ]]; then - printf "missing dependency: curl\n" + printf "missing dependency: curl\n" >&2 exit 1 fi if ! [[ -x "$(command -v jq)" ]]; then - printf "missing dependency: jq\n" + printf "missing dependency: jq\n" >&2 exit 1 fi + # :command.command_filter action=${1:-} @@ -791,6 +940,13 @@ parse_requirements() { -* ) ;; + completions ) + action="completions" + shift + orcli_completions_parse_requirements "$@" + shift $# + ;; + batch ) action="batch" shift @@ -827,36 +983,77 @@ parse_requirements() { ;; # :command.command_fallback + "" ) + orcli_usage >&2 + exit 1 + ;; + * ) - orcli_usage + printf "invalid command: %s\n" "$action" >&2 exit 1 ;; esac + # :command.parse_requirements_while while [[ $# -gt 0 ]]; do key="$1" case "$key" in -?* ) - printf "invalid option: %s\n" "$key" + printf "invalid option: %s\n" "$key" >&2 exit 1 ;; * ) # :command.parse_requirements_case - printf "invalid argument: %s\n" "$key" + # :command.parse_requirements_case_simple + printf "invalid argument: %s\n" "$key" >&2 exit 1 + ;; esac done - # :command.required_args_filter - # :command.required_flags_filter - # :command.catch_all_filter - # :command.default_assignments - # :command.whitelist_filter - # :command.user_filter + +} + +# :command.parse_requirements +orcli_completions_parse_requirements() { + # :command.fixed_flags_filter + case "${1:-}" in + --help | -h ) + long_usage=yes + orcli_completions_usage + exit + ;; + + esac + + # :command.command_filter + action="completions" + + # :command.parse_requirements_while + while [[ $# -gt 0 ]]; do + key="$1" + case "$key" in + + -?* ) + printf "invalid option: %s\n" "$key" >&2 + exit 1 + ;; + + * ) + # :command.parse_requirements_case + # :command.parse_requirements_case_simple + printf "invalid argument: %s\n" "$key" >&2 + exit 1 + + ;; + + esac + done + } # :command.parse_requirements @@ -870,45 +1067,48 @@ orcli_batch_parse_requirements() { ;; esac - # :command.environment_variables_filter - # :command.dependencies_filter + # :command.command_filter action="batch" + # :command.parse_requirements_while while [[ $# -gt 0 ]]; do key="$1" case "$key" in # :flag.case --memory ) - # :flag.conflicts + + # :flag.case_arg if [[ -n ${2+x} ]]; then - # :flag.validations + args[--memory]="$2" shift shift else - printf "%s\n" "--memory requires an argument: --memory RAM" + printf "%s\n" "--memory requires an argument: --memory RAM" >&2 exit 1 fi ;; # :flag.case --port ) - # :flag.conflicts + + # :flag.case_arg if [[ -n ${2+x} ]]; then - # :flag.validations + args[--port]="$2" shift shift else - printf "%s\n" "--port requires an argument: --port PORT" + printf "%s\n" "--port requires an argument: --port PORT" >&2 exit 1 fi ;; # :flag.case --quiet | -q ) - # :flag.conflicts + + # :flag.case_no_arg args[--quiet]=1 shift ;; @@ -920,24 +1120,25 @@ orcli_batch_parse_requirements() { * ) # :command.parse_requirements_case - other_args+=("$1") - shift + # :command.parse_requirements_case_catch_all + other_args+=("$1") + shift + ;; esac done - # :command.required_args_filter - # :command.required_flags_filter + # :command.catch_all_filter if [[ ${#other_args[@]} -eq 0 ]]; then - printf "missing required argument: ORCLI COMMANDS...\nusage: orcli batch [options] ORCLI COMMANDS...\n" + printf "missing required argument: ORCLI COMMANDS...\nusage: orcli batch [OPTIONS] ORCLI COMMANDS...\n" >&2 exit 1 fi + # :command.default_assignments [[ -n ${args[--memory]:-} ]] || args[--memory]="2048M" [[ -n ${args[--port]:-} ]] || args[--port]="3333" - # :command.whitelist_filter - # :command.user_filter + } # :command.parse_requirements @@ -951,8 +1152,7 @@ orcli_import_parse_requirements() { ;; esac - # :command.environment_variables_filter - # :command.dependencies_filter + # :command.command_filter action=${1:-} @@ -968,36 +1168,39 @@ orcli_import_parse_requirements() { ;; # :command.command_fallback + "" ) + orcli_import_usage >&2 + exit 1 + ;; + * ) - orcli_import_usage + printf "invalid command: %s\n" "$action" >&2 exit 1 ;; esac + # :command.parse_requirements_while while [[ $# -gt 0 ]]; do key="$1" case "$key" in -?* ) - printf "invalid option: %s\n" "$key" + printf "invalid option: %s\n" "$key" >&2 exit 1 ;; * ) # :command.parse_requirements_case - printf "invalid argument: %s\n" "$key" + # :command.parse_requirements_case_simple + printf "invalid argument: %s\n" "$key" >&2 exit 1 + ;; esac done - # :command.required_args_filter - # :command.required_flags_filter - # :command.catch_all_filter - # :command.default_assignments - # :command.whitelist_filter - # :command.user_filter + } # :command.parse_requirements @@ -1011,97 +1214,101 @@ orcli_import_csv_parse_requirements() { ;; esac - # :command.environment_variables_filter - # :command.dependencies_filter + # :command.command_filter action="import csv" + # :command.parse_requirements_while while [[ $# -gt 0 ]]; do key="$1" case "$key" in # :flag.case --separator ) - # :flag.conflicts + + # :flag.case_arg if [[ -n ${2+x} ]]; then - # :flag.validations + args[--separator]="$2" shift shift else - printf "%s\n" "--separator requires an argument: --separator SEPARATOR" + printf "%s\n" "--separator requires an argument: --separator SEPARATOR" >&2 exit 1 fi ;; # :flag.case --encoding ) - # :flag.conflicts + + # :flag.case_arg if [[ -n ${2+x} ]]; then - # :flag.validations + args[--encoding]="$2" shift shift else - printf "%s\n" "--encoding requires an argument: --encoding ENCODING" + printf "%s\n" "--encoding requires an argument: --encoding ENCODING" >&2 exit 1 fi ;; # :flag.case --trimStrings ) - # :flag.conflicts + + # :flag.case_no_arg args[--trimStrings]=1 shift ;; # :flag.case --projectName ) - # :flag.conflicts + + # :flag.case_arg if [[ -n ${2+x} ]]; then - # :flag.validations + args[--projectName]="$2" shift shift else - printf "%s\n" "--projectName requires an argument: --projectName PROJECTNAME" + printf "%s\n" "--projectName requires an argument: --projectName PROJECTNAME" >&2 exit 1 fi ;; # :flag.case --quiet | -q ) - # :flag.conflicts + + # :flag.case_no_arg args[--quiet]=1 shift ;; -?* ) - printf "invalid option: %s\n" "$key" + printf "invalid option: %s\n" "$key" >&2 exit 1 ;; * ) # :command.parse_requirements_case + # :command.parse_requirements_case_repeatable if [[ -z ${args[file]+x} ]]; then - # :argument.validations + args[file]="\"$1\"" shift else args[file]="${args[file]} \"$1\"" shift fi + ;; esac done - # :command.required_args_filter - # :command.required_flags_filter - # :command.catch_all_filter + # :command.default_assignments [[ -n ${args[file]:-} ]] || args[file]="-" [[ -n ${args[--separator]:-} ]] || args[--separator]="," - # :command.whitelist_filter - # :command.user_filter + } # :command.parse_requirements @@ -1115,40 +1322,38 @@ orcli_list_parse_requirements() { ;; esac - # :command.environment_variables_filter - # :command.dependencies_filter + # :command.command_filter action="list" + # :command.parse_requirements_while while [[ $# -gt 0 ]]; do key="$1" case "$key" in # :flag.case --quiet | -q ) - # :flag.conflicts + + # :flag.case_no_arg args[--quiet]=1 shift ;; -?* ) - printf "invalid option: %s\n" "$key" + printf "invalid option: %s\n" "$key" >&2 exit 1 ;; * ) # :command.parse_requirements_case - printf "invalid argument: %s\n" "$key" + # :command.parse_requirements_case_simple + printf "invalid argument: %s\n" "$key" >&2 exit 1 + ;; esac done - # :command.required_args_filter - # :command.required_flags_filter - # :command.catch_all_filter - # :command.default_assignments - # :command.whitelist_filter - # :command.user_filter + } # :command.parse_requirements @@ -1162,50 +1367,50 @@ orcli_info_parse_requirements() { ;; esac - # :command.environment_variables_filter - # :command.dependencies_filter + # :command.command_filter action="info" + # :command.parse_requirements_while while [[ $# -gt 0 ]]; do key="$1" case "$key" in # :flag.case --quiet | -q ) - # :flag.conflicts + + # :flag.case_no_arg args[--quiet]=1 shift ;; -?* ) - printf "invalid option: %s\n" "$key" + printf "invalid option: %s\n" "$key" >&2 exit 1 ;; * ) # :command.parse_requirements_case + # :command.parse_requirements_case_simple if [[ -z ${args[project]+x} ]]; then - # :argument.validations + args[project]=$1 shift else - printf "invalid argument: %s\n" "$key" + printf "invalid argument: %s\n" "$key" >&2 exit 1 fi + ;; esac done + # :command.required_args_filter if [[ -z ${args[project]+x} ]]; then - printf "missing required argument: PROJECT\nusage: orcli info PROJECT [options]\n" + printf "missing required argument: PROJECT\nusage: orcli info PROJECT [OPTIONS]\n" >&2 exit 1 fi - # :command.required_flags_filter - # :command.catch_all_filter - # :command.default_assignments - # :command.whitelist_filter - # :command.user_filter + } # :command.parse_requirements @@ -1219,8 +1424,7 @@ orcli_export_parse_requirements() { ;; esac - # :command.environment_variables_filter - # :command.dependencies_filter + # :command.command_filter action=${1:-} @@ -1236,36 +1440,39 @@ orcli_export_parse_requirements() { ;; # :command.command_fallback + "" ) + orcli_export_usage >&2 + exit 1 + ;; + * ) - orcli_export_usage + printf "invalid command: %s\n" "$action" >&2 exit 1 ;; esac + # :command.parse_requirements_while while [[ $# -gt 0 ]]; do key="$1" case "$key" in -?* ) - printf "invalid option: %s\n" "$key" + printf "invalid option: %s\n" "$key" >&2 exit 1 ;; * ) # :command.parse_requirements_case - printf "invalid argument: %s\n" "$key" + # :command.parse_requirements_case_simple + printf "invalid argument: %s\n" "$key" >&2 exit 1 + ;; esac done - # :command.required_args_filter - # :command.required_flags_filter - # :command.catch_all_filter - # :command.default_assignments - # :command.whitelist_filter - # :command.user_filter + } # :command.parse_requirements @@ -1279,79 +1486,83 @@ orcli_export_tsv_parse_requirements() { ;; esac - # :command.environment_variables_filter - # :command.dependencies_filter + # :command.command_filter action="export tsv" + # :command.parse_requirements_while while [[ $# -gt 0 ]]; do key="$1" case "$key" in # :flag.case --output ) - # :flag.conflicts + + # :flag.case_arg if [[ -n ${2+x} ]]; then - # :flag.validations + args[--output]="$2" shift shift else - printf "%s\n" "--output requires an argument: --output FILE" + printf "%s\n" "--output requires an argument: --output FILE" >&2 exit 1 fi ;; # :flag.case --encoding ) - # :flag.conflicts + + # :flag.case_arg if [[ -n ${2+x} ]]; then - # :flag.validations + args[--encoding]="$2" shift shift else - printf "%s\n" "--encoding requires an argument: --encoding ENCODING" + printf "%s\n" "--encoding requires an argument: --encoding ENCODING" >&2 exit 1 fi ;; # :flag.case --quiet | -q ) - # :flag.conflicts + + # :flag.case_no_arg args[--quiet]=1 shift ;; -?* ) - printf "invalid option: %s\n" "$key" + printf "invalid option: %s\n" "$key" >&2 exit 1 ;; * ) # :command.parse_requirements_case + # :command.parse_requirements_case_simple if [[ -z ${args[project]+x} ]]; then - # :argument.validations + args[project]=$1 shift else - printf "invalid argument: %s\n" "$key" + printf "invalid argument: %s\n" "$key" >&2 exit 1 fi + ;; esac done + # :command.required_args_filter if [[ -z ${args[project]+x} ]]; then - printf "missing required argument: PROJECT\nusage: orcli export tsv PROJECT [options]\n" + printf "missing required argument: PROJECT\nusage: orcli export tsv PROJECT [OPTIONS]\n" >&2 exit 1 fi - # :command.required_flags_filter - # :command.catch_all_filter + # :command.default_assignments [[ -n ${args[--encoding]:-} ]] || args[--encoding]="UTF-8" - # :command.whitelist_filter - # :command.user_filter + } # :command.initialize @@ -1360,7 +1571,11 @@ initialize() { long_usage='' set -e + # :command.environment_variables_default + export OPENREFINE_URL="${OPENREFINE_URL:-http://localhost:3333}" + # src/initialize.sh + } # :command.run @@ -1371,7 +1586,15 @@ run() { normalize_input "$@" parse_requirements "${input[@]}" - if [[ $action == "batch" ]]; then + if [[ $action == "completions" ]]; then + if [[ ${args[--help]:-} ]]; then + long_usage=yes + orcli_completions_usage + else + orcli_completions_command + fi + + elif [[ $action == "batch" ]]; then if [[ ${args[--help]:-} ]]; then long_usage=yes orcli_batch_usage diff --git a/src/bashly.yml b/src/bashly.yml index a231a57..d4d99f9 100644 --- a/src/bashly.yml +++ b/src/bashly.yml @@ -25,6 +25,10 @@ examples: export tsv "duplicates" commands: + - name: completions + help: |- + Generate bash completions + Usage: eval "\$(orcli completions)" - name: batch help: start tmp OpenRefine workspace and run multiple orcli commands catch_all: diff --git a/src/completions_command.sh b/src/completions_command.sh new file mode 100644 index 0000000..e093647 --- /dev/null +++ b/src/completions_command.sh @@ -0,0 +1,5 @@ +# Users can now enable bash completion for this script by running: +# +# $ eval "$(orcli completions)" +# +send_completions \ No newline at end of file diff --git a/src/lib/send_completions.sh b/src/lib/send_completions.sh new file mode 100644 index 0000000..720fc54 --- /dev/null +++ b/src/lib/send_completions.sh @@ -0,0 +1,74 @@ +## [@bashly-upgrade completions send_completions] +send_completions() { + echo $'# orcli completion -*- shell-script -*-' + echo $'' + echo $'# This bash completions script was generated by' + echo $'# completely (https://github.com/dannyben/completely)' + echo $'# Modifying it manually is not recommended' + echo $'' + echo $'_orcli_completions_filter() {' + echo $' local words="$1"' + echo $' local cur=${COMP_WORDS[COMP_CWORD]}' + echo $' local result=()' + echo $'' + echo $' if [[ "${cur:0:1}" == "-" ]]; then' + echo $' echo "$words"' + echo $' ' + echo $' else' + echo $' for word in $words; do' + echo $' [[ "${word:0:1}" != "-" ]] && result+=("$word")' + echo $' done' + echo $'' + echo $' echo "${result[*]}"' + echo $'' + echo $' fi' + echo $'}' + echo $'' + echo $'_orcli_completions() {' + echo $' local cur=${COMP_WORDS[COMP_CWORD]}' + echo $' local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")' + echo $' local compline="${compwords[*]}"' + echo $'' + echo $' case "$compline" in' + echo $' \'completions\'*)' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help -h")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' \'import csv\'*)' + 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 --quiet -h -q")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' \'import\'*)' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help -h csv")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' \'export\'*)' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help -h tsv")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' \'batch\'*)' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --memory --port --quiet -h -q")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' \'list\'*)' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --quiet -h -q")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' \'info\'*)' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --quiet -h -q")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' *)' + echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_orcli_completions_filter "--help --version -h -v batch completions export import info list")" -- "$cur" )' + echo $' ;;' + echo $'' + echo $' esac' + echo $'} &&' + echo $'complete -F _orcli_completions orcli' + echo $'' + echo $'# ex: filetype=sh' +} \ No newline at end of file