# shellcheck shell=dash disable=SC2016

___x_cmd_sysctl_run(){
    local format=auto
    while [ $# -gt 0 ]; do
        case "$1" in
            --app)          format=app  ;;
            --tsv|-t)       format=tsv  ;;
            --csv|-c)       format=csv  ;;
            --raw|-r)       format=raw  ;;
            --auto)         format=auto ;;
            *)              break ;;
        esac
        shift
    done

    ___x_cmd_sysctl_run___"$format" "$@"
}

___x_cmd_sysctl_run___auto(){
    if ___x_cmd_is_stdout2tty; then     ___x_cmd_sysctl_run___app    "$@"
    else                                ___x_cmd_sysctl_run___tsv    "$@"
    fi
}

___x_cmd_sysctl_run___tsv(){

    ___x_cmd_sysctl_run___raw "$@" | \
        ___x_cmd_cmds awk -v format="tsv" -f "$___X_CMD_ROOT_MOD/sysctl/lib/awk/parse.awk"
}

___x_cmd_sysctl_run___csv(){
    ___x_cmd_sysctl_run___tsv "$@" | ___x_cmd tsv tocsv
}

___x_cmd_sysctl_run___app(){
    ___x_cmd_sysctl_run___csv "$@" | ___x_cmd csv app --clear
}

___x_cmd_sysctl_run___raw(){
    if ___x_cmd_hascmd sysctl; then
        ___x_cmd_cmds sysctl -a "$@"
    fi
}
