App-Easer

 view release on metacpan or  search on metacpan

lib/App/Easer/Tutorial/V2_008.pod  view on Meta::CPAN

   use warnings;
   use English;
   use experimental qw< signatures >;
   use Log::Log4perl::Tiny qw< :easy LOGLEVEL >;

   use App::Easer::V2 qw< run >;

   my $app = {
      aliases => [qw< MAIN >],
      help    => 'An example',
      sources => 'v2.008',
      config_hash_key => 'v2.008',
      options => [
         {
            getopt => 'loglevel|l=s',
            help   => 'logging level for Log::Log4perl::Tiny',
            default => 'info',
            transmit => 1,
         },
      ],

      ##################################################################
      # WE USE final_commit TO SET THE LOGLEVEL
      final_commit => sub ($self) {
         LOGLEVEL(uc($self->config('loglevel')));
         return;
      },

      children => [
         {
            aliases => [qw< seeker >],
            help    => 'some add-on to look at the seed!',
            description => '',
            options => [ '+parent' ],
            execute => sub ($self) {
               WARN 'this is WARN';
               INFO 'this is INFO';
               DEBUG 'this is DEBUG';
               return 0;
            },
         },
      ],
   };

   exit(run($app, $0, @ARGV) // 0);

Alas, this does not work yet! Example run after this change:

   $ logger-example seeker --loglevel warn
   [2024/09/07 16:34:49] [ WARN] this is WARN
   [2024/09/07 16:34:49] [ INFO] this is INFO

What is happening now?

Each command level in L<App::Easer> is tracked as an object instance by
itself, representing the specific root/intermediate/leaf command. For
this reason, methods called on the specific object provide a I<view>
from that object's perspective.

The C<final_commit> is set inside the root command, so when we call the
C<config> method it only looks at the options collected in the root
command. Which means... no C<loglevel> value set in the selected leaf
command.

For this reason, method C<leaf> allows getting the final I<leaf> command
object that resulted from the search done by L<App::Easer>. Calling it
is meaningful only inside C<final_commit>, but it's exactly where we
need it:

   #!/usr/bin/env perl
   use v5.24;
   use warnings;
   use English;
   use experimental qw< signatures >;
   use Log::Log4perl::Tiny qw< :easy LOGLEVEL >;

   use App::Easer::V2 qw< run >;

   my $app = {
      aliases => [qw< MAIN >],
      help    => 'An example',
      sources => 'v2.008',
      config_hash_key => 'v2.008',
      options => [
         {
            getopt => 'loglevel|l=s',
            help   => 'logging level for Log::Log4perl::Tiny',
            default => 'info',
            transmit => 1,
         },
      ],
      final_commit => sub ($self) {

         ###############################################################
         # We get the config from $self->leaf insted of $self
         LOGLEVEL(uc($self->leaf->config('loglevel')));

         return;
      },
      children => [
         {
            aliases => [qw< seeker >],
            help    => 'some add-on to look at the seed!',
            description => '',
            options => [ '+parent' ],
            execute => sub ($self) {
               WARN 'this is WARN';
               INFO 'this is INFO';
               DEBUG 'this is DEBUG';
               return 0;
            },
         },
      ],
   };

   exit(run($app, $0, @ARGV) // 0);

We're there at last:

   $ logger-example seeker --loglevel warn
   [2024/09/07 16:34:49] [ WARN] this is WARN



( run in 0.464 second using v1.01-cache-2.11-cpan-39bf76dae61 )