Getopt --verbose --silent relationship, and --dryrun too

[email protected] (Poul Henning Sørensen)
Newsgroups perl.moose
Message-ID <[email protected]>
Hi

Using Getopt::Long it was fairly easy to implement two related options, like --verbose and --silent:

  my $verbose = '';   # option variable with default value (false)
  GetOptions ('verbose|v' => \$verbose, 'quiet'   => sub { $verbose = 0 });

or:
  sub opt_verbose {
    my ( $name, $val ) = @_;
    if ( $name =~ /^s/i ) {
      $verbose = 0;
    } else {
      $verbose = 1 unless $val;
      $verbose += $val;
    }
  }
  GetOptions( 'verbose|v:i' => \&opt_verbose, 'silent' => \&opt_verbose );

How can I achieve something similar with Moose::Getopt?

  has verbose =>
    (
       is => 'rw',
       traits => [ qw( Getopt ENV ) ],
       default      => 0,
       lazy         => 1,
       cmd_aliases => [ qw/ v / ],
       # ...
    );
  has silent => 
    (
    );

I want to do the whole standard thing:
  default value is 0
  %ENV overrides the default
  @ARGV overrides $ENV
  new_with_options parameters overrides @ARGV
    [I'm not certain this is what one wants, but I can't seem anyway around it]
  $foo->set_verbose(1) as expected

This should work for the std options --verbose, --silent/--quiet, --debug, and possible a few more...

Beside this I also intend to implement --version and --dryrun. Dryrun also seems to come with some trouble,
as you shouldn't be able to unset it if it is set...
i.e.: 
  default value is 0
  set it in %ENV: DRYRUN=1 and you won't be able to unset it 
  set it on cmd-line @ARGV --dryrun, and you won't be able to unset it 
  set it on instantiation  new_with_options( dryrun => 1 ); and you should be able to change it
  $foo->set_dryrun(1) should also be overridable

The reason for this?
Set outside the program: Once set, can't be unset, not even from within the program.
Set inside the program, would encourage: do a dryrun and then do the real thing (as in: determine what to do,
show it, get a confirmation and do it)


Anyone done this before me or have some good pointers or ideas?

Thanks.

regards

Poul H. Sørensen
senior systems consultant
--
Basefarm AS
Nydalen Allé 37A, NO-0484 Oslo
Postal Address: PB 4488 Nydalen, NO-0403 Oslo
+47 9117 7237    | +47 4000 4100
[email protected] | www.basefarm.com

Technical excellence – caring for your business
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.