From 5e528ed7ef6b5facb7634ceccc3ff75090bc90a3 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Thu, 19 Oct 2023 17:42:00 +0100 Subject: [PATCH] Add url command to {en,de}code text --- .gitignore | 2 +- url/_url | 22 ++++++++++++++++++++++ url/url | 25 +++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 url/_url create mode 100755 url/url 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..b3dbd0f --- /dev/null +++ b/url/url @@ -0,0 +1,25 @@ +#!/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']) + cli.add_argument('text', nargs='?') + 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)