cli: mkclioptions tool to parse help output of given commands
authorMischa POSLAWSKY <perl@shiar.org>
Fri, 6 Sep 2019 18:57:42 +0000 (20:57 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Tue, 25 Feb 2020 13:19:11 +0000 (14:19 +0100)
Initial automation of getting available options from basic GNU programs.

tools/mkclioptions [new file with mode: 0755]

diff --git a/tools/mkclioptions b/tools/mkclioptions
new file mode 100755 (executable)
index 0000000..54192b0
--- /dev/null
@@ -0,0 +1,52 @@
+#!/usr/bin/env perl
+use 5.014;
+use warnings;
+use utf8;
+use re '/msx';
+use open OUT => ':utf8', ':std';
+
+our $VERSION = '1.00';
+
+say '# automatically generated by tools/mkclioptions';
+say '+{';
+
+for my $program (@ARGV) {
+       my $help = eval {
+               local $/;
+               open my $output, '-|', $program, '--help';
+               return readline $output;
+       } or next;
+
+       printf "%s => {\n", $program;
+
+       while ($help =~ m{ ^\h+ (-\N*?) (?: \h{3,} (\N*) )? \n }g) {
+               #TODO: continuations
+               my ($options, $explain) = ($1, $2);
+               my ($short) = $options =~ m{ (?<! \H) -([^-]) (?! [^,\h]) }
+                       or next;
+               printf "  ['%s', '%s', q{%s}]\n", $short, $options, $explain // '';
+       }
+
+       say '},';
+}
+
+say '}';
+
+__END__
+
+=head1 NAME
+
+mkclioptions
+
+=head1 SYNOPSIS
+
+    mkclioptions ls >clioptions.inc.pl
+
+=head1 AUTHOR
+
+Mischa POSLAWSKY <perl@shiar.org>
+
+=head1 LICENSE
+
+Licensed under the GNU Affero General Public License version 3.
+