unix 接受多个参数的Bash CLI

wgeznvg7  于 12个月前  发布在  Unix
关注(0)|答案(2)|浏览(130)

我试图创建一个自定义CLI &我遇到了一个问题。
这是我的代码。

#!/bin/bash

# Function definations.
__help_menu() {

      echo -e ""
      echo -e "======================="
      echo -e "THESE ARE THE AVAILABLE COMMANDS:"
      echo -e ""
      echo -e "⟹   ./cli.sh --create -domain example.com"
      echo -e "⟹   ./cli.sh --create -domain example.com -ssl <no|yes>"
      echo -e "⟹   ./cli.sh --create -domain example.com -ssl <yes|no> -wp <yes|no>"
      echo -e "⟹   ./cli.sh --delete -domain example.com"
      echo -e "⟹   ./cli.sh --delete -domain example.com -ssl <yes|no>"
      echo -e "======================="
      echo -e ""
}

__create() {

    # Do something. I got this.
    echo -e "I got this"
}

__delete() {

    # Do something. I got this.
    echo -e "I got this"
}

__cli() {

    # Run while loop.
    while [[ "$#" -gt 0 ]]; do
        case $1 in
            --create)
                shift
                case $1 in
                    -domain) 
                        DOMAIN_NAME="$2"; 
                        shift 
                        ;;
                    -ssl)
                        INSTALL_SSL="$2"; 
                        shift 
                        ;;
                    -wp|--wp) 
                        INSTALL_WP="$2"; 
                        shift 
                        ;;
                    *)
                    echo -e "Unknown parameter passed: $1";
                    __help_menu
                    exit
                    ;;
                esac
                ;;
            --delete)
                shift
                case $1 in
                    -domain) 
                        DOMAIN_NAME="$2"; 
                        shift 
                        ;;
                    -ssl)
                        DELETE_SSL="$2"; 
                        shift 
                        ;;
                    *)
                    echo -e "Unknown parameter passed: $1";
                    __help_menu
                    exit
                    ;;
                esac
                ;;
            --help) __help_menu; exit ;;
        *) echo -e "Unknown parameter passed: $1"; exit 1 ;;
        
        esac
        shift
    done
}

__cli "$@"

if [ "$1" == "--create" ]; then

    echo -e "Command is to create a new site."
    echo -e "Domain name: $DOMAIN_NAME"
    echo -e "Install SSL: $INSTALL_SSL"
    echo -e "Install WP: $INSTALL_WP"
    echo -e ""
fi

字符串
当我运行./cli.sh --create -domain example.com时,它工作正常。但是当我运行./cli.sh --create -domain example.com -ssl yes时,它说Unknown parameter passed:-ssl。我在哪里做错了?
另一个问题:
foo --create -domain hello.com替换./cli.sh --create -domain hello.com的最佳方法是什么,这样我就可以在终端的任何地方使用CLI。

zbwhf8kr

zbwhf8kr1#

如何使用GNU getopt简化命令行解析的示例:

#! /bin/bash

options=$(getopt -q -o '' -l domain:,ssl:,wp: -- "$@") || {
  printf 'ERROR: Invalid argument\n' >&2
  exit 1
}
eval set -- "$options"

while true; do
  case "$1" in
    --domain) DOMAIN="$2"; shift 2;;
    --ssl)    SSL="$2";    shift 2;;
    --wp)     WP="$2";     shift 2;;
    --) shift; break;;
    *)  break;;
  esac
done

COMMAND=$1; shift

case ${COMMAND:?missing} in
  create|delete)
    echo "$COMMAND" "${DOMAIN:?missing}" "${SSL:-yes}" "${WP:-yes}";;
  *)
    printf 'COMMAND: invalid\n' >&2; exit 1;;
esac

字符串
使用方法:

./cli.sh create --domain http://example.com


--ssl--wp的默认值为“yes”。

ddarikpa

ddarikpa2#

@ceving
我想我得到了我想要的东西。我将进一步研究getopt命令。我做了一个简单的调整。它可以工作。

#!/bin/bash

__help_menu() {

      echo -e ""
      echo -e "======================="
      echo -e "THESE ARE THE AVAILABLE COMMANDS:"
      echo -e ""
      echo -e "⟹   ./cli.sh create --domain example.com"
      echo -e "⟹   ./cli.sh create --domain example.com --ssl <no|yes>"
      echo -e "⟹   ./cli.sh create --domain example.com --ssl <yes|no> --wp <yes|no>"
      echo -e "⟹   ./cli.sh delete --domain example.com"
      echo -e "⟹   ./cli.sh delete --domain example.com --ssl <yes|no>"
      echo -e "======================="
      echo -e ""
}

__handle_create () {

    if [ "$COMMAND" = "create" ]; then

        if [ "$SSL" = "no" ] && [ "$WP" = "no" ]; then
            __create $DOMAIN
        else
           INSTALL_SSL=$SSL
           INSTALL_WP=$WP   
            __create $DOMAIN $INSTALL_SSL $INSTALL_WP
        fi 
    fi
}

__create() {

    echo -e ""
    if [ "$DOMAIN" != "" ]; then
        echo -e "DOMAIN is: $DOMAIN"
    fi
    if [ "$SSL" != "" ]; then
        echo -e "INSTALL SSL: $INSTALL_SSL"
    fi
    if [ "$WP" != "" ]; then
        echo -e "INSTALL WP: $INSTALL_WP"
    fi
    echo -e ""
}

__handle_delete() {

    if [ "$COMMAND" = "delete" ]; then

        if [ "$SSL" = "no" ]; then
            __delete $DOMAIN
        else
            __delete $DOMAIN $SSL
        fi 
    fi
}

__delete() {

    echo -e ""
    echo -e "DOMAIN is: $DOMAIN"
    echo -e "DELETE SSL: $SSL"
    echo -e ""
}

__cli() {

    options=$(getopt -q -o '' -l domain:,ssl:,wp: -- "$@") || {

    echo -e 'ERROR: Invalid argument\n' >&2
    exit 1
    }

    eval set -- "$options"

    while true; do
    case "$1" in
        --domain) DOMAIN="$2"; shift 2;;
        --ssl)    SSL="$2";    shift 2;;
        --wp)     WP="$2";     shift 2;;
        --) shift; break;;
        *)  break;;
    esac
    done

    COMMAND=$1; shift

    case ${COMMAND:?missing} in
        create )
            __handle_create $@
            ;;
        delete )
            handle_delete $@
            ;;
        help )
            __help_menu
            ;;
        *)
        __help_menu
        ;;
    esac
}

__cli "$@"

字符串
顺便说一句,为了用foo create --domain example.com替换./cli.sh create --domain example.com,我应该添加一个指向./cli.sh文件的文件夹,或者有更好的方法吗?

相关问题