This patch adds support to cobbler for --helpbash in the sub modules.

Then the bash-completion script takes advantage of the --helpbash
command to generate completions

diff --git a/cobbler/commands.py b/cobbler/commands.py
index a774360..a3e3994 100644
--- a/cobbler/commands.py
+++ b/cobbler/commands.py
@@ -172,6 +172,21 @@ class CobblerFunction:
         """
         pass

+    def helpbash(self, parser, args):
+        """
+        Print out the arguments in an easily parseable format
+        """
+        option_list = []
+        for sub in self.subcommands():
+            if sub.__str__() in args:
+                option_list = []
+                break
+            option_list.append(sub.__str__())
+        for opt in parser.option_list:
+            option_list.extend(opt.__str__().split('/'))
+        print ' '.join(option_list)
+
+
     def parse_args(self,args):
         """
         Processes arguments, called prior to run ... do not override.
@@ -185,7 +200,9 @@ class CobblerFunction:
                 break
         p = optparse.OptionParser(usage="cobbler %s [ARGS]" % accum)
         self.add_options(p, args)
-
+        if "--helpbash" in args:
+            self.helpbash(p, args)
+            sys.exit(0)
         # if using subcommands, ensure one and only one is used
         subs = self.subcommands()
         if len(subs) > 0:
@@ -199,7 +216,6 @@ class CobblerFunction:
                 for x in subs:
                     print "cobbler %s %s [ARGS|--help]" %
(self.command_name(), x)
                 sys.exit(1)
-
         (self.options, self.args) = p.parse_args(args)
         return True

diff --git a/config/cobbler_bash b/config/cobbler_bash
index c4f7864..0b1dd8c 100644
--- a/config/cobbler_bash
+++ b/config/cobbler_bash
@@ -24,12 +24,35 @@

 _cobbler()
 {
-    local cur prev
+    local cur prev special specialsub SPECIAL_CMDS SPECIALSUB_CMDS

     COMPREPLY=()
     cur=${COMP_WORDS[COMP_CWORD]}
     prev=${COMP_WORDS[COMP_CWORD-1]}

+    # Let's only run it once
+    SPECIAL_CMDS=`cobbler --helpbash | sed "s/ /|/g"`
+    for (( i=0; i < [EMAIL PROTECTED]; i++ )); do
+           if [[ ${COMP_WORDS[i]} == @(${SPECIAL_CMDS}) ]]; then
+                   special=${COMP_WORDS[i]}
+           fi
+    done
+
+    if [ -n "$special" ]
+    then
+       # Take care of sub commands
+       SPECIALSUB_CMDS=`cobbler $special --helpbash | sed "s/-[^ ]\+//g;s/
*$//" | tr -s " " | sed "s/ /|/g"`
+       for (( i=0; i < [EMAIL PROTECTED]; i++ )); do
+               # We want to remove any -* commands from the list, because we 
are
trying to figure out if we have a subcommand
+               if [[ ${COMP_WORDS[i]} == @(${SPECIALSUB_CMDS}) ]]; then
+                       specialsub=${COMP_WORDS[i]}
+               fi
+       done
+
+       COMPREPLY=( $( compgen -W '$( cobbler $special $specialsub
--helpbash )' -- $cur ) )
+       return 0
+    fi
+
     case $cur in
        --*)
            COMPREPLY=( $( compgen -W 'help' -- $cur ) )
@@ -45,9 +68,7 @@ _cobbler()

     case $args in
        1)
-           COMPREPLY=( $( compgen -W 'buildiso check distro import list \
-           profile replicate repo report reposync status sync system \
-           validateks' -- $cur ) )
+           COMPREPLY=( $( compgen -W '$( cobbler --helpbash )' -- $cur ) )
            ;;
        *)
            case $prev in
diff --git a/cobbler/commands.py b/cobbler/commands.py
index a774360..a3e3994 100644
--- a/cobbler/commands.py
+++ b/cobbler/commands.py
@@ -172,6 +172,21 @@ class CobblerFunction:
         """
         pass
 
+    def helpbash(self, parser, args):
+        """
+        Print out the arguments in an easily parseable format
+        """
+        option_list = []
+        for sub in self.subcommands():
+            if sub.__str__() in args:
+                option_list = []
+                break
+            option_list.append(sub.__str__())
+        for opt in parser.option_list:
+            option_list.extend(opt.__str__().split('/'))
+        print ' '.join(option_list)
+
+
     def parse_args(self,args):
         """
         Processes arguments, called prior to run ... do not override.
@@ -185,7 +200,9 @@ class CobblerFunction:
                 break
         p = optparse.OptionParser(usage="cobbler %s [ARGS]" % accum)
         self.add_options(p, args)
-
+        if "--helpbash" in args:
+            self.helpbash(p, args)
+            sys.exit(0)
         # if using subcommands, ensure one and only one is used
         subs = self.subcommands()
         if len(subs) > 0:
@@ -199,7 +216,6 @@ class CobblerFunction:
                 for x in subs: 
                     print "cobbler %s %s [ARGS|--help]" % (self.command_name(), x)
                 sys.exit(1)    
-
         (self.options, self.args) = p.parse_args(args)
         return True
 
diff --git a/config/cobbler_bash b/config/cobbler_bash
index c4f7864..0b1dd8c 100644
--- a/config/cobbler_bash
+++ b/config/cobbler_bash
@@ -24,12 +24,35 @@
 
 _cobbler()
 {
-    local cur prev
+    local cur prev special specialsub SPECIAL_CMDS SPECIALSUB_CMDS
 
     COMPREPLY=()
     cur=${COMP_WORDS[COMP_CWORD]}
     prev=${COMP_WORDS[COMP_CWORD-1]}
 
+    # Let's only run it once
+    SPECIAL_CMDS=`cobbler --helpbash | sed "s/ /|/g"`
+    for (( i=0; i < [EMAIL PROTECTED]; i++ )); do
+	    if [[ ${COMP_WORDS[i]} == @(${SPECIAL_CMDS}) ]]; then
+		    special=${COMP_WORDS[i]}
+	    fi
+    done
+
+    if [ -n "$special" ]
+    then
+	# Take care of sub commands
+	SPECIALSUB_CMDS=`cobbler $special --helpbash | sed "s/-[^ ]\+//g;s/ *$//" | tr -s " " | sed "s/ /|/g"`
+	for (( i=0; i < [EMAIL PROTECTED]; i++ )); do
+		# We want to remove any -* commands from the list, because we are trying to figure out if we have a subcommand
+		if [[ ${COMP_WORDS[i]} == @(${SPECIALSUB_CMDS}) ]]; then
+			specialsub=${COMP_WORDS[i]}
+		fi
+	done
+
+	COMPREPLY=( $( compgen -W '$( cobbler $special $specialsub --helpbash )' -- $cur ) )
+	return 0
+    fi
+
     case $cur in
 	--*)
 	    COMPREPLY=( $( compgen -W 'help' -- $cur ) )
@@ -45,9 +68,7 @@ _cobbler()
 
     case $args in
 	1)
-	    COMPREPLY=( $( compgen -W 'buildiso check distro import list \
-	    profile replicate repo report reposync status sync system \
-	    validateks' -- $cur ) )
+	    COMPREPLY=( $( compgen -W '$( cobbler --helpbash )' -- $cur ) )
 	    ;;
 	*)
 	    case $prev in
_______________________________________________
cobbler mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/cobbler

Reply via email to