tools: validate utf8 encoding in stdin/out
[sheet.git] / tools / mkclioptions
1 #!/usr/bin/env perl
2 use 5.014;
3 use warnings;
4 use utf8;
5 use re '/msx';
6 use open OUT => ':encoding(utf-8)', ':std';
7
8 our $VERSION = '1.00';
9
10 say '# automatically generated by tools/mkclioptions';
11 say '+{';
12
13 for my $program (@ARGV) {
14         my $help = eval {
15                 local $/;
16                 open my $output, '-|', $program, '--help';
17                 return readline $output;
18         } or next;
19
20         printf "%s => {\n", $program;
21
22         while ($help =~ m{ ^\h+ (-\N*?) (?: \h{3,} (\N*) )? \n }g) {
23                 #TODO: continuations
24                 my ($options, $explain) = ($1, $2);
25                 my ($short) = $options =~ m{ (?<! \H) -([^-]) (?! [^,\h]) }
26                         or next;
27                 printf "  ['%s', '%s', q{%s}]\n", $short, $options, $explain // '';
28         }
29
30         say '},';
31 }
32
33 say '}';
34
35 __END__
36
37 =head1 NAME
38
39 mkclioptions
40
41 =head1 SYNOPSIS
42
43     mkclioptions ls >clioptions.inc.pl
44
45 =head1 AUTHOR
46
47 Mischa POSLAWSKY <perl@shiar.org>
48
49 =head1 LICENSE
50
51 Licensed under the GNU Affero General Public License version 3.
52