Skip to content

Commit

Permalink
util (ble-import): support option "-q"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Aug 24, 2022
1 parent 94d1371 commit 1ca87a9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions note.txt
Expand Up @@ -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 が
Expand Down
67 changes: 52 additions & 15 deletions src/util.sh
Expand Up @@ -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
Expand All @@ -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 ;;
Expand All @@ -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 ;;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1ca87a9

Please sign in to comment.