summaryrefslogtreecommitdiff
path: root/lib/MON/Options.pm
blob: b798f569e238d7e88c20d477cdfe6b87c1c0a697 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package MON::Options;

use strict;
use warnings;
use v5.10;
use autodie;

use Data::Dumper;
use Scalar::Util qw(looks_like_number);

use MON::Utils;

sub new {
  my ( $class, %opts ) = @_;

  my $self = bless \%opts, $class;

  $self->init();
  $self->parse();

  return $self;
}

sub init {
  my ($self) = @_;

  my %opts = (
    opts => {
      config      => '',
      debug       => 0,
      dry         => 0,
      help        => 0,
      interactive => 0,
      meta        => 0,
      nocolor     => 0,
      quiet       => 0,
      syslog      => 0,
      unique      => 0,
      verbose     => 0,
      version     => 0,
      errfile     => '',
    },
    opts_short => {
      c => 'config',
      D => 'debug',
      d => 'dry',
      i => 'interactive',
      h => 'help',
      m => 'meta',
      n => 'nocolor',
      q => 'quiet',
      s => 'syslog',
      u => 'unique',
      v => 'verbose',
      V => 'version',
      R => 'errfile',
    },
    unknown => [],
  );

  $self->{$_} = $opts{$_} for keys %opts;

  return undef;
}

sub parse {
  my ($self) = @_;

  my $opts_passed = $self->{opts_passed};

  for my $opt (@$opts_passed) {
    my ( $k, $v ) = split /=/, $opt;

    # Longopt
    if ( $k =~ s/^--// && isin $k, keys %{ $self->{opts} } ) {
      if ( defined $v ) {
        $self->{opts}{$k} = $v;
      }
      else {
        $self->{opts}{$k} = 1;
      }
    }

    # Shortopt
    elsif ( $k =~ s/^-// && isin $k, keys %{ $self->{opts_short} } ) {
      if ( defined $v ) {
        $self->{opts}{ $self->{opts_short}{$k} } = $v;
      }
      else {
        $self->{opts}{ $self->{opts_short}{$k} } = 1;
      }

    }
    elsif ( $k !~ /\./ ) {

      # If key is not separated by dot, it is unknown
      push @{ $self->{unknown} }, $opt;

    }
    else {

      # Otherise it might overwrite a value of mon.conf
      $self->{opts}{$k} = $v;
    }
  }

  # Help implies dry mode
  $self->{opts}{dry} = 1 if $self->{opts}{help};

  # Debug implies verbose mode
  $self->{opts}{verbose} = 1 if $self->{opts}{debug};

  return undef;
}

sub get_keys {
  my ($self) = @_;
  my @keys;

  while ( my ( $k, $v ) = each %{ $self->{opts_short} } ) {
    if ( looks_like_number( $self->{opts}{$v} ) ) {
      push @keys, "--$v -$k";
    }
    else {
      push @keys, "--$v=VAL -$k=VAL";
    }
  }

  return @keys;
}

sub store {
  my ( $self, $config ) = @_;

  $self->store_first($config);
  $self->store_after($config);

  return undef;
}

# Only store values which are not separated by dots
sub store_first {
  my ( $self, $config ) = @_;

  for ( grep !/\./, keys %{ $self->{opts} } ) {
    $config->{$_} = $self->{opts}{$_};
  }

  return undef;
}

# Only store values which are separated by dots
sub store_after {
  my ( $self, $config ) = @_;

  for ( grep /\./, keys %{ $self->{opts} } ) {
    $config->{$_} = $self->{opts}{$_};
  }

  return undef;
}

1;