任意の文字列を使用してコマンドライン引数を解析する方法

任意の文字列を使用してコマンドライン引数を解析する方法

引用符で囲まれた任意のテキスト(スペースを含む)を含むオプションを持つスクリプトを作成しようとしていますが、検索して実装するのは困難です。

基本的に私が望む動作は、docker_build_image.sh -i "image" -v 2.0 --options "--build-arg ARG=value"これがビルドサーバーを使用してDockerイメージのバージョン管理を簡素化するヘルパースクリプトになることです。

値の取得に最も近いのは、--optionsgetoptで「認識できないオプション」--build-arg ARG = valueというエラーが発生することです。

完全なスクリプトは次のとおりです。

#!/usr/bin/env bash
set -o errexit -o noclobber -o nounset -o pipefail
params="$(getopt -o hi:v: -l help,image:,options,output,version: --name "$0" -- "$@")"
eval set -- "$params"

show_help() {
cat << EOF
Usage: ${0##*/} [-i IMAGE] [-v VERSION] [OPTIONS...]

Builds the docker image with the Dockerfile located in the current directory.

    -i, --image         Required. Set the name of the image.
    --options           Set the additional options to pass to the build command.
    --output            (Default: stdout) Set the output file.
    -v, --version       Required. Tag the image with the version.
    -h, --help          Display this help and exit.
EOF
}

while [[ $# -gt 0 ]]
do
    case $1 in
        -h|-\?|--help)
            show_help
            exit 0
            ;;
        -i|--image)
            if [ -n "$2" ]; then
                IMAGE=$2
                shift
            else
                echo -e "ERROR: '$1' requires an argument.\n" >&2
                exit 1
            fi
            ;;        
        -v|--version)            
            if [ -n "$2" ]; then
                VERSION=$2
                shift
            else
                echo -e "ERROR: '$1' requires an argument.\n" >&2
                exit 1
            fi
            ;;
        --options)
        echo -e "OPTIONS=$2\n"
            OPTIONS=$2
        ;;
        --output)            
            if [ -n "$2" ]; then
                BUILD_OUTPUT=$2
                shift
            else
                BUILD_OUTPUT=/dev/stderr
            fi
        ;;
        --)
            shift
            break
        ;;
        *)
            echo -e "Error: $0 invalid option '$1'\nTry '$0 --help' for more information.\n" >&2
            exit 1
        ;;
    esac
shift
done

echo "IMAGE: $IMAGE"
echo "VERSION: $VERSION"
echo ""

# Grab the SHA-1 from the docker build output
ID=$(docker build ${OPTIONS} -t ${IMAGE}  . | tee $BUILD_OUTPUT | tail -1 | sed 's/.*Successfully built \(.*\)$/\1/')

# Tag our image
docker tag ${ID} ${IMAGE}:${VERSION}
docker tag ${ID} ${IMAGE}:latest

ベストアンサー1

「改善された」getopt(util-linuxまたはBusyboxで)を使用しているように見える場合は、引数(および)を使用して他の操作とoptions同様に進んでください。つまり、getoptに移動するオプション文字列に必要なパラメータを表示するコロンを追加してから、値を選択します。imageversion$2

私はあなたが受け取っているエラーが引数が必要だと言われていgetoptなかったので、から来たと思いますoptions。だからそれを長いオプションとして解釈しようとしています--build-arg ARG=value(二重ダッシュで始まります)。

$ cat opt.sh
#!/bin/bash
getopt -T
if [ "$?" -ne 4 ]; then
    echo "wrong version of 'getopt' installed, exiting..." >&2
    exit 1
fi 
params="$(getopt -o hv: -l help,options:,version: --name "$0" -- "$@")"
eval set -- "$params"

while [[ $# -gt 0 ]] ; do
    case $1 in
        -h|-\?|--help)
            echo "help"
            ;;
        -v|--version)            
            if [ -n "$2" ]; then
                echo "version: <$2>"
                shift
            fi
            ;;
        --options)            
            if [ -n "$2" ]; then
                echo "options: <$2>"
                shift
            fi
            ;;
    esac
    shift
done

$ bash opt.sh --version 123 --options blah --options "foo bar"
version: <123>
options: <blah>
options: <foo bar>

おすすめ記事