Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Create json from command line args

by teamster_jr (Curate)
on May 20, 2015 at 14:39 UTC ( [id://1127248]=CUFP: print w/replies, xml ) Need Help??

Not hilariously useful, but quite fun to write ntl.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; @ARGV = qw{ --keyed_array_from_multiple_arguments a b --keyed_array_from_multiple_arguments c --keyed_array_from_multiple_arguments 1 --keyed_with_equals=1 --switch --multi.level a --funny_numbers -99.999 --switchable_switch --noswitchable_switch } unless @ARGV; use JSON::XS; use Scalar::Util qw{looks_like_number}; my ( $arguments, $data ); $arguments = join $", @ARGV; $arguments =~ s{ \s* # some space --([\.\w]+) # an argument \s*?=?\s* # some space or = (.*?) # some values \s*(?=--|$) # followed by another argument or end of line }{ my ($opt_name, $values)=($1,$2); for my $opt_value ( $values ? ( split /\s+/, $values ) : $opt_name +=~s/^no// ? JSON::XS::false : JSON::XS::true ) { my $pointer = \$data; for my $opt_key ( split /\./, $opt_name ) { $pointer = \$$poin +ter->{$opt_key} }; $opt_value = ( ref($opt_value) || !looks_like_number($opt_valu +e) ) ? $opt_value : 0 + $opt_value; $$pointer = $$pointer && ! (ref($opt_value) =~/Bool/) ? [ ( re +f($$pointer) eq 'ARRAY' ? @$$pointer : $$pointer ), $opt_value ] : $o +pt_value; } }xeg; print JSON::XS->new->pretty->encode($data)

Replies are listed 'Best First'.
Re: Create json from command line args
by afoken (Chancellor) on May 21, 2015 at 07:21 UTC

    I miss a few lines of documentation and an example.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Create json from command line args
by boftx (Deacon) on May 21, 2015 at 10:06 UTC

    Does the phrase "Get a life!" have any meaning for you? Code on, brother! :)

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://1127248]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found