From 1ca87a9a06d15e28b286234d2351bca814e716d8 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Mon, 22 Aug 2022 13:55:48 +0900 Subject: [PATCH] util (ble-import): support option "-q" --- docs/ChangeLog.md | 1 + note.txt | 8 ++++++ src/util.sh | 67 ++++++++++++++++++++++++++++++++++++----------- 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/docs/ChangeLog.md b/docs/ChangeLog.md index e8171261..8014f592 100644 --- a/docs/ChangeLog.md +++ b/docs/ChangeLog.md @@ -105,6 +105,7 @@ - util: suppress false warnings of `bind` inside non-interactive shells (reported by wukuan405) `#D1823` 1e19a67 - history: support `bleopt history_erasedups_limit` (motivated by SuperSandro2000) `#D1822` e4afb5a 3110967 - prompt: support `bleopt prompt_{emacs,vi}_mode_indicator` (motivated by ferdinandyb) `#D1843` 2b905f8 +- util (`ble-import`): support option `-q` `#D1859` XXXXXXX ## Changes diff --git a/note.txt b/note.txt index 616ee894..0dce2042 100644 --- a/note.txt +++ b/note.txt @@ -6636,6 +6636,14 @@ bash_tips 2022-08-21 + * util (ble-import): 調整する [#D1859] + + * done: source 時の引数の継承についてチェックする + * done: 既にロード済みのファイル一覧 --list, --list-imported → これは -q, + --query という名前で登録した。 + * done: ble-import --help でもっとちゃんとした説明を表示する (wiki も参考) + * done: wiki update. -f の説明がない。-q の説明も追加する。 + * blehook: ERR 関連の動作が怪しい [#D1858] x fixed: 更に ble/builtin/trap 経由で設定した通常の trap が diff --git a/src/util.sh b/src/util.sh index c1598b30..e6f389b3 100644 --- a/src/util.sh +++ b/src/util.sh @@ -5064,15 +5064,15 @@ function ble/util/import/finalize { _ble_util_import_files=() } ## @fn ble/util/import/.read-arguments args... -## @var[out] files +## @var[out] files not_found ## @var[out] flags -## d delay +## E error ## h help +## d delay ## f force -## E error +## q query function ble/util/import/.read-arguments { - flags= files=() - local -a not_found=() + flags= files=() not_found=() while (($#)); do local arg=$1; shift if [[ $flags != *-* ]]; then @@ -5085,6 +5085,7 @@ function ble/util/import/.read-arguments { (--delay) flags=d$flags ;; (--help) flags=h$flags ;; (--force) flags=f$flags ;; + (--query) flags=q$flags ;; (*) ble/util/print "ble-import: unrecognized option '$arg'" >&2 flags=E$flags ;; @@ -5095,7 +5096,7 @@ function ble/util/import/.read-arguments { for ((i=1;i<${#arg};i++)); do c=${arg:i:1} case $c in - ([df]) flags=$c$flags ;; + ([dfq]) flags=$c$flags ;; (*) ble/util/print "ble-import: unrecognized option '-$c'" >&2 flags=E$flags ;; @@ -5114,7 +5115,7 @@ function ble/util/import/.read-arguments { done # 存在しないファイルがあった時 - if [[ $flags != *f* ]] && ((${#not_found[@]})); then + if [[ $flags != *[fq]* ]] && ((${#not_found[@]})); then local file for file in "${not_found[@]}"; do ble/util/print "ble-import: file '$file' not found" >&2 @@ -5125,8 +5126,10 @@ function ble/util/import/.read-arguments { return 0 } function ble/util/import { - local file ext=0 ret enc - for file; do + local files file ext=0 ret enc + files=("$@") + set -- # Note #D: source によって引数が継承されるのを防ぐ + for file in "${files[@]}"; do ble/util/import/encode-filename "$file"; enc=$ret local guard=ble/util/import/guard:$enc ble/is-function "$guard" && return 0 @@ -5140,18 +5143,52 @@ function ble/util/import { done return "$ext" } +## @fn ble/util/import/option:query +## @var[in] files not_found +function ble/util/import/option:query { + if ((${#not_found[@]})); then + return 127 + elif ((${#files[@]})); then + local file + for file in "${files[@]}"; do + ble/util/import/is-loaded "$file" || return 1 + done + return 0 + else + ble/util/print-lines "${_ble_util_import_files[@]}" + return "$?" + fi +} + function ble-import { - local files flags + local files flags not_found ble/util/import/.read-arguments "$@" if [[ $flags == *[Eh]* ]]; then [[ $flags == *E* ]] && ble/util/print - { - ble/util/print 'usage: ble-import [-df] SCRIPTFILE...' - ble/util/print ' Search and source script files that have not yet been loaded.' - } >&2 + ble/util/print-lines \ + 'usage: ble-import [-dfq|--delay|--force|--query] [--] [SCRIPTFILE...]' \ + 'usage: ble-import --help' \ + ' Search and source script files that have not yet been loaded.' \ + '' \ + ' OPTIONS' \ + ' --help Show this help.' \ + ' -d, --delay Delay actual loading of the files if possible.' \ + ' -f, --force Ignore non-existent files without errors.' \ + ' -q, --query When SCRIPTFILEs are specified, test if all of these files' \ + ' are already loaded. Without SCRIPTFILEs, print the list of' \ + ' already imported files.' \ + '' \ + >&2 [[ $flags == *E* ]] && return 2 return 0 - elif ((!${#files[@]})); then + fi + + if [[ $flags == *q* ]]; then + ble/util/import/option:query + return "$?" + fi + + if ((!${#files[@]})); then [[ $flags == *f* ]] && return 0 ble/util/print 'ble-import: files are not specified.' >&2 return 2