feat: use Getopt::Long to manage arguments

This commit is contained in:
2025-11-23 11:28:43 +01:00
parent c80e7d8e6b
commit 277ca8be24

33
m2h.pl
View File

@@ -1,5 +1,6 @@
#!perl -w #!perl -w
use strict; use strict;
use Getopt::Long;
use MarkdownParser; use MarkdownParser;
sub show_help { sub show_help {
@@ -37,27 +38,19 @@ sub read_input {
} }
my $output_file; my $output_file;
my $input_file; my $help = 0;
my $version = 0;
for ( my $i = 0 ; $i < @ARGV ; $i++ ) { GetOptions(
my $arg = $ARGV[$i]; 'help|h' => \$help,
if ( $arg eq '-h' || $arg eq '--help' ) { 'version|v' => \$version,
show_help(); 'output|o=s' => \$output_file,
} ) or show_help();
elsif ( $arg eq '-v' || $arg eq '--version' ) {
show_version(); show_help() if $help;
} show_version() if $version;
elsif ( $arg eq '-o' || $arg eq '--output' ) {
$output_file = $ARGV[ ++$i ] my $input_file = shift @ARGV;
or die "Error: -o requires a filename\n";
}
elsif ( $arg =~ /^-/ ) {
die "Error: Unknown option: $arg\n";
}
else {
$input_file = $arg;
}
}
my $input = read_input($input_file); my $input = read_input($input_file);