| 1 | # | ||
| 2 | # Getopt::Awesome | ||
| 3 | # | ||
| 4 | # Author(s): Pablo Fischer (pfischer@cpan.org) | ||
| 5 | # Created: 09/25/2009 11:43:15 AM | ||
| 6 | package Getopt::Awesome; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Getopt::Awesome - Let your modules define/export their own arguments | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | First of, this module was very inspired in the Getopt::Modular CPAN package | ||
| 15 | however at the moment of using it I found it was giving me "more" of what I | ||
| 16 | was looking so I thought I could borrow some ideas of it, make it lighter | ||
| 17 | and add some of the features/functionalities I was looking for and so this | ||
| 18 | is the result: a module I've been using every day for all my perl scripts | ||
| 19 | and modules, though would be nice to give it to the Perl community. | ||
| 20 | |||
| 21 | Now, this module is handy if you want to give your modules the freedom of | ||
| 22 | definining their own "getopt options" so next time they get called (or *used*) | ||
| 23 | the options will be available in the form of arguments (--foo, --bar). | ||
| 24 | |||
| 25 | Another feature of this module is that when user asks for help (-h or --help) | ||
| 26 | a usage will be printed by showing all the options available by the current | ||
| 27 | perl script and by all the modules in use. | ||
| 28 | |||
| 29 | All options are prefixed by the package name in lowercase where namespace | ||
| 30 | separator (::) gets replaced by a dash (-), so --help will return: | ||
| 31 | |||
| 32 | --foo-bar-option Description. | ||
| 33 | --foo-bar-option2 Description 2. | ||
| 34 | |||
| 35 | and so on.. | ||
| 36 | |||
| 37 | See the SYNOPSYS section for examples. | ||
| 38 | |||
| 39 | B<Notes:> | ||
| 40 | |||
| 41 | =over 4 | ||
| 42 | |||
| 43 | =item * | ||
| 44 | |||
| 45 | The use of short aliases is not supported for options defined in modules, this | ||
| 46 | feature (provided by Getopt) is only available in the main script (.pl) | ||
| 47 | |||
| 48 | =item * | ||
| 49 | |||
| 50 | In your perl script (.pl) remember to call parse_opts otherwise the values of | ||
| 51 | the options you request might be undef, empty or have their default values. | ||
| 52 | |||
| 53 | =item * | ||
| 54 | |||
| 55 | I<Remember:> ARGV is ONLY parsed when parse_opt is called. | ||
| 56 | |||
| 57 | =back | ||
| 58 | |||
| 59 | =head1 SYNOPSYS | ||
| 60 | |||
| 61 | package Your::Package; | ||
| 62 | |||
| 63 | use Getopt::Awesome qw(:common); | ||
| 64 | define_option('foo=s', 'Foo bar'); | ||
| 65 | # ...or... | ||
| 66 | |||
| 67 | define_options( | ||
| 68 | ('foo=s', 'Foo'), | ||
| 69 | ('bar=s', 'Bar')); | ||
| 70 | |||
| 71 | parse_opts(); | ||
| 72 | my $foo_val = get_opt('option_name', 'Default value'); | ||
| 73 | |||
| 74 | =head1 AUTHOR | ||
| 75 | |||
| 76 | Pablo Fischer (pablo@pablo.com.mx). | ||
| 77 | |||
| 78 | =head1 COPYRIGHT | ||
| 79 | |||
| 80 | Copyright (C) 2009 by Pablo Fischer | ||
| 81 | |||
| 82 | This library is free software; you can redistribute it and/or modify | ||
| 83 | it under the same terms as Perl itself. | ||
| 84 | |||
| 85 | =cut | ||
| 86 | use 5.006; | ||
| 87 | |||
| 88 | use strict; | ||
| 89 | use warnings; | ||
| 90 | use vars qw($no_usage $app_name $no_usage_exit); | ||
| 91 | use Getopt::Long qw( | ||
| 92 | :config | ||
| 93 | require_order | ||
| 94 | gnu_compat | ||
| 95 | no_ignore_case); | ||
| 96 | use Text::Wrap; | ||
| 97 | use Exporter 'import'; | ||
| 98 | |||
| 99 | our @EXPORT_OK = qw( | ||
| 100 | usage | ||
| 101 | define_option | ||
| 102 | define_options | ||
| 103 | get_opt | ||
| 104 | set_opt | ||
| 105 | parse_opts | ||
| 106 | %args); | ||
| 107 | our @EXPORT = qw(parse_opts); | ||
| 108 | our %EXPORT_TAGS = ( | ||
| 109 | 'all' => [ @EXPORT_OK ], | ||
| 110 | 'common' => [ qw(define_option define_options get_opt set_opt) ], | ||
| 111 | ); | ||
| 112 | |||
| 113 | our $VERSION = '0.1.3'; | ||
| 114 | |||
| 115 | |||
| 116 | my (%options, $parsed_args, $show_usage, %args); | ||
| 117 | |||
| 118 | =head1 FUNCTIONS | ||
| 119 | |||
| 120 | =over 4 | ||
| 121 | |||
| 122 | =item B<define_options (@options)> | ||
| 123 | |||
| 124 | use Getopt::Awesome; | ||
| 125 | Getopt::Awesome qw(:common); | ||
| 126 | |||
| 127 | define_options( | ||
| 128 | ('option_name', 'Option description') | ||
| 129 | ); | ||
| 130 | |||
| 131 | It defines the given options for the package/script that is making *the call*. | ||
| 132 | |||
| 133 | Please note the options defined in the current caller package are not shared | ||
| 134 | with other modules unless it's explicitly specified (see I<get_opt()>). | ||
| 135 | |||
| 136 | Each array item should consist at least of 1 item with a max of 2. The first | ||
| 137 | parameter should be the option name while the second one (optional) is the | ||
| 138 | description. | ||
| 139 | |||
| 140 | Some notes about the option name, the first item of every array: | ||
| 141 | |||
| 142 | =over 4 | ||
| 143 | |||
| 144 | =item * | ||
| 145 | |||
| 146 | It's a required parameter. | ||
| 147 | |||
| 148 | =item * | ||
| 149 | |||
| 150 | It accepts any of the C<Getopt::Long> option name styles (=s, !, =s@, etc). | ||
| 151 | |||
| 152 | =back | ||
| 153 | |||
| 154 | =cut | ||
| 155 | sub define_options { | ||
| 156 | my $new_options = shift; | ||
| 157 | if (ref $new_options ne 'ARRAY') { | ||
| 158 | die 'The options should be passed as an array'; | ||
| 159 | } | ||
| 160 | my $current_package = _get_option_package(); | ||
| 161 | foreach my $opt (@$new_options) { | ||
| 162 | my ($option_name, $option_description) = (@$opt); | ||
| 163 | if (!$option_name) { | ||
| 164 | die "Option name wasn't found"; | ||
| 165 | } | ||
| 166 | if ($option_name =~ /\|/ && $current_package ne 'main') { | ||
| 167 | die "Sorry but no aliases ($option_name) are suported for " . | ||
| 168 | "modules except main"; | ||
| 169 | } | ||
| 170 | $options{$current_package}{$option_name} = $option_description; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | =item B<define_option( $name, $description )> | ||
| 175 | |||
| 176 | use Getopt::Awesome qw(:common); | ||
| 177 | |||
| 178 | define_option('option_name', 'Description'); | ||
| 179 | |||
| 180 | It calls the I<define_options> subroutine for adding the given option | ||
| 181 | (I<$name>) with an optional description (I<$description>). | ||
| 182 | |||
| 183 | Please refer to the documentation of C<define_options> for a more complete | ||
| 184 | description about it, but basically some notes: | ||
| 185 | |||
| 186 | =over 4 | ||
| 187 | |||
| 188 | =item * | ||
| 189 | |||
| 190 | The option name is a required parameter | ||
| 191 | |||
| 192 | =item * | ||
| 193 | |||
| 194 | The option accepts any of te C<Getopt::Long> option name styles. | ||
| 195 | |||
| 196 | =back | ||
| 197 | |||
| 198 | =cut | ||
| 199 | sub define_option { | ||
| 200 | # Find the right option, we don't like namespaces or classes.. | ||
| 201 | my ($option_name, $option_description) = @_; | ||
| 202 | define_options([[$option_name, $option_description]]); | ||
| 203 | } | ||
| 204 | |||
| 205 | =item B<get_opt($option_name, $default_value)> | ||
| 206 | |||
| 207 | use Getopt::Awesome qw(:common); | ||
| 208 | |||
| 209 | my $val = get_opt('option_name', 'Some default opt'); | ||
| 210 | # Gets the 'foome' option value of Foo::Bar module and defaults to 'foobie' | ||
| 211 | my $val = get_opt('Foo::Bar::foome', 'foobie'); | ||
| 212 | |||
| 213 | It will return the value of the given option, if there's no option set then | ||
| 214 | undefined will be returned. | ||
| 215 | |||
| 216 | Please note that if the option is set to expect a list you will receive a list, | ||
| 217 | same for integer, strings, booleans, etc. Same as it happens with the | ||
| 218 | Getopt::Long. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | sub get_opt { | ||
| 222 | my ($option_name, $default_value) = @_; | ||
| 223 | return $default_value unless $option_name; | ||
| 224 | # The option name comes with a namespace? | ||
| 225 | my $package = _get_option_package(); | ||
| 226 | if ($option_name =~ /(\S+::){1,}(\S+)$/) { | ||
| 227 | $package = substr($1, 0, -2); | ||
| 228 | $option_name = $2; | ||
| 229 | } | ||
| 230 | if ($package ne 'main') { | ||
| 231 | $package = lc $package; | ||
| 232 | $package =~ s/::/-/g; | ||
| 233 | $option_name = $package . '-' . $option_name; | ||
| 234 | } | ||
| 235 | if (defined $args{$option_name}) { | ||
| 236 | return $args{$option_name}; | ||
| 237 | } else { | ||
| 238 | if (defined $default_value) { | ||
| 239 | return $default_value; | ||
| 240 | } | ||
| 241 | } | ||
| 242 | return ''; | ||
| 243 | } | ||
| 244 | |||
| 245 | =item B<set_opt ($option_name, $value)> | ||
| 246 | |||
| 247 | use Getopt::Awesome qw(:common); | ||
| 248 | |||
| 249 | set_opt('option_name', 'Value'); | ||
| 250 | # Sets the 'foome' option value to foobie of the Foo::Bar package. | ||
| 251 | set_opt('Foo::Bar::foome', 'foobie') | ||
| 252 | |||
| 253 | Sets the given value to the given option. | ||
| 254 | |||
| 255 | =cut | ||
| 256 | sub set_opt { | ||
| 257 | my ($option_name, $option_value) = @_; | ||
| 258 | my $package = _get_option_package(); | ||
| 259 | if ($option_name =~ /(\S+::){1,}(\S+)$/) { | ||
| 260 | $package = substr($1, 0, -2); | ||
| 261 | $option_name = $2; | ||
| 262 | } | ||
| 263 | if ($package ne 'main') { | ||
| 264 | $package = lc $package; | ||
| 265 | $package =~ s/::/-/g; | ||
| 266 | $option_name = $package . '-' . $option_name; | ||
| 267 | } | ||
| 268 | $args{$option_name} = $option_value; | ||
| 269 | } | ||
| 270 | |||
| 271 | =item B<parse_opts()> | ||
| 272 | |||
| 273 | This subroutine should never be called directly unless you want to re-parse the | ||
| 274 | arguments or that your module is not getting called from a perl script (.pl). | ||
| 275 | |||
| 276 | In case you want to call it: | ||
| 277 | |||
| 278 | use Getopt::Awesome qw(:common); | ||
| 279 | |||
| 280 | parse_opts(); | ||
| 281 | |||
| 282 | =cut | ||
| 283 | sub parse_opts { | ||
| 284 | my %all_options = _build_all_options(); | ||
| 285 | if (!defined $no_usage) { | ||
| 286 | $all_options{'h|help'} = 1; | ||
| 287 | } | ||
| 288 | my $res = GetOptions( | ||
| 289 | \%args, | ||
| 290 | keys %all_options); | ||
| 291 | if ($args{'h'} || | ||
| 292 | (!defined $no_usage && !%args)) { | ||
| 293 | usage(); | ||
| 294 | if (!defined $no_usage_exit) { | ||
| 295 | exit(1); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | =item B<usage()> | ||
| 301 | |||
| 302 | Based on all the current options it returns a nice and helpful 'guide' | ||
| 303 | of all the available options. | ||
| 304 | |||
| 305 | Although the usage gets called directly if a -h or --help is passed | ||
| 306 | and also if no_usage is set you can call it directly: | ||
| 307 | |||
| 308 | use Getopt::Awesome qw(:all); | ||
| 309 | |||
| 310 | usage(); | ||
| 311 | |||
| 312 | =back | ||
| 313 | |||
| 314 | =cut | ||
| 315 | sub usage { | ||
| 316 | my (%main_options, %other_options); | ||
| 317 | if ($app_name) { | ||
| 318 | print "$app_name\n"; | ||
| 319 | } | ||
| 320 | if (scalar %options ge 1) { | ||
| 321 | print "Options:\n"; | ||
| 322 | } | ||
| 323 | $Text::Wrap::columns = 80; | ||
| 324 | my @packages = keys %options; | ||
| 325 | my ($main_pos) = grep($packages[$_] eq 'main', 0 .. $#packages); | ||
| 326 | # Lets make sure that main options are showed first | ||
| 327 | if ($main_pos) { | ||
| 328 | splice(@packages, $main_pos, 1); | ||
| 329 | unshift(@packages, 'main'); | ||
| 330 | } | ||
| 331 | foreach my $package (@packages) { | ||
| 332 | my $package_option_prefix = lc $package; | ||
| 333 | $package_option_prefix =~ s/::/-/g; | ||
| 334 | if ($package ne 'main') { | ||
| 335 | print "\nOptions from: $package\n"; | ||
| 336 | print "Prepend --$package_option_prefix to use them\n"; | ||
| 337 | } | ||
| 338 | foreach my $opt (keys %{$options{$package}}) { | ||
| 339 | my ($option_name, $option_type) = split('=', $opt); | ||
| 340 | # Perhaps option is a + or a !? | ||
| 341 | if (substr($option_name, -1, 1) eq '!') { | ||
| 342 | $option_type = '!'; | ||
| 343 | $option_name =~ s/!//; | ||
| 344 | } elsif(substr($option_name, -1, 1) eq '+') { | ||
| 345 | $option_type = '+'; | ||
| 346 | $option_name =~ s/\+//; | ||
| 347 | } | ||
| 348 | if (!defined $option_type) { | ||
| 349 | $option_type = ''; | ||
| 350 | } | ||
| 351 | $option_name=~ s/[\!,\+]//g; | ||
| 352 | my @aliases = split('\|', $option_name); | ||
| 353 | foreach (@aliases) { | ||
| 354 | my $dash = length $_ eq 1 ? '-' : '--'; | ||
| 355 | if ($package eq 'main') { | ||
| 356 | $_ = "$dash$_"; | ||
| 357 | } else { | ||
| 358 | $_ = "-$_"; | ||
| 359 | } | ||
| 360 | } | ||
| 361 | my $description = $options{$package}{$opt}; | ||
| 362 | printf " %-35s", join(', ', @aliases); | ||
| 363 | printf "%-10s", $description; | ||
| 364 | print "\n"; | ||
| 365 | } | ||
| 366 | } | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | ################### PRIVATE METHODS #################### | ||
| 371 | # | ||
| 372 | # Should _never_ be called directly. It will parse all the options we have and | ||
| 373 | # will prepare a hash that C<Getopt::Long::Getoptions> will use to parse the | ||
| 374 | # arguments provided by @ARGV. | ||
| 375 | sub _build_all_options { | ||
| 376 | my %get_options; | ||
| 377 | foreach my $package (keys %options) { | ||
| 378 | foreach my $opt (keys %{$options{$package}}) { | ||
| 379 | my $option_name = $opt; | ||
| 380 | if ($package ne 'main') { | ||
| 381 | $option_name = lc $package; | ||
| 382 | $option_name =~ s/::/-/g; | ||
| 383 | $option_name = $option_name . '-' . $opt; | ||
| 384 | } | ||
| 385 | $get_options{$option_name} = 1; | ||
| 386 | } | ||
| 387 | } | ||
| 388 | return %get_options; | ||
| 389 | } | ||
| 390 | |||
| 391 | # | ||
| 392 | # Returns the right option package where the options are going to be stored. | ||
| 393 | # | ||
| 394 | sub _get_option_package { | ||
| 395 | # Look for the real package, it shouldn't be this package | ||
| 396 | my ($caller_package, $tries, $max_tries) = ('', 0, 10); | ||
| 397 | while($tries ne $max_tries) { | ||
| 398 | ($caller_package) = caller($tries); | ||
| 399 | if ($caller_package eq __PACKAGE__) { | ||
| 400 | $tries += 1; | ||
| 401 | } else { | ||
| 402 | last; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | if ($caller_package eq __PACKAGE__) { | ||
| 406 | return 'main'; | ||
| 407 | } | ||
| 408 | if (!$caller_package) { | ||
| 409 | return 'main'; | ||
| 410 | } | ||
| 411 | return $caller_package; | ||
| 412 | } | ||
| 413 | |||
| 414 | 1; |