在 getopts

问题描述

我有一个使用 getopts 解析命令行参数的 bash 脚本。其中一个参数 -l <name> 指向用于确定某些设置的 if 语句。是否可以在命令行中自动完成输入 <name> 参数?

这是我的脚本的命令行解析部分 (getopts):

while getopts 'l:r:m:?h' c
do
  case $c in
    l) 
        library=$OPTARG 
        ;;
    r)  
        rename_config=$OPTARG 
        ;;
    m)  
        align_mm=$OPTARG
        ;;  
    h|?) usage 
        ;;
  esac
done

库选项 (-l) 指的是脚本的这一部分:

if [ $library = "bassik" ];
    then
        read_mod="clip"
        clip_seq="GTTTAAGAGCTAAGCTGGAAACAGCATAGCAA"
        echo "Bassik library selected"
elif [ $library = "moffat_tko1" ];
    then
        read_mod="trim"
        sg_length=20    
        echo "Moffat TKO1 library selected"
elif [ $library = "sabatini" ];
    then
        read_mod="trim"
        sg_length=20    
        echo "Sabatini library selected"
fi

自动完成应该起作用的部分是“bassik”、“moffat_tko1”和“sabatini”参数。 到目前为止,我已经尝试在 <TAB> 之后立即点击 ./script.sh -l,但这不起作用。我已经用谷歌搜索了它,但找不到任何适合我的情况(也不知道如何称呼它,bash 新手)。

解决方法

首先,我将您的脚本片段复制到一个名为 auto.sh 的文件中,并为其设置了执行权限:

#!/bin/bash

while getopts 'l:r:m:?h' c
do
  case $c in
    l) 
        library=$OPTARG 
        ;;
    r)  
        rename_config=$OPTARG 
        ;;
    m)  
        align_mm=$OPTARG
        ;;  
    h|?) usage 
        ;;
  esac
done


if [ $library = "bassik" ];
    then
        read_mod="clip"
        clip_seq="GTTTAAGAGCTAAGCTGGAAACAGCATAGCAA"
        echo "Bassik library selected"
elif [ $library = "moffat_tko1" ];
    then
        read_mod="trim"
        sg_length=20    
        echo "Moffat TKO1 library selected"
elif [ $library = "sabatini" ];
    then
        read_mod="trim"
        sg_length=20    
        echo "Sabatini library selected"
fi

然后,要为 -l 选项设置自动完成功能,您可以从这些基本步骤开始(这可以在未来增强):

1. 创建一个完成脚本(例如 ./auto-complete.sh),其中包含要在完成请求时调用的 libs 函数(-F complete 命令的参数)。如果 -l 选项是完成位置之前的单词($3 参数),该函数会触发库名称的显示(COMPREPLY 数组变量的内容):

function libs()
{
  # $1 is the name of the command 
  # $2 is the word being completed
  # $3 is the word preceding the word being completed

  case $3 in
    -l) COMPREPLY+=("bassi")
        COMPREPLY+=("moffat_tko1")
        COMPREPLY+=("sabatini");;
  esac
}

complete -F libs auto.sh

2. 在本地 shell 中获取脚本:

$ source ./auto-complete.sh

3. 启动 shell 脚本并在 TAB 选项后面的空格后键入两次 -l 键:

$ ./auto.sh -l <tab><tab>
bassik       moffat_tko1  sabatini
$ ./auto.sh  -l bassik
Bassik library selected

4. 前面系统地列出了您输入TAB键时的所有选择。为了在键入首字母时更准确地完成,可以增强完成脚本以使用 compgen 命令:

function libs()
{
  # $1 is the name of the command 
  # $2 is the word being completed
  # $3 is the word preceding the word being completed

  case $3 in
    -l) COMPREPLY=($(compgen -W "bassik moffat_tko1 sabatini" "${COMP_WORDS[$COMP_CWORD]}"));;
  esac
}

complete -F libs auto.sh