[Bps-public-commit] r14689 - in sd: trunk/lib/App/SD/CLI

spang at bestpractical.com spang at bestpractical.com
Thu Jul 31 07:48:21 EDT 2008


Author: spang
Date: Thu Jul 31 07:48:21 2008
New Revision: 14689

Modified:
   sd/   (props changed)
   sd/trunk/lib/App/SD/CLI/Command.pm

Log:
 r47464 at loki:  spang | 2008-07-31 12:42:53 +0100
 make get_content more flexible


Modified: sd/trunk/lib/App/SD/CLI/Command.pm
==============================================================================
--- sd/trunk/lib/App/SD/CLI/Command.pm	(original)
+++ sd/trunk/lib/App/SD/CLI/Command.pm	Thu Jul 31 07:48:21 2008
@@ -5,10 +5,22 @@
 =head2 get_content %args
 
 This is a helper routine for use in SD commands to enable getting records
-in different ways, such as from a file, on the commandline, or from an
+in different ways such as from a file, on the commandline, or from an
 editor. Returns the record content.
 
-Valid keys in %args are type and default_edit => bool.
+Valid keys in %args are type => str, default_edit => bool, and
+prefill_props => $props_hash_ref, props_order => $order_array_ref,
+footer => str, header => str.
+
+Specifying props with prefill_props allows you to present lists of
+key/value pairs (with possible default values) for a user to fill in.
+If you need a specific ordering of props, specify it with
+C<props_order>. Specifying C<header> and/or C<footer> allows you to
+print some optional text before and/or after the list of props.
+
+Note that you will have to post-process C<$content> from the routine
+calling C<get_content> in order to extract the keys and values from
+the returned text.
 
 =cut
 
@@ -20,10 +32,25 @@
     if (my $file = file($self->delete_arg('file'))) {
         $content = $file->slurp();
         $self->set_prop(name => $file->basename);
-    } elsif ($content = $self->delete_arg('content')) {
+    } elsif ($content = $self->delete_prop('content')) {
 
     } elsif ($args{default_edit} || $self->has_arg('edit')) {
-        $content = $self->edit_text('');
+        my $text = '';
+        if (my $header = $args{header}) {
+            $text .= $header;
+        }
+        if (my $props = $args{prefill_props}) {
+            my @ordering = (my $props_order = $args{props_order}) ?
+                                @$props_order : keys %$props;
+            $text .= join "\n", map { "$_: $props->{$_}" } @ordering;
+        }
+        if (my $footer = $args{footer}) {
+            $text .= $footer;
+        }
+        $content = $self->edit_text($text);
+        # user aborted their text editor without changing anything; signify
+        # this to the caller by returning nothing
+        $content = '' if $content eq $text;
     } else {
         print "Please type your $args{type} and press ctrl-d.\n";
         $content = do { local $/; <> };



More information about the Bps-public-commit mailing list