From 9f102978dc4ced7f1e846aa793a4b146c447d082 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Mon, 3 Apr 2023 13:16:15 -0300 Subject: bin/uri: Allow string argument to be given instead of STDIN BONUS: Make reading from STDIN streaming instead of all at once. --- bin/uri | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'bin/uri') 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(); -} else { - print URI::Escape::uri_escape(); +my $fn = $opts{d} ? \&URI::Escape::uri_unescape : \&URI::Escape::uri_escape; + +for (@ARGV ? @ARGV : ) { + say &$fn($_); } -- cgit v1.2.3