123 lines
3.9 KiB
Bash
Executable File
123 lines
3.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
MAIN_DIR=$(realpath -e "$(dirname "$0")")
|
|
# shellcheck disable=1091
|
|
source "$MAIN_DIR/vendor/std/src/lib.sh"
|
|
source "$MAIN_DIR/src/lib.sh"
|
|
|
|
declare -A clap__flags=(
|
|
[-h]=bool
|
|
[-v]=value
|
|
["--long-flag"]=value
|
|
["--long-bool-flag"]=bool
|
|
)
|
|
|
|
# formatter doesn't like these unquoted and zsh doesn't like quotes on
|
|
# associative arrays
|
|
declare __long_flag=--long-flag
|
|
declare __long_bool_flag=--long-bool-flag
|
|
|
|
eval "$(std::sensible)"
|
|
|
|
clap::parse a b c
|
|
std::assert-eq "should have parsed 3 args" 3 "${#clap__arguments[@]}"
|
|
|
|
clap::parse a b -h c
|
|
std::assert-eq "should have parsed 3 args" 3 "${#clap__arguments[@]}"
|
|
|
|
clap::parse -h
|
|
std::assert-eq "should have parsed 1 flag" 1 "${#clap__options[@]}"
|
|
std::assert-eq "should have parsed 0 args" 0 "${#clap__arguments[@]}"
|
|
|
|
clap::parse -- -h
|
|
std::assert-eq "should have parsed 0 flags" 0 "${#clap__options[@]}"
|
|
std::assert-eq "should have parsed 1 arg" 1 "${#clap__arguments[@]}"
|
|
|
|
clap::parse -- --
|
|
std::assert-eq "should have parsed 0 flags" 0 "${#clap__options[@]}"
|
|
std::assert-eq "should have parsed 1 arg" 1 "${#clap__arguments[@]}"
|
|
std::assert "1st arg should be '--'" "${clap__arguments[0]}" == "--"
|
|
|
|
# shellcheck disable=2251
|
|
! clap::parse --long-flagarg &>/dev/null
|
|
# shellcheck disable=2251
|
|
! clap::parse --long-flagh &>/dev/null
|
|
|
|
clap::parse --long-flag arg
|
|
std::assert-eq "should have parsed 0 args" 0 "${#clap__arguments[@]}"
|
|
std::assert "'--long-flag' should be set" "${clap__options[$__long_flag]}" == arg
|
|
|
|
clap::parse --long-bool-flag arg
|
|
std::assert-eq "should have parsed 1 args" 1 "${#clap__arguments[@]}"
|
|
std::assert-eq "'--long-bool-flag' should be set" 1 "${clap__options[$__long_bool_flag]}"
|
|
|
|
# --- Subcommands
|
|
|
|
CLAP__PARSE_IS_SUBCOMMAND=1 clap::parse --long-flag arg subcommand --subc-arg
|
|
std::assert-eq "should have parsed 1 subcommand" 1 "${#clap__arguments[@]}"
|
|
std::assert "subcommand should be 'subcommand'" subcommand == "${clap__arguments[0]}"
|
|
std::assert "'--long-flag' should be 'arg'" arg == "${clap__options[$__long_flag]}"
|
|
std::assert-eq "should have 1 subcommand argument" 1 "${#clap__subcommand_arguments[@]}"
|
|
std::assert "subcommand argument should be '--subc-arg'" "--subc-arg" == "${clap__subcommand_arguments[0]}"
|
|
|
|
# --- Help Menus
|
|
|
|
declare -a clap__help_commands=()
|
|
declare -a clap__help_options=()
|
|
|
|
# --- OUTPUT 1
|
|
std::assert "reviewed clap output 1 must match" "$(
|
|
clap::pretty-help "This help is empty" | xxd -p
|
|
)" == "$(xxd -p <test/clap1.ansi)"
|
|
|
|
# --- OUTPUT 2
|
|
clap__help_commands=(
|
|
"subcommand1 Description 1."
|
|
"subcommand2-longer Description 2."
|
|
"empty-subcommand"
|
|
)
|
|
clap__help_options=(
|
|
"-s"
|
|
"-h --help Shows help."
|
|
"--just-long"
|
|
"--description But with description."
|
|
)
|
|
std::assert "reviewed clap output 2 must match" "$(
|
|
clap::pretty-help "This is the description" "hard-subcommand" "<OPTION>" "[ARGUMENTS...]" | xxd -p
|
|
)" == "$(xxd -p <test/clap2.ansi)"
|
|
|
|
# --- OUTPUT 3
|
|
std::assert "reviewed clap output 3 must match" "$(
|
|
CLAP_HL_SECTION=$'\033[4;1;38;2;236;134;150m' \
|
|
CLAP_HL_WORD=$'\033[33m' \
|
|
CLAP_HL_OPTION=$'\033[34m' \
|
|
clap::pretty-help "This is the colored description" "hard-subcommand" "<OPTION>" "[ARGUMENTS...]" | xxd -p
|
|
)" == "$(xxd -p <test/clap3.ansi)"
|
|
|
|
# --- OUTPUT 4
|
|
clap__help_options=(
|
|
"-s"
|
|
"-h Shows help."
|
|
)
|
|
std::assert "reviewed clap output 4 must match" "$(
|
|
CLAP_HL_SECTION=$'\033[4;1;38;2;236;134;150m' \
|
|
CLAP_HL_WORD=$'\033[33m' \
|
|
CLAP_HL_OPTION=$'\033[34m' \
|
|
clap::pretty-help "This is the colored description" "hard-subcommand" "<OPTION>" "[ARGUMENTS...]" | xxd -p
|
|
)" == "$(xxd -p <test/clap4.ansi)"
|
|
|
|
# --- OUTPUT 5
|
|
clap__help_options=(
|
|
"--help Shows help."
|
|
"--just-long"
|
|
"--description But with description."
|
|
)
|
|
std::assert "reviewed clap output 5 must match" "$(
|
|
CLAP_HL_SECTION=$'\033[4;1;38;2;236;134;150m' \
|
|
CLAP_HL_WORD=$'\033[33m' \
|
|
CLAP_HL_OPTION=$'\033[34m' \
|
|
clap::pretty-help "This is the colored description" "hard-subcommand" "<OPTION>" "[ARGUMENTS...]" | xxd -p
|
|
)" == "$(xxd -p <test/clap5.ansi)"
|