bluto

Release package and announcement generator
Info | Log | Files | Refs | README | LICENSE

commit f0a5b6ad46fee62a151e6813ae59d837b42d6062
parent 6ff4405e50abae24bb9c432d82cc1ddae3a2487a
Author: lash <dev@holbrook.no>
Date:   Tue,  1 Oct 2024 02:02:50 +0100

WIP factor out cmd env handling

Diffstat:
ABluto/Cmd.pm | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mbluto-boot | 3+++
Abluto.pl | 19+++++++++++++++++++
3 files changed, 108 insertions(+), 0 deletions(-)

diff --git a/Bluto/Cmd.pm b/Bluto/Cmd.pm @@ -0,0 +1,86 @@ +package Bluto::Cmd; + +use Getopt::Long qw/ :config auto_help /; +use Cwd qw/ getcwd abs_path /; +use File::Spec; +use File::Path qw/make_path/; + +use Bluto::SemVer; +use YAML::Tiny; + +use Bluto::Log qw/debug/; + + +my $force_version = undef; +my $force_help = 0; +my %env = ( + src_dir => File::Spec->catfile(getcwd, '.bluto'), + version => undef, + loglevel => undef, +); +my @opts_x; + +sub croak { + die(shift); +} + +sub help { + print("$0\n\nwould have shown help...\n"); + exit 0; +} + +sub register_param { + my $env_k = shift; + my $env_v_default = shift; + my $switch_long = shift, + my $switch_short = shift, + my $switch_typ = shift; + $env{$env_k} = $v; + + if (!defined $switch_typ) { + $switch_typ = "s"; + } + if (defined $switch_long) { + push(@opts_x, $switch_long . ':' . $switch_typ); + push(@opts_x, \$env{$env_k}); + } + if (defined $switch_long) { + push(@opts_x, $switch_short . ':' . $switch_typ); + push(@opts_x, \$env{$env_k}); + } + debug("added env k $env_k switches $switch_long / $switch_short"); +} + +sub process_param { + GetOptions( + 'd:s', \$env{src_dir}, + 'v+', \$env{loglevel}, + 'h+', \$force_help, + 'help+', \$force_help, + @opts_x, + ); + + if ($force_help > 0) { + help; + } + + if (defined $env{version}) { + $env{version} = SemVer->new($env{version}); + } + + foreach my $k (keys %env) { + my $v = $env{$k}; + if (defined $v) { + debug("env k $k v $v"); + } else { + debug("env k $k not defined"); + } + } +} + +sub get_param { + my $k = shift; + return $env{$k}; +} + +1; diff --git a/bluto-boot b/bluto-boot @@ -8,6 +8,9 @@ use Getopt::Long qw/ :config auto_help /; use Cwd qw/ getcwd abs_path /; use File::Spec; use File::Path qw/make_path/; +use File::Basename qw/ dirname /; + +use lib (dirname(abs_path($0))); use YAML::Tiny; diff --git a/bluto.pl b/bluto.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +use v5.10.1; +use warnings; +use strict; + +use File::Basename qw/ dirname /; +use Cwd qw/ getcwd abs_path /; +use lib (dirname(abs_path($0))); + +use Bluto::Cmd; + +Bluto::Cmd::register_param("version", undef, "version", undef); +Bluto::Cmd::process_param(); + +my $v = Bluto::Cmd::get_param("version"); +if (!defined $v) { + Bluto::Cmd::croak($v); +}