aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2023-04-03 13:16:15 -0300
committerEuAndreh <eu@euandre.org>2023-04-03 13:25:07 -0300
commit9f102978dc4ced7f1e846aa793a4b146c447d082 (patch)
treeec04e22aae9f481279cc580cb84261d9126428f4
parentopt/bin-dirs/euandre.org: Remove symlink, link pb(1) directly (diff)
downloaddotfiles-9f102978dc4ced7f1e846aa793a4b146c447d082.tar.gz
dotfiles-9f102978dc4ced7f1e846aa793a4b146c447d082.tar.xz
bin/uri: Allow string argument to be given instead of STDIN
BONUS: Make reading from STDIN streaming instead of all at once.
-rwxr-xr-xbin/uri17
1 files changed, 10 insertions, 7 deletions
diff --git a/bin/uri b/bin/uri
index 9b7a61b..ac3d8b8 100755
--- a/bin/uri
+++ b/bin/uri
@@ -10,7 +10,7 @@ use URI::Escape ();
sub usage($fh) {
print $fh <<~'EOF'
Usage:
- uri [-e|-d]
+ uri [-e|-d] [CONTENT...]
uri -h
EOF
}
@@ -23,15 +23,18 @@ sub help($fh) {
-d decode the string
-h, --help show this message
+ CONTENT a literal string to be escaped
- Get a string from STDIN and convert it to/from URL encoding.
+
+ Convert data to/from URL encoding. If CONTENT is not given, get
+ data from STDIN.
Examples:
Encode the URL:
- $ echo "https://euandre.org/?q=$(printf '%s' 'a param' | uri)"
+ $ echo "https://euandre.org/?q=$(uri 'a param')"
https://euandre.org/?q=a%20param
@@ -68,8 +71,8 @@ if ($opts{h}) {
}
-if ($opts{d}) {
- print URI::Escape::uri_unescape(<STDIN>);
-} else {
- print URI::Escape::uri_escape(<STDIN>);
+my $fn = $opts{d} ? \&URI::Escape::uri_unescape : \&URI::Escape::uri_escape;
+
+for (@ARGV ? @ARGV : <STDIN>) {
+ say &$fn($_);
}