Add url command to {en,de}code text
This commit is contained in:
parent
5c62ff219c
commit
4b0425b2d4
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,3 @@
|
|||||||
# Ignore all plugin files in subdirectories
|
# Ignore all plugin files in subdirectories
|
||||||
*/
|
zsh-*/
|
||||||
local
|
local
|
||||||
|
22
url/_url
Normal file
22
url/_url
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#compdef url
|
||||||
|
|
||||||
|
_url() {
|
||||||
|
local ret=1 context curcontext="$curcontext" state line
|
||||||
|
typeset -A opt_args
|
||||||
|
|
||||||
|
_arguments -C -w -s \
|
||||||
|
'(-h --help)'{-h,--help}'[show this help message and exit]' \
|
||||||
|
'1: :->command'
|
||||||
|
|
||||||
|
case $state in
|
||||||
|
(command)
|
||||||
|
declare -a commands
|
||||||
|
local commands=(
|
||||||
|
encode:'encode unencoded text'
|
||||||
|
decode:'decode encoded text'
|
||||||
|
)
|
||||||
|
_describe -t commands command commands && ret=0 ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
27
url/url
Executable file
27
url/url
Executable file
@ -0,0 +1,27 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
"""URL encode or decode text."""
|
||||||
|
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
from urllib import parse
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
cli = ArgumentParser(description=__doc__)
|
||||||
|
cli.add_argument('command',
|
||||||
|
choices=['encode', 'decode'],
|
||||||
|
help='type of processing to perform on text')
|
||||||
|
cli.add_argument('text',
|
||||||
|
nargs='?',
|
||||||
|
help='optional text read from stdin when omitted')
|
||||||
|
args = cli.parse_args()
|
||||||
|
print({
|
||||||
|
'encode': parse.quote_plus,
|
||||||
|
'decode': parse.unquote_plus,
|
||||||
|
}[args.command](args.text if args.text else input()))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
try:
|
||||||
|
main()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
exit(130)
|
Loading…
x
Reference in New Issue
Block a user