接受多个参数的 Bash CLI

标签 bash shell unix while-loop command-line-interface

我正在尝试创建自定义 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 时,它显示未知参数已传递:-ssl。我哪里做错了?

另一个问题:

foo --create -domain hello.com 替换 ./cli.sh --create -domain hello.com 的最佳方法是什么,这样我就可以从终端中的任何位置使用 CLI。

最佳答案

如何使用 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”。

关于接受多个参数的 Bash CLI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70829350/

相关文章:

linux - 从变量执行linux命令

linux - 时间源 somescript.sh : time not displayed

java - 无法使用 Java 运行 bash shell

mysql - 安装 MySQL Workbench 但 File/etc/my.cnf 不存在

c - C/Unix 中的 Socketpair()

linux - Bash 读取用户 y/n 答案不起作用(在循环读取查找输出时读取内部命令)

mysql - 精确时间之间的查询

linux - AWK 脚本自动从字符串中删除前导 0

C 适当使用全局变量

Bash shell 读取错误 : 0: Resource temporarily unavailable