summaryrefslogtreecommitdiff
path: root/uri
blob: 9f77bbf6e82a3ec4711b92f775b04a0e573adc42 (plain)
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
#!/usr/bin/env perl

use warnings;
use strict;
use URI;
use URI::QueryParam;
use feature 'say';

die "Usage: $0 COMMAND [URIs]" unless @ARGV >= 1;

my $command = shift;

sub make_uri {
    my $uri = shift;
    my @opts = map { s/^--//; [split /=/, $_, 2] } @ARGV;
    for (@opts) {
        my ($k, $v) = @$_;
        if ($k =~ /^query-param-(.+)$/) {
            $uri->query_param($1 => $v);
        } else {
            $uri->$k($v);
        }
    }
    say $uri->as_string;
}

sub take_it {
    chomp;
    my $uri = URI->new($_);
    die "Invalid command for $_: $command" unless $uri->can($command);
    my $val = $uri->$command;

    if (defined $val) {
        exit !$val if $val =~ /^[01]$/;
        say $val;
    } else {
        say "undefined";
        exit 1;
    }
}

if ($command eq 'create') {
    make_uri(URI->new);
} elsif ($command eq 'change') {
    make_uri(URI->new($_)) while <STDIN>;
} else {
    if (@ARGV) {
        take_it for @ARGV;
    } else {
        take_it while <STDIN>;
    }
}