📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
ページの作成:「== 概要 == Fishシェルの補完システムは非常に強力で柔軟性がある。<br> <br> Fishの補完システムを理解するには、これらの概念を理解して、実際に補完スクリプトを記述することが重要である。<br> また、既存の補完スクリプト (~/.config/fish/completionsディレクトリ) を参考にする。<br> <br> 補完システムの詳細を知りたい場合は、Fishの公式サイトにある[https…」 |
|||
| 79行目: | 79行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
== サブコマンドのオプション補完 == | |||
サブコマンドのオプション補完を行う場合は、以下に示すような手順を行う。<br> | |||
# メインコマンドの補完を定義する。 | |||
# サブコマンドの補完を定義する。 | |||
# サブコマンドのオプション補完を定義する。 | |||
<br> | |||
以下に示す関数およびオプションを使用して、サブコマンドとそのオプションの補完を細かく制御できる。<br> | |||
* __fish_use_subcommand関数 | |||
*: Fishの組み込み関数であり、現在のコマンドラインにサブコマンドがまだ入力されていない場合に真を返す。 | |||
*: これにより、サブコマンドの補完を適切なタイミングで提供することができる。 | |||
* __fish_git_using_command clone関数 | |||
*: Fishの補完ファイル (completionsディレクトリ) で定義される関数である。 | |||
*: この関数は、現在のコマンドラインでサブコマンドが使用されている場合に真を返す。 | |||
* -nオプション | |||
*: これは、conditionオプションであり、指定された条件が真の場合にのみ補完を適用する。 | |||
* -fオプション | |||
*: これは、no-filesオプションであり、ファイル名の補完を無効にする。 | |||
* -xオプション | |||
*: これは、requires-parameterオプションであり、このオプションが値を取ることを示す。 | |||
* -aオプション | |||
*: これは、argumentsオプションであり、補完の候補を指定する。 | |||
* -dオプション | |||
*: これは、descriptionオプションであり、補完候補の説明を提供する。 | |||
<br> | |||
サブコマンドのオプションの補完を効果的に実装するには、以下に示す事柄を考慮する。<br> | |||
* 補完関数を作成して、現在のコマンドラインの状態を判断する。 | |||
* 条件付き補完を使用して、適切なコンテキストで正しい補完を提供する。 | |||
* 可能な場合は、動的補完を使用して、実行時に補完候補を生成する。 | |||
<br> | |||
以下の例では、git cloneコマンドのオプション補完を示している。<br> | |||
完全なgitコマンドの補完スクリプトは、Fishのインストールディレクトリ内にあるcompletionsディレクトリのgit.fishファイルで確認できる。<br> | |||
<br> | |||
<syntaxhighlight lang="fish"> | |||
function __fish_git_needs_command | |||
# Figure out if the current invocation already has a command. | |||
# | |||
# This is called hundreds of times and the argparse is kinda slow, | |||
# so we cache it as long as the commandline doesn't change. | |||
set -l cmdline "$(commandline -c)" | |||
if set -q __fish_git_cmdline; and test "$cmdline" = "$__fish_git_cmdline" | |||
if set -q __fish_git_cmd[1] | |||
echo -- $__fish_git_cmd | |||
return 1 | |||
end | |||
return 0 | |||
end | |||
set -g __fish_git_cmdline $cmdline | |||
set -l cmd (commandline -opc) | |||
set -e cmd[1] | |||
argparse -s (__fish_git_global_optspecs) -- $cmd 2>/dev/null | |||
or return 0 | |||
# These flags function as commands, effectively. | |||
set -q _flag_version; and return 1 | |||
set -q _flag_html_path; and return 1 | |||
set -q _flag_man_path; and return 1 | |||
set -q _flag_info_path; and return 1 | |||
if set -q argv[1] | |||
# Also print the command, so this can be used to figure out what it is. | |||
set -g __fish_git_cmd $argv[1] | |||
echo $argv[1] | |||
return 1 | |||
end | |||
set -g __fish_git_cmd | |||
return 0 | |||
end | |||
function __fish_git_using_command | |||
set -l cmd (__fish_git_needs_command) | |||
test -z "$cmd" | |||
and return 1 | |||
contains -- $cmd $argv | |||
and return 0 | |||
# Check aliases. | |||
set -l varname __fish_git_alias_(string escape --style=var -- $cmd) | |||
set -q $varname | |||
and contains -- $$varname[1][1] $argv | |||
and return 0 | |||
return 1 | |||
end | |||
# 1. git コマンドの補完を定義 | |||
complete -f -c git -n __fish_use_subcommand -a clone -d "Clone a repository into a new directory" | |||
# 2. git clone サブコマンドの補完を定義 | |||
complete -f -c git -n '__fish_git_using_command clone' | |||
# 3. git clone のオプション補完を定義 | |||
complete -f -c git -n '__fish_git_using_command clone' -l recursive -d "Initialize submodules in the clone" | |||
complete -f -c git -n '__fish_git_using_command clone' -s q -l quiet -d "Operate quietly" | |||
complete -f -c git -n '__fish_git_using_command clone' -s v -l verbose -d "Run verbosely" | |||
complete -f -c git -n '__fish_git_using_command clone' -l progress -d "Force progress reporting" | |||
complete -f -c git -n '__fish_git_using_command clone' -s n -l no-checkout -d "No checkout of HEAD is performed after clone is complete" | |||
complete -f -c git -n '__fish_git_using_command clone' -l bare -d "Make a bare Git repository" | |||
complete -f -c git -n '__fish_git_using_command clone' -l mirror -d "Set up a mirror of the source repository" | |||
complete -f -c git -n '__fish_git_using_command clone' -s o -l origin -x -d "Use <name> instead of 'origin' to track upstream" | |||
complete -f -c git -n '__fish_git_using_command clone' -s b -l branch -x -d "Checkout <branch> instead of the remote's HEAD" | |||
complete -f -c git -n '__fish_git_using_command clone' -l depth -x -d "Create a shallow clone of that depth" | |||
</syntaxhighlight> | |||
<br><br> | |||
__FORCETOC__ | |||
[[カテゴリ:シェルスクリプト]] | |||
== 組み込み関数 == | == 組み込み関数 == | ||