]> git.proxmox.com Git - pve-common.git/commitdiff
add experimental 'asciidoc' generator
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 29 Dec 2015 16:02:00 +0000 (17:02 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 29 Dec 2015 16:02:00 +0000 (17:02 +0100)
useful to generate manual pages using asciidoc.

src/PVE/CLIHandler.pm
src/PVE/RESTHandler.pm

index 149353797238ef481e135fafb9e65b51259a21bf..21fb916da9d50e02a20b0ab44113c9a7a211208e 100644 (file)
@@ -110,6 +110,46 @@ __PACKAGE__->register_method ({
 
     }});
 
+sub print_simple_asciidoc_synopsys {
+    my ($class, $name, $arg_param, $uri_param) = @_;
+
+    die "not initialized" if !$cli_handler_class;
+
+    my $pwcallback = $cli_handler_class->can('read_password');
+
+    my $synopsis = "*${name}* `help`\n\n";
+
+    $synopsis .= $class->usage_str($name, $name, $arg_param, $uri_param, 'asciidoc', $pwcallback);
+
+    return $synopsis;
+}
+
+sub print_asciidoc_synopsys {
+
+    die "not initialized" if !($cmddef && $exename && $cli_handler_class);
+
+    my $pwcallback = $cli_handler_class->can('read_password');
+
+    my $synopsis = "";
+
+    $synopsis .= "*${exename}* `<COMMAND> [ARGS] [OPTIONS]`\n\n";
+
+    my $oldclass;
+    foreach my $cmd (sort keys %$cmddef) {
+       my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
+       my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
+                                   $uri_param, 'asciidoc', $pwcallback);
+       $synopsis .= "\n" if $oldclass && $oldclass ne $class;
+
+       $synopsis .= "$str\n\n";
+       $oldclass = $class;
+    }
+
+    $synopsis .= "\n";
+
+    return $synopsis;
+}
+
 sub print_simple_pod_manpage {
     my ($podfn, $class, $name, $arg_param, $uri_param) = @_;
 
@@ -389,6 +429,27 @@ sub generate_pod_manpage {
     }
 }
 
+sub generate_asciidoc_synopsys {
+    my ($class) = @_;
+
+    $cli_handler_class = $class;
+
+    $exename = &$get_exe_name($class);
+
+    no strict 'refs';
+    my $def = ${"${class}::cmddef"};
+
+    if (ref($def) eq 'ARRAY') {
+       print_simple_asciidoc_synopsys(@$def);
+    } else {
+       $cmddef = $def;
+
+       $cmddef->{help} = [ __PACKAGE__, 'help', ['cmd'] ];
+
+       print_asciidoc_synopsys();
+    }
+}
+
 my $handle_cmd  = sub {
     my ($def, $cmdname, $cmd, $args, $pwcallback, $podfn, $preparefunc) = @_;
 
index dda58ea1d8430ce7d5ea3a5311b5d80acd81dd8b..c6e7436128ae5507afa14566d44a954c21df34ab 100644 (file)
@@ -414,6 +414,7 @@ sub handle {
 #   'long'     ... default (list all options)
 #   'short'    ... command line only (one line)
 #   'full'     ... also include description
+#   'asciidoc' ... generate asciidoc for man pages (like 'full')
 # $hidepw      ... hide password option (use this if you provide a read passwork callback)
 sub usage_str {
     my ($self, $name, $prefix, $arg_param, $fixed_param, $format, $hidepw) = @_;
@@ -460,22 +461,39 @@ sub usage_str {
        if ($hidepw && $k eq 'password') {
            $type = '';
        }
-       
-       my $defaulttxt = '';
-       if (defined(my $dv = $phash->{default})) {
-           $defaulttxt = "   (default=$dv)";
-       }
-       my $tmp = sprintf "  %-10s %s$defaulttxt\n", $display_name, "$type";
-       my $indend = "             ";
 
-       $res .= Text::Wrap::wrap('', $indend, ($tmp));
-       $res .= "\n",
-       $res .= Text::Wrap::wrap($indend, $indend, ($descr)) . "\n\n";
+       if ($format eq 'asciidoc') {
+           $res .= "[horizontal]\n";
+           $res .= "`$display_name`:: `$type` ";
+           if (defined(my $dv = $phash->{default})) {
+               $res .= "(default=`$dv`)";
+           }
+           $res .= "\n+\n";
+           $res .= Text::Wrap::wrap('', '', ($descr)) . "\n";
+
+           if (my $req = $phash->{requires}) {
+           #if (my $req = 'test') {
+               my $tmp .= ref($req) ? join(', ', @$req) : $req;
+               $res .= "+\nNOTE: Requires option(s): `$tmp`\n";
+           }
+           $res .= "\n";
+       } else {
+           my $defaulttxt = '';
+           if (defined(my $dv = $phash->{default})) {
+               $defaulttxt = "   (default=$dv)";
+           }
+           my $tmp = sprintf "  %-10s %s$defaulttxt\n", $display_name, "$type";
+           my $indend = "             ";
+
+           $res .= Text::Wrap::wrap('', $indend, ($tmp));
+           $res .= "\n",
+           $res .= Text::Wrap::wrap($indend, $indend, ($descr)) . "\n\n";
 
-       if (my $req = $phash->{requires}) {
-           my $tmp = "Requires option(s): ";
-           $tmp .= ref($req) ? join(', ', @$req) : $req;
-           $res .= Text::Wrap::wrap($indend, $indend, ($tmp)). "\n\n";
+           if (my $req = $phash->{requires}) {
+               my $tmp = "Requires option(s): ";
+               $tmp .= ref($req) ? join(', ', @$req) : $req;
+               $res .= Text::Wrap::wrap($indend, $indend, ($tmp)). "\n\n";
+           }
        }
 
        return $res;
@@ -515,17 +533,26 @@ sub usage_str {
        }
     } 
 
-    $out .= "USAGE: " if $format ne 'short';
-
-    $out .= "$prefix $args";
-
-    $out .= $opts ? " [OPTIONS]\n" : "\n";
+    if ($format eq 'asciidoc') {
+       $out .= "*${prefix}*";
+       $out .= " `$args`" if $args;
+       $out .= $opts ? " `[OPTIONS]`\n" : "\n";
+    } else {
+       $out .= "USAGE: " if $format ne 'short';
+       $out .= "$prefix $args";
+       $out .= $opts ? " [OPTIONS]\n" : "\n";
+    }
 
     return $out if $format eq 'short';
 
-    if ($info->{description} && $format eq 'full') {
-       my $desc = Text::Wrap::wrap('  ', '  ', ($info->{description}));
-       $out .= "\n$desc\n\n";
+    if ($info->{description}) {
+       if ($format eq 'asciidoc') {
+           my $desc = Text::Wrap::wrap('', '', ($info->{description}));
+           $out .= "\n$desc\n\n";
+       } elsif ($format eq 'full') {
+           my $desc = Text::Wrap::wrap('  ', '  ', ($info->{description}));
+           $out .= "\n$desc\n\n";
+       }
     }
 
     $out .= $argdescr if $argdescr;