diff --git a/.gitignore b/.gitignore index df55767..1248c5c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ # Ignore all plugin files in subdirectories -*/ +zsh-*/ local diff --git a/url/_url b/url/_url new file mode 100644 index 0000000..1735c40 --- /dev/null +++ b/url/_url @@ -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 +} diff --git a/url/url b/url/url new file mode 100755 index 0000000..13c24fb --- /dev/null +++ b/url/url @@ -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)