| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | #------------------------------- | ||
| 4 | # flickr_make_test_values.pl | ||
| 5 | #_______________________________ | ||
| 6 | |||
| 7 | use warnings; | ||
| 8 | use strict; | ||
| 9 | use Term::ReadLine; | ||
| 10 | use Pod::Usage; | ||
| 11 | use Getopt::Long; | ||
| 12 | |||
| 13 | my $values = {}; | ||
| 14 | my $cli_args = {}; | ||
| 15 | |||
| 16 | my %defaults = ( | ||
| 17 | 'search_email' => '', | ||
| 18 | 'search_username' => '', | ||
| 19 | ); | ||
| 20 | |||
| 21 | my %prompts = ( | ||
| 22 | 'search_email' => 'An email to search for', | ||
| 23 | 'search_username' => 'A user id to search for', | ||
| 24 | ); | ||
| 25 | |||
| 26 | my $invalues = \%defaults; | ||
| 27 | |||
| 28 | |||
| 29 | GetOptions ( | ||
| 30 | $cli_args, | ||
| 31 | 'values_in=s', | ||
| 32 | 'values_out=s', | ||
| 33 | 'help', | ||
| 34 | 'man', | ||
| 35 | 'usage' | ||
| 36 | ); | ||
| 37 | |||
| 38 | |||
| 39 | #------------------------------------------------------------- | ||
| 40 | # Respond to help-type arguments or if missing required params | ||
| 41 | #_____________________________________________________________ | ||
| 42 | |||
| 43 | if ($cli_args->{'help'} or $cli_args->{'usage'} or $cli_args->{'man'} or !$cli_args->{'values_out'}) { | ||
| 44 | |||
| 45 | pod2usage({ -verbose => 2 }); | ||
| 46 | |||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | #------------------------------------------------------------------ | ||
| 51 | # If an incoming values is specified and exists, read it if we can. | ||
| 52 | # if any of the keys are defined (that is, a value we will use) | ||
| 53 | # overwrite the default. | ||
| 54 | #__________________________________________________________________ | ||
| 55 | |||
| 56 | if (defined($cli_args->{'values_in'}) and -e $cli_args->{'values_in'}) { | ||
| 57 | |||
| 58 | my $key; | ||
| 59 | my $value; | ||
| 60 | |||
| 61 | open my $VALUES_IN, '<', $cli_args->{'values_in'} or die "\nCannot open $cli_args->{'values_in'} for read: $!\n"; | ||
| 62 | while (<$VALUES_IN>) { | ||
| 63 | |||
| 64 | s/\s+//g; | ||
| 65 | ($key,$value) = split(/=/); | ||
| 66 | |||
| 67 | if (defined($invalues->{$key})) { $invalues->{$key}=$value; } | ||
| 68 | } | ||
| 69 | |||
| 70 | close($VALUES_IN) or die "\nClose error $!\n"; | ||
| 71 | |||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | #--------------------------------- | ||
| 76 | # Create a term incase we need it. | ||
| 77 | #_________________________________ | ||
| 78 | |||
| 79 | my $term = Term::ReadLine->new('Flickr Value Collector'); | ||
| 80 | $term->ornaments(0); | ||
| 81 | |||
| 82 | my $which_rl = $term->ReadLine; | ||
| 83 | |||
| 84 | if ($which_rl eq "Term::ReadLine::Perl" or $which_rl eq "Term::ReadLine::Perl5") { | ||
| 85 | |||
| 86 | warn "\n\nTerm::ReadLine::Perl and Term::ReadLine::Perl5 may display prompts" . | ||
| 87 | "\nincorrectly. If this is the case for you, try adding \"PERL_RL=Stub\"" . | ||
| 88 | "\nto the environment variables passed in with make test\n\n"; | ||
| 89 | |||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | #-------------------------------------------------------------------- | ||
| 94 | # build or confirm values | ||
| 95 | #____________________________________________________________________ | ||
| 96 | |||
| 97 | foreach my $key (sort keys %defaults) { | ||
| 98 | |||
| 99 | my $value = $term->readline( $prompts{$key} ." [ ". $invalues->{$key}."]: "); | ||
| 100 | |||
| 101 | if (!$value) { $values->{$key} = $invalues->{$key}; } | ||
| 102 | else { $values->{$key} = $value; } | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | #------------------------------- | ||
| 109 | # Display values and store same. | ||
| 110 | #_______________________________ | ||
| 111 | |||
| 112 | |||
| 113 | open my $VALUES_OUT, '>', $cli_args->{'values_out'} or die "\nCannot open $cli_args->{'values_out'} for write: $!\n"; | ||
| 114 | |||
| 115 | foreach my $key (sort keys %defaults) { | ||
| 116 | |||
| 117 | print $key," = ",$values->{$key},"\n"; | ||
| 118 | print $VALUES_OUT $key," = ",$values->{$key},"\n"; | ||
| 119 | |||
| 120 | } | ||
| 121 | |||
| 122 | close($VALUES_OUT) or die "\nClose error $!\n"; | ||
| 123 | |||
| 124 | |||
| 125 | exit; | ||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | __END__ |