Where I create a function with completion for interacting with a minecraft server.
.0 Preamble: A function
As for bounty:
Please provide a clear and scalable sample code
There is a nice function intented to create an URL for stackoverflow.com
:
soUrl() {
local __so_url='https://stackoverflow.com/'
if [ "$1" = "-v" ]; then
printf -v "$2" '%s%s' "$__so_url" "$3"
else
printf '%s%s\n' "$__so_url" "$1"
fi
}
Usage:
soUrl
https://stackoverflow.com/
soUrl fooBar
https://stackoverflow.com/fooBar
or create a variable:
soUrl -v testvar baz
echo $testvar
https://stackoverflow.com/baz
.1 Collect available options in an array
The list of valid values for --some-id flag is available by making some HTTP...
You have to write something like:
wget -O - https://somewhere.net/somepath?someoption... |
sed -ne 's/^.*someRE\(Re2\).*/\1/p'
Of course, instead of wget
, you could use curl
, nc
or even /dev/tcp
or openssl
(Login to a site (stackoverflow) by bash)
But as you could have to make many tries on command line, use a temporary file in order to avoid stressing web server! Upto you're able to show all your available options.
.1b Then create an array:
soUrlOpts=($(soUrl -v var;wget -qO - $var | sed -ne '/a href="\//{
s/.*a href="\/\([^"\/?]*\)[?\/"].*/\1/;H;};${x;:a;
s/\n\([^\n]\+\)\(\n\(.*\n\|\)\)\1\n/\n\1\2\n/;ta;s/^\n//;s/\n\+/ /g;p;}'))
or
soUrlOpts=($(
soUrl -v var
wget -qO - $var |
sed -ne '
/a href="\//{
s/.*a href="\/\([^"\/?]*\)[?\/"].*/\1/;
H;
};
${
x;
:a;
s/\n\([^\n]\+\)\(\n\(.*\n\|\)\)\1\n/\n\1\2\n/;
ta;
s/^\n//;
s/\n\+/ /g;
p;
}'
))
Ensure your variable is correct:
declare -p soUrlOpts
declare -a soUrlOpts=([0]="company" [1]="teams" [2]="questions" [3]="jobs"
[4]="collectives" [5]="users" [6]="advertising" [7]="talent" [8]="help")
.2 enable completion
complete -W "${soUrlOpts[*]}" soUrl
Then now, if you write: soUrl -v testvar q
, then hit Tab, console must write uestion
:
soUrl -v testvar questions
echo $testvar
https://stackoverflow.com/questions