File: Config.pm

package info (click to toggle)
libmoosex-app-perl 1.43-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: perl: 4,011; makefile: 2
file content (97 lines) | stat: -rw-r--r-- 1,766 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# ============================================================================
package MooseX::App::Plugin::Config;
# ============================================================================

use 5.010;
use utf8;

use namespace::autoclean;
use Moose::Role;
use MooseX::App::Role;

use Config::Any;

has 'config' => (
    is              => 'ro',
    predicate       => 'has_config',
    documentation   => q[Path to command config file],
    traits          => ['MooseX::App::Meta::Role::Attribute::Option'],
    cmd_type        => 'proto',
    cmd_position    => 99990,
);

has '_config_data' => (
    is              => 'ro',
    isa             => 'HashRef',
    predicate       => 'has_config_data',
);

sub plugin_metaroles {
    my ($self,$class) = @_;

    return {
        class   => ['MooseX::App::Plugin::Config::Meta::Class'],
    }
}

1;

__END__

=encoding utf8

=head1 NAME

MooseX::App::Plugin::Config - Config files your MooseX::App applications

=head1 SYNOPSIS

In your base class:

 package MyApp;
 use MooseX::App qw(Config);
 
 option 'global_option' => (
     is          => 'rw',
     isa         => 'Int',
 );

In your command class:

 package MyApp::Some_Command;
 use MooseX::App::Command;
 extends qw(MyApp);
 
 option 'some_option' => (
     is          => 'rw',
     isa         => 'Str',
 );

Now create a config file (see L<Config::Any>) eg. a yaml file:

 ---
 global:
   global_option: 123
 some_command:
   global_option: 234
   some_option: "hello world"

The user can now call the program with a config file:

 bash$ myapp some_command --config /path/to/config.yml

=head1 METHODS

=head2 config

Read the config filename

=head2 _config_data

The full content of the loaded config file

=head1 SEE ALSO

L<Config::Any>

=cut