aboutsummaryrefslogtreecommitdiff
path: root/bin/uri
diff options
context:
space:
mode:
Diffstat (limited to 'bin/uri')
-rwxr-xr-xbin/uri75
1 files changed, 75 insertions, 0 deletions
diff --git a/bin/uri b/bin/uri
new file mode 100755
index 0000000..9b7a61b
--- /dev/null
+++ b/bin/uri
@@ -0,0 +1,75 @@
+#!/usr/bin/env perl
+
+use v5.34;
+use warnings;
+use feature 'signatures';
+no warnings ('experimental::signatures');
+use Getopt::Std ();
+use URI::Escape ();
+
+sub usage($fh) {
+ print $fh <<~'EOF'
+ Usage:
+ uri [-e|-d]
+ uri -h
+ EOF
+}
+
+sub help($fh) {
+ print $fh <<~'EOF'
+
+ Options:
+ -e encode the string (the default action)
+ -d decode the string
+ -h, --help show this message
+
+
+ Get a string from STDIN and convert it to/from URL encoding.
+
+
+ Examples:
+
+ Encode the URL:
+
+ $ echo "https://euandre.org/?q=$(printf '%s' 'a param' | uri)"
+ https://euandre.org/?q=a%20param
+
+
+ Decode the content from the file:
+
+ $ uri -d < file
+ EOF
+}
+
+for (@ARGV) {
+ last if $_ eq '--';
+ if ($_ eq '--help') {
+ usage *STDOUT;
+ help *STDOUT;
+ exit;
+ }
+}
+
+my %opts;
+if (!Getopt::Std::getopts('edh', \%opts)) {
+ usage *STDERR;
+ exit 2;
+}
+
+if ($opts{h}) {
+ usage *STDOUT;
+ help *STDOUT;
+ exit;
+} elsif ($opts{e} and $opts{d}) {
+ say STDERR 'Both -e and -d given. Pick one.';
+ say STDERR '';
+ usage *STDERR;
+ exit 2;
+}
+
+
+if ($opts{d}) {
+ print URI::Escape::uri_unescape(<STDIN>);
+} else {
+ print URI::Escape::uri_escape(<STDIN>);
+}