# shellcheck shell=dash

xrc:mod:lib line    \
    iter/wrap iter/join             \
    iter/arg1 iter/args iter/prf    \
    iter/duo iter/uni iter/uno      \
    two/minus

___x_cmd_line___main(){
    [ "$#" -gt 0 ] ||   set -- --help

    local op="$1"; shift
    case "$op" in
        wrap|wrap_|map|eval|\
        join|join_|\
        prf|uni|duo|uno|minus|arg1|args|prln|\
        from|from_|fromcomma|fromcomma_|fromcolon|fromcolon_|fromeq|fromeq_|\
        split|split_|\
        count|count_|\
        indexof|indexof_|contain)
                            ___x_cmd_line_"${op}"       "$@" ;;

        -h|--help)          ___x_cmd help -m line       "$@" ;;
        *)                  ___x_cmd_line_prf "$op"     "$@" ;;
    esac
}


___x_cmd_line_prln(){
    case "$1" in
        -h|--help)      ___x_cmd help -m line   prln "$@"; return ;;
        --)             shift ;;
    esac

    local IFS=' '
    printf "%s\n" "$@"
}

___x_cmd_line_split(){
    [ $# -gt 0 ]        ||  set -- --help
    case "$1" in
        -h|--help)      ___x_cmd help -m line   split "$@"; return ;;
        --)             shift ;;
    esac

    local x_
    ___x_cmd_line_split_ "$@" || return $?
    printf "%s" "$x_"
}

___x_cmd_line_split_(){
    local sep="$1"
    local srcstr="$2"
    x_=""

    while [ -n "$srcstr" ]; do
        case "$srcstr" in
            *"$sep"*)   elem="${srcstr%%"$sep"*}";  srcstr="${srcstr#*"$sep"}" ;;
            *)          elem="$srcstr";              srcstr="" ;;
        esac
        [ -z "$elem" ] || x_="${x_}${elem}
"
    done
}

___x_cmd_line_from(){
    [ $# -gt 0 ]        ||  set -- --help
    case "$1" in
        -h|--help)      ___x_cmd help -m line   from "$@"; return ;;
        --)             shift ;;
    esac

    local x_
    ___x_cmd_line_from_ "$@" || return $?
    printf "%s" "$x_"
}

___x_cmd_line_from_(){
    case "$1" in
        -h|--help)      ___x_cmd help -m line   from_ "$@"; return ;;
        --)             shift ;;
    esac

    local srcstr="$1"
    local elem=""
    x_=""

    while [ -n "$srcstr" ]; do
        case "$srcstr" in
            *:*)    elem="${srcstr%%:*}";   srcstr="${srcstr#*:}" ;;
            *,*)    elem="${srcstr%%,*}";   srcstr="${srcstr#*,}" ;;
            *" "*)  elem="${srcstr%%" "*}"; srcstr="${srcstr#*" "}" ;;
            *)      elem="$srcstr";         srcstr="" ;;
        esac
        [ -z "$elem" ] || x_="${x_}${elem}
"
    done
}

___x_cmd_line_fromcomma(){
    [ $# -gt 0 ]        ||  set -- --help
    case "$1" in
        -h|--help)      ___x_cmd help -m line   fromcomma "$@"; return ;;
        --)             shift ;;
    esac

    local x_
    ___x_cmd_line_fromcomma_ "$@" || return $?
    printf "%s" "$x_"
}

___x_cmd_line_fromcomma_(){
    local srcstr="$1"
    local elem=""
    x_=""

    while [ -n "$srcstr" ]; do
        case "$srcstr" in
            *,*)    elem="${srcstr%%,*}";   srcstr="${srcstr#*,}" ;;
            *)      elem="$srcstr";         srcstr="" ;;
        esac
        [ -z "$elem" ] || x_="${x_}${elem}
"
    done
}

___x_cmd_line_fromcolon(){
    [ $# -gt 0 ]        ||  set -- --help
    case "$1" in
        -h|--help)      ___x_cmd help -m line   fromcolon "$@"; return ;;
        --)             shift ;;
    esac

    local x_
    ___x_cmd_line_fromcolon_ "$@" || return $?
    printf "%s" "$x_"
}

