Make aliases be auto-detected and inserted into help texts.
authorDavor Ocelic <docelic@crystallabs.io>
Tue, 30 Apr 2019 01:10:00 +0200
changeset 111 252611c8d283
parent 110 dbceec51da98
child 112 32f396697fb3
Make aliases be auto-detected and inserted into help texts.
shelldap
--- a/shelldap	Tue Apr 30 01:01:19 2019 +0200
+++ b/shelldap	Tue Apr 30 01:10:00 2019 +0200
@@ -304,8 +304,6 @@
     copy uid=mahlon ou=Others,dc=example,o=company
     copy uid=mahlon,ou=People,dc=example,o=company uid=mahlon,ou=Others,dc=example,o=company
 
-aliased to: cp
-
 =head2 create
 
 Create an entry.
@@ -321,8 +319,6 @@
 
     create top person organizationalPerson inetOrgPerson posixAccount
 
-aliased to: touch
-
 =head2 delete
 
 Remove an entry.
@@ -335,8 +331,6 @@
     delete uid=ma*
     rm -v uid=mahlon,ou=People,dc=example,o=company l=office
 
-aliased to: rm
-
 =head2 edit
 
 Edit an entry in an external editor.
@@ -347,8 +341,6 @@
 
     edit uid=mahlon
 
-aliased to: vi
-
 =head2 env
 
 Print values of configurable shelldap variables.
@@ -366,8 +358,6 @@
     grep uid=mahlon ou=People
     grep -r (&(uid=mahlon)(objectClass=*))
 
- aliased to: search
-
 =head2 inspect
 
 View schema and flags for an entry or objectClass.
@@ -394,8 +384,6 @@
 
 Globbing is supported.
 
-aliased to: ls
-
     ls -l
     ls -lR uid=mahlon
     list uid=m*
@@ -425,8 +413,6 @@
 
 Usage is identical to B<copy>.
 
-aliased to: mv
-
 =head2 passwd
 
 Change user password.
@@ -2413,6 +2399,7 @@
 	local $| = 1;
 	my %aliases;
 
+	# In first pass, only identify aliases.
 	while(my($cmd, $data) = each %cmd_map ) {
 		# If command is an alias, it is enough to mark it as such.
 		if( $$data[0]) {
@@ -2420,7 +2407,12 @@
 			push @{$aliases{$$data[0]}}, $cmd;
 
 		# If it is a real command, let's do more work.
-		} else {
+		}
+	}
+
+	# In second pass, deal with non-aliases.
+	while(my($cmd, $data) = each %cmd_map ) {
+		if( !$$data[0]) {
 			# If completer is defined, set it.
 			if( $$data[1]) {
 				my $comp_sub = "comp_$cmd";
@@ -2438,6 +2430,10 @@
 				$summary =~ s/\s+/ /s;
 			}
 			my $help = join "\n", @pod;
+			if( $aliases{$cmd}) {
+				local $" = ', ';
+				$help .= "\n\n    Aliases: @{$aliases{$cmd}}\n"
+			}
 
 			my $helpfunc = sub { "$help\n" };
 			*{"help_$cmd"} = \&$helpfunc;
@@ -2447,6 +2443,7 @@
 		}
 	}
 
+	# In third pass, actually register found aliases.
 	while(my($cmd,$aliases) = each %aliases) {
 		my $aliasfunc = sub { @$aliases };
 		*{"alias_$cmd"} = \&$aliasfunc;