CLI-Helpers
view release on metacpan or search on metacpan
lib/CLI/Helpers.pm view on Meta::CPAN
# Warning for syslogging data file
unshift @output, "CLI::Helpers logging a data section, use --data-file to suppress this in syslog."
if $opts->{data};
# Now syslog the message
debug({no_syslog=>1,color=>'magenta'}, sprintf "[%s] Syslogging %d messages, with: %s", $level, scalar(@output), join(",", map { $_=>$opts->{$_} } keys %{ $opts }));
for( @output ) {
# One bad message means no more syslogging
eval {
syslog($level, colorstrip($_));
1;
} or do {
my $error = $@;
$DEF{SYSLOG} = 0;
output({stderr=>1,color=>'red',no_syslog=>1}, "syslog() failed: $error");
};
}
}
# Sticky messages don't just go away
if(exists $opts->{sticky}) {
my %o = %{ $opts }; # Make a copy because we shifted this off @_
# So this doesn't happen in the END block again
delete $o{$_} for grep { exists $o{$_} } qw(sticky data);
$o{no_syslog} = 1;
push @STICKY, [ \%o, @input ];
}
if( $DEF{NOPASTE} ) {
push @NOPASTE, map { $indent . colorstrip($_) } @output;
}
}
sub verbose {
my $opts = is_hashref($_[0]) ? shift @_ : {};
$opts->{level} = 1 unless exists $opts->{level};
$opts->{syslog_level} = $opts->{level} > 1 ? 'debug' : 'info';
my @msgs=@_;
# Ensure we're all configured
cli_helpers_initialize() unless keys %DEF;
if( !$DEF{DEBUG} ) {
return unless $DEF{VERBOSE} >= $opts->{level};
}
output( $opts, @msgs );
}
sub debug {
my $opts = is_hashref($_[0]) ? shift @_ : {};
my @msgs=@_;
# Ensure we're all configured
cli_helpers_initialize() unless keys %DEF;
# Smarter handling of debug output
return unless $DEF{DEBUG};
# Check against caller class
my $package = exists $opts->{_caller_package} ? $opts->{_caller_package} : (caller)[0];
return unless lc $DEF{DEBUG_CLASS} eq 'all' || $package eq $DEF{DEBUG_CLASS};
# Check if we really want to debug syslog data
$opts->{syslog_level} = 'debug';
$opts->{no_syslog} //= !$DEF{SYSLOG_DEBUG};
# Output
output( $opts, @msgs );
}
sub debug_var {
my $opts = {
clear => 1, # Meant for the screen
no_syslog => 1, # Meant for the screen
_caller_package => (caller)[0], # Make sure this is set on entry
};
# Merge with options
if( is_hashref($_[0]) && defined $_[1] && is_ref($_[1]) ) {
my $ref = shift;
foreach my $k (keys %{ $ref } ) {
$opts->{$k} = $ref->{$k};
};
}
my $var = shift;
debug($opts, $DEF{ENCODE} eq 'json' || $opts->{json} ? $var : YAML::XS::Dump $var);
}
my %_allow_override = map { $_ => 1 } qw(debug verbose);
sub override {
my ($var,$value) = @_;
return unless exists $_allow_override{lc $var};
my $def_var = uc $var;
$DEF{$def_var} = $value;
}
my $_Confirm_Valid;
sub confirm {
my ($question) = @_;
# Initialize Globals
$_Confirm_Valid ||= {qw(y 1 yes 1 n 0 no 0)};
$question =~ s/\s*$/ [yN] /;
my $answer = undef;
until( defined $answer && exists $_Confirm_Valid->{$answer} ) {
output({color=>'red',stderr=>1},"ERROR: must be one of 'y','n','yes','no'") if defined $answer;
$answer = lc _get_input($question);
}
return $_Confirm_Valid->{$answer};
}
sub text_input {
my $question = shift;
my %args = @_;
# Prompt fixes
chomp($question);
my $terminator = $question =~ s/([^a-zA-Z0-9\)\]\}])\s*$// ? $1 : ':';
if(exists $args{default}) {
$question .= " (default=$args{default}) ";
}
$question .= "$terminator ";
# Make sure there's a space before the prompt
$question =~ s/\s*$/ /;
my $validate = exists $args{validate} ? $args{validate} : {};
my $text;
my $error = undef;
( run in 1.381 second using v1.01-cache-2.11-cpan-389fe586d7c )