File: help2man

package info (click to toggle)
ncbi-tools6 6.1.20041020-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 65,588 kB
  • ctags: 74,723
  • sloc: ansic: 1,020,512; xml: 3,390; sh: 2,668; csh: 587; makefile: 546; perl: 292; lisp: 81; cpp: 59
file content (127 lines) | stat: -rw-r--r-- 3,120 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/perl -w
use strict;

use POSIX qw(strftime);

my $AUTHOR = 'Aaron M. Ucko <ucko@debian.org>';

# Generates initial Debian manpages based on help output ("-" flag).
# bod's help2man won't work here, since it assumes GNU conventions.

# Sigh...vibrant and non-vibrant versions only agree on T/F, Integer,
# and String.
my %type_map = ( 'Data In'     => 'filename',
		 'Data Out'    => 'filename',
		 'File In'     => 'filename',
		 'File Out'    => 'filename',
		 'Float'       => 'X',
		 'Input-Data'  => 'filename',
		 'Input-File'  => 'filename',
		 'Integer'     => 'N',
		 'Output-Data' => 'filename',
		 'Output-File' => 'filename',
		 'Real'        => 'X',
		 'String'      => 'str',
		 'T/F'         => undef );

foreach my $prog (@ARGV) {
    my $base = $prog;
    $base =~ s@.*/@@;
    open(MAN, ">doc/man/$base.1") or die;
    print MAN '.TH ', uc($base), ' 1 ', strftime('%Y-%m-%d', localtime),
    " NCBI \"NCBI Tools User's Manual\"\n";
    print MAN ".SH NAME\n$base \\- ...\n";

    my %options = parse_options($prog);
    my $key;

    print MAN ".SH SYNOPSIS\n.B $base\n", synopsis(\%options);
    print MAN <<EOF;
.SH DESCRIPTION
\\fB$base\\fP is ...
.SH OPTIONS
EOF
    if (keys %options) {
	print MAN "A summary of options is included below.\n";
	foreach my $key (sort keys %options) {
	    my $desc = $options{$key};
	    $desc =~ s/\s+\[T\/F\]//;
	    $desc =~ s/\s+Optional//;
	    $desc =~ s/\n(.)/\n.br\n$1/g;
	    print MAN ".TP\n", format_arg($key, $desc), "\n", $desc;
	}
    } else {
	print MAN "None.\n";
    }
    print MAN <<EOF;
.SH AUTHOR
The National Center for Biotechnology Information.
EOF
}

sub parse_options
{
    my $prog = $_[0];
    my %options;
    my $last_option;
    my $last_description;

    open(HELP, "$prog - </dev/null |") or die;

    while (<HELP>) {
	if (/^  (-\S+)\s*(.*)/) {
	    $options{$last_option} = $last_description if defined $last_option;
	    $last_option = $1;
	    $last_description = "$2\n";
	} elsif (defined $last_option) {
	    $last_description .= $_;
	}
    }

    if (defined $last_option) {
	$options{$last_option} = $last_description;
	$options{'-'} = "Print usage message\n";
    }

    close(HELP);

    foreach my $key (keys %options) {
	if ($options{$key} =~ s@(\[T/F\](\s+Optional)?)\s+default = T@$1@) {
	    $options{"$key\\ F"} = "NOT $options{$key}";
	    delete $options{$key};
	} else {
	    $options{$key} =~ s@(\[T/F\](\s+Optional)?)(\s+default = F)?@$1@;
	}
    }

    return %options;
}

sub synopsis
{
    my %options = %{$_[0]};
    my $result = '';

    foreach my $key (sort keys %options) {
	my $desc = $options{$key};
	my $optional = ($desc =~ /Optional/  ||  $desc =~ /default = /
			||  $key eq '-'  ||  $desc =~ /\[T\/F\]/);
	$result .= '[\|' if $optional;
	$result .= format_arg($key, $desc);
	$result .= '\|]' if $optional;
	$result .= "\n";
    }

    return $result;
}

sub format_arg
{
    my ($arg, $desc) = @_;
    my $result = "\\fB\\$arg\\fP";
    if ($desc =~ /\[(.*?)\]/) {
	my $contents = $type_map{$1};
	$result .= '\ \fI' . $contents . '\fP' if defined $contents;
    }
    return $result;
}