aboutsummaryrefslogtreecommitdiff
path: root/bin/uri
blob: 5930c399688c083863a01c424b70f4054c2a1565 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env perl

use v5.34;
use strict;
use warnings;
use feature 'signatures';
no warnings 'experimental';
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' | uri


		  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;
Getopt::Std::getopts('edh', \%opts);

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;
} elsif ($opts{d}) {
	print URI::Escape::uri_unescape(<STDIN>);
} else {
	print URI::Escape::uri_escape(<STDIN>);
}