add kvpairs2td
[hband-tools.git] / user-tools / replcmd
blobb551247eeca1a9aeed324996205345ae9769da05
1 #!/bin/bash
3 true <<'EOF'
4 =pod
6 =head1 NAME
8 replcmd - Wrap any command in a REPL interface
10 =head1 SYNOPSIS
12 replcmd I<COMMAND> [I<ARGS>]
14 =head1 DESCRIPTION
16 Run I<COMMAND> repeatedly with words read from STDIN appended to its argument list after I<ARGS>.
17 You may add prompt, history, and other CLI-goodies on top of replcmd(1) by eg. rlwrap(1).
19 =head1 RUNTIME COMMANDS
21 =over 4
23 =item I<WORDS>
25 Run I<COMMAND> I<ARGS> I<WORDS>.
26 I<WORDS> get split on C<$IFS>.
28 =item # [I<PARAM-1> [I<PARAM-2> [...]]]
30 Prefix the line with a C<#> hash mark to
31 set fixed parameters for I<COMMAND>.
32 These will be inserted between I<ARGS> and I<WORDS> read form STDIN.
34 =back
36 =head1 EXAMPLE
38 rlwrap --remember --command-name dict --substitute-prompt "dict> " replcmd dict
40 =cut
42 EOF
45 if [ $# -le 0 ]
46 then
47 pod2text "$0" >&2
48 exit 2
51 fixed_params=()
53 while read -a words
55 line=${words[*]}
56 if [[ $line =~ ^#\s*(.+) ]]
57 then
58 fixed_params=(${BASH_REMATCH[1]})
59 continue
62 if [ ${#words[@]} != 0 ]
63 then
64 "$@" "${fixed_params[@]}" "${words[@]}"
66 done