___x_cmd_line_fromcolon_(){
    local srcstr="$1"
    local elem=""
    x_=""

    while [ -n "$srcstr" ]; do
        case "$srcstr" in
            *:*)    elem="${srcstr%%:*}";   srcstr="${srcstr#*:}" ;;
            *)      elem="$srcstr";         srcstr="" ;;
        esac
        [ -z "$elem" ] || x_="${x_}${elem}
"
    done
}

___x_cmd_line_fromeq(){
    [ $# -gt 0 ]        ||  set -- --help
    case "$1" in
        -h|--help)      ___x_cmd help -m line   fromeq "$@"; return ;;
        --)             shift ;;
    esac

    local x_
    ___x_cmd_line_fromeq_ "$@" || return $?
    printf "%s" "$x_"
}

___x_cmd_line_fromeq_(){
    local srcstr="$1"
    local elem=""
    x_=""

    while [ -n "$srcstr" ]; do
        case "$srcstr" in
            *=*)    elem="${srcstr%%=*}";   srcstr="${srcstr#*=}" ;;
            *)      elem="$srcstr";         srcstr="" ;;
        esac
        [ -z "$elem" ] || x_="${x_}${elem}
"
    done
}

___x_cmd_line_count(){
    [ $# -gt 0 ]        ||  set -- --help
    case "$1" in
        -h|--help)      ___x_cmd help -m line count "$@" ; return 0 ;;
        --)             shift ;;
    esac

    local x_
    ___x_cmd_line_count_ "$@" || return $?
    printf "%s\n" "$x_"
}

___x_cmd_line_count_(){
    case "$1" in
        ""|-)
            x_="$(___x_cmd_cmds wc -l)" ;
            x_="${x_##* }"
            ;;
        *)
            if [ $# -eq 1 ]; then
                x_="$(___x_cmd_cmds wc -l "$1" 2>/dev/null)" || {
                    x_=0
                    return 1
                }
            else
                x_="${___X_CMD_UNSEENCHAR_NEWLINE}$(___x_cmd_cmds wc -l "$@" 2>/dev/null)"
                x_="${x_##*"$___X_CMD_UNSEENCHAR_NEWLINE"}"
            fi
            x_="${x_% *}"
            x_="${x_##* }"
            ;;
    esac
}

___x_cmd_line_indexof(){
    [ $# -gt 0 ]        ||  set -- --help
    case "$1" in
        -h|--help)      ___x_cmd help -m line indexof "$@" ; return 0 ;;
        --)             shift ;;
    esac

    local x_=""
    ___x_cmd_line_indexof_ "$@" || return $?
    printf "%s\n" "$x_"
}

___x_cmd_line_indexof_(){
    local mode=awk
    while [ $# -gt 0 ]; do
        case "$1" in
            --awk|--sh)     mode="${1#--}" ; shift ;;
            --)             shift; break ;;
            *)              break ;;
        esac
    done

    local target="$1" ;     [ -n "$target" ]    ||  N=line M="Target is empty string."      log:ret:64
    local fp="${2}" ;
    if [ "$#" -eq 1 ] || [ "$fp" = - ]; then
        fp="/dev/stdin"
    fi

    [ -r "$fp" ]        ||  N=line M="File is not readable -> $fp"  log:ret:64

    if [ "$mode" = awk ]; then
        x_="$(
            export target
            target="$target" ___x_cmd_cmds awk '
                BEGIN{ target=ENVIRON["target"] }
                index($0, target) { print NR; exit }
            ' "$fp"
        )"
        [ -n "$x_" ] || x_=-1
        return 0
    fi

    x_=0
    while read -r line; do
        x_=$((x_+1))
        case "$line" in
            *"$target"*)    return 0 ;;
        esac
    done <"$fp"
    x_=-1
    return 1
}

___x_cmd_line_contain(){
    [ $# -gt 0 ]        ||  set -- --help
    case "$1" in
        -h|--help)      ___x_cmd help -m line contain "$@" ; return 0 ;;
        --)             shift ;;
    esac

    local target="$1"
    local fp="$2"

    [ -n "$target" ]    ||  N=line M="Target is empty string."      log:ret:64
    [ -r "$fp" ]        ||  N=line M="File is not readable -> $fp"  log:ret:64

    ___x_cmd_cmds grep -q -F "$target" "$fp" 2>/dev/null
}

