# Author:       Li Junhao           l@x-cmd.com
# shellcheck    shell=sh            disable=SC3043,SC2164     # xrc

# license:      GPLv3

xrc:mod:lib     arg     code helper

___x_cmd_arg(){
    [ "$#" -gt 0 ] || {
        x help -m arg >&2
        return 64
    }

    local op="$1";
    case "$op" in
        shift)          ___x_cmd_arg_shift "$@" ;;
        unshift)        ___x_cmd_arg_unshift "$@" ;;
        push)           ___x_cmd_arg_push "$@" ;;
        pop)            ___x_cmd_arg_pop "$@" ;;
        has)            ___x_cmd_arg_has "$@" ;;
        print)          ___x_cmd_arg_print "$@" ;;

        code)           ___x_cmd_arg_code "$@" ;;
        -h|--help)      x help -m arg ;;
        *)              x help -m arg >&2; return 64 ;;
    esac
}


alias arglist="___x_cmd_arg"

___x_cmd_arg_all(){
    local a="$#"
    local s=""
    while [ "$a" -gt 0 ]; do
        s="$s \"\$$a\""
        a=$((a-1))
    done

    printf "%s" "$s"
}

___x_cmd_arg_push(){
    printf "%s" "set -- \"\$@\" "
}

# set -- "$abc" "$@"

___x_cmd_arg_shift(){
    printf "%s" "[ \"\$#\" -ne 0 ] && shift"
}

___x_cmd_arg_print(){
    local IFS="${1:?Provide IFS}"; shift
    printf "%s\n" "$*"
}

xrc setmain ___x_cmd_arg
