oh-my-fish/plugins/php/phphttp.fish

88 lines
1.9 KiB
Fish
Raw Normal View History

2013-10-31 19:28:06 +00:00
# PHP HTTP server.
function phphttp
2013-11-20 09:53:39 +00:00
set -l port 8000
set -l path
set -l host 127.0.0.1
2013-10-31 19:28:06 +00:00
2013-11-20 09:53:39 +00:00
# Ignore argument for slice.
set argv $argv ignore
2013-10-31 19:28:06 +00:00
2013-11-20 09:53:39 +00:00
# Process options. I think that fish should have some builtin for
# option parsing, but it doesn't.
while count $argv > /dev/null
set -l option $argv[1]
2013-10-31 19:28:06 +00:00
2013-11-20 09:53:39 +00:00
switch $option
# When two hyphens appear, stop processing, while removing
# hyphens from $argv.
case --
set argv $argv[2..-1]
break
2013-10-31 19:28:06 +00:00
2013-11-20 09:53:39 +00:00
# Public mode.
case -p\* --p --pu --pub --publ --publi --public p public
if test $host = 0
echo phphttp: Duplicate option --public >&2
end
set host 0
2013-10-31 19:28:06 +00:00
2013-11-20 09:53:39 +00:00
# Help.
case -h\* --h --he --hel --help '-\?' h help
echo 'phphttp [--public] <port=8000> <path=.>'
return
2013-10-31 19:28:06 +00:00
2013-11-20 09:53:39 +00:00
# Anything else stops processing.
case \*
break
end
2013-10-31 19:28:06 +00:00
2013-11-20 09:53:39 +00:00
# Check if the option was one letter.
switch $option
case --\*
# Doesn't count as single option
2013-10-31 19:28:06 +00:00
2013-11-20 09:53:39 +00:00
case -\?\?\*
set argv[1] -(expr substr $argv[1] 3 length $argv[1])
continue
2013-10-31 19:28:06 +00:00
end
2013-11-20 09:53:39 +00:00
set argv $argv[2..-1]
end
2013-10-31 19:28:06 +00:00
2013-11-20 09:53:39 +00:00
if test (count $argv[1..-1]) -ge 4
echo 'phphttp: Expected up to two arguments, got '(math (count $argv) - 1)'.' >&2
return
end
2013-10-31 19:28:06 +00:00
2013-11-20 09:53:39 +00:00
# argv is bigger by 1 because of "ignore" argument.
if test (count $argv) -ge 2
# Check legality of first argument
switch $argv[1]
# Fine values
case {0,1,2,3,4,5,6,7,8,9}\*
# Do nothing
2013-10-31 19:28:06 +00:00
2013-11-20 09:53:39 +00:00
case \*
# The dev team thinks of everything. Or something.
if test -d $argv[1]
if test (count $argv) -eq 2
echo "phphttp: directory specified without port." >&2
else
echo "phphttp: swapped directory and port arguments." >&2
end
return
else
echo "phphttp: $argv[1] is not a port." >&2
return
end
2013-10-31 19:28:06 +00:00
end
2013-11-20 09:53:39 +00:00
set port $argv[1]
end
if test (count $argv) -eq 3
set path -t$argv[2]
end
php -S$host:$port $path
2013-10-31 19:28:06 +00:00
end