use
5.010;
option
'arglist'
=> (
is
=>
'ro'
,
format
=>
's'
,
required
=> 1,
doc
=>
'the original casperjs arguments'
);
option
'output'
=> (
is
=>
'ro'
,
format
=>
's'
,
predicate
=>
'is_output_set'
);
has
'options'
=> (
is
=>
'ro'
,
default
=>
sub
{ {} });
has
'subcommand'
=> (
is
=>
'ro'
,
writer
=>
'set_subcommand'
,
predicate
=>
'subcommand_is_set'
);
has
'paths'
=> (
is
=>
'ro'
,
default
=>
sub
{ [] });
sub
truthify_maybe {
$_
[0] eq
'false'
? JSON::false :
$_
[0] eq
'true'
? JSON::true :
$_
[0];
}
sub
arrayrefify_maybe {
my
@values
=
split
(/,/,
$_
[0]);
if
(
@values
> 1) {
return
[
map
{ truthify_maybe(
$_
) }
@values
];
}
return
truthify_maybe(
$values
[0]);
}
sub
repsac_nur {
my
$self
=
shift
;
foreach
my
$argument
(
split
(/\s+/,
$self
->arglist)) {
if
(
$argument
=~ m/^--(?<option>[^=]+)(?:=(?<value>.+))?/) {
my
(
$option
,
$value
) = ($+{option}, $+{value});
$self
->options->{
$option
} =
defined
$value
? arrayrefify_maybe(
$value
) : JSON::true;
}
else
{
if
(
$self
->subcommand_is_set) {
push
@{
$self
->paths},
$argument
;
}
else
{
$self
->set_subcommand(
$argument
);
}
}
}
my
$json
= JSON->new->pretty->encode({ %{
$self
->options},
paths
=>
$self
->paths });
if
(
$self
->is_output_set) {
open
my
$fh
,
'>'
,
$self
->output
or croak
sprintf
(
q{Cannot open '%s' for writing: %s}
,
$self
->output, $!);
$fh
->
print
(
$json
);
$fh
->
close
;
}
else
{
print
$json
;
}
}
1;