[cig-commits] r13774 - in cs/cigma/trunk: . src tests/system

luis at geodynamics.org luis at geodynamics.org
Wed Dec 17 02:33:52 PST 2008


Author: luis
Date: 2008-12-17 02:33:52 -0800 (Wed, 17 Dec 2008)
New Revision: 13774

Added:
   cs/cigma/trunk/completion.sh
Modified:
   cs/cigma/trunk/quick.sh
   cs/cigma/trunk/src/cli_application.cpp
   cs/cigma/trunk/src/cli_application.h
   cs/cigma/trunk/src/cli_base_cmd.cpp
   cs/cigma/trunk/src/cli_base_cmd.h
   cs/cigma/trunk/src/cli_compare_cmd.cpp
   cs/cigma/trunk/src/cli_eval_cmd.cpp
   cs/cigma/trunk/src/cli_extract_cmd.cpp
   cs/cigma/trunk/src/cli_list_cmd.cpp
   cs/cigma/trunk/tests/system/200-eval.sh
Log:
Simple bash command line completion through --commands and --list-options

Added: cs/cigma/trunk/completion.sh
===================================================================
--- cs/cigma/trunk/completion.sh	                        (rev 0)
+++ cs/cigma/trunk/completion.sh	2008-12-17 10:33:52 UTC (rev 13774)
@@ -0,0 +1,32 @@
+#!/bin/bash
+
+#
+# Based on darcs bash completion 
+#
+_cigma()
+{
+    local cur
+
+    cur=${COMP_WORDS[COMP_CWORD]}
+
+    COMPREPLY=()
+
+    if (($COMP_CWORD == 1)); then
+        COMPREPLY=( $( cigma --commands | grep "^$cur" ) )
+        return 0
+    fi
+
+    local IFS=$'\n'
+    COMPREPLY=( $( cigma ${COMP_WORDS[1]} --list-options | grep "^${cur//./\\.}") )
+
+    local i=${#COMPREPLY[*]}
+    local colonprefixes=${cur%"${cur##*:}"}
+    while [ $((--i)) -ge 0 ]; do
+        COMPREPLY[$i]=`printf %q "${COMPREPLY[$i]}"`
+        COMPREPLY[$i]=${COMPREPLY[$i]#"$colonprefixes"}
+    done
+    return 0
+
+}
+complete -F _cigma -o default cigma
+

Modified: cs/cigma/trunk/quick.sh
===================================================================
--- cs/cigma/trunk/quick.sh	2008-12-17 10:33:49 UTC (rev 13773)
+++ cs/cigma/trunk/quick.sh	2008-12-17 10:33:52 UTC (rev 13774)
@@ -144,7 +144,7 @@
     A=$D/brick1
     B=$D/brick2
     cigma eval \
-        --function  'test.cube' \
+        --field     'test.cube' \
         --points    $A/brick1.h5:/coordinates  \
         --output    cubefn.h5:/values \
         $*
@@ -157,7 +157,7 @@
     A=$D/brick1
     B=$D/brick2
     cigma eval \
-        --function  'test.cube' \
+        --field     'test.cube' \
         --points    qpts.h5:/points \
         --output    qvals.h5:/values \
         $*

Modified: cs/cigma/trunk/src/cli_application.cpp
===================================================================
--- cs/cigma/trunk/src/cli_application.cpp	2008-12-17 10:33:49 UTC (rev 13773)
+++ cs/cigma/trunk/src/cli_application.cpp	2008-12-17 10:33:52 UTC (rev 13774)
@@ -75,17 +75,39 @@
 
 static bool helpflag(const char *s)
 {
-    return (strcmp(s, "help") == 0) ||
-           (strcmp(s, "--help") == 0) ||
-           (strcmp(s, "-h") == 0);
+    return (strncmp(s, "help"  , 5) == 0) ||
+           (strncmp(s, "--help", 7) == 0) ||
+           (strncmp(s, "-h"    , 3) == 0);
 }
 
 static bool versionflag(const char *s)
 {
-    return (strcmp(s, "version") == 0) ||
-           (strcmp(s, "--version") == 0);
+    return (strncmp(s, "version"  ,  8) == 0) ||
+           (strncmp(s, "--version", 10) == 0);
 }
 
+static bool commandsflag(const char *s)
+{
+    return (strncmp(s, "--commands", 11) == 0);
+}
+
+static bool listoptionsflag(const char *s)
+{
+    return (strncmp(s, "--list-options", 15) == 0);
+}
+
+void Application::showCommands()
+{
+    CmdNames::iterator i;
+    for (i = names.begin(); i != names.end(); ++i)
+    {
+        cout << *i << endl;
+    }
+    cout << "help" << endl;
+    //cout << "--help" << endl;
+    //cout << "--version" << endl;
+}
+
 void Application::showHelp(int argc, char *argv[])
 {
     /*
@@ -121,11 +143,21 @@
         }
         else
         {
-            /* Complain, since argv[2] was not a valid subcommand */
-            cout << "Unknown "
-                 << ((argv[2][0] == '-') ? "option" : "command")
-                 << ": '" << argv[2] << "'"
-                 << endl;
+            if (listoptionsflag(argv[2]))
+            {
+                showCommands();
+                exit(1);
+            }
+            else if (helpflag(argv[2]))
+            {
+                showHelp();
+                exit(1);
+            }
+            else if (argv[2][0] != '-')
+            {
+                /* Complain, since argv[2] was not a valid subcommand */
+                cout << "Unknown command: '" << argv[2] << "'" << endl;
+            }
             printquickhelp();
         }
     }
@@ -217,9 +249,21 @@
             }
             else if (versionflag(argv[1]))
             {
+                if (argc > 2)
+                {
+                    if (listoptionsflag(argv[2]))
+                    {
+                        exit(1);
+                    }
+                }
                 this->showVersion();
                 return 0;
             }
+            else if (commandsflag(argv[1]))
+            {
+                this->showCommands();
+                return 0;
+            }
             else
             {
                 cmd = this->getCommand(argv[1]);

Modified: cs/cigma/trunk/src/cli_application.h
===================================================================
--- cs/cigma/trunk/src/cli_application.h	2008-12-17 10:33:49 UTC (rev 13773)
+++ cs/cigma/trunk/src/cli_application.h	2008-12-17 10:33:52 UTC (rev 13774)
@@ -60,6 +60,11 @@
      */
     void showVersion();
 
+    /**
+     * Internal method for displaying the list of available commands.
+     */
+    void showCommands();
+
 public:
     /**
      * Main method for dispatching to the appropriate command,

Modified: cs/cigma/trunk/src/cli_base_cmd.cpp
===================================================================
--- cs/cigma/trunk/src/cli_base_cmd.cpp	2008-12-17 10:33:49 UTC (rev 13773)
+++ cs/cigma/trunk/src/cli_base_cmd.cpp	2008-12-17 10:33:52 UTC (rev 13774)
@@ -1,5 +1,7 @@
 #include "cli_base_cmd.h"
+#include "tri_logger.hpp"
 #include <iostream>
+#include <vector>
 
 using namespace std;
 using namespace cigma;
@@ -58,21 +60,72 @@
     other.add_options()
         ("help,h", "produce help message")
         ("verbose,v", "produce verbose output")
-        ("debug,D", "print debugging information") // XXX: make this a hidden option?
         ;
+
+    // hidden options
+    hidden.add_options()
+        ("debug,D", "print debugging information")
+        ("list-options", "print list of options for command")
+        ;
 }
 
 void BaseCmd::parseOptions(int argc, char *argv[])
 {
     po::options_description all;
-    all.add(opts).add(opts2).add(other);
+    all.add(opts).add(opts2).add(other).add(hidden);
     po::store(po::command_line_parser(argc, argv).options(all).positional(args).run(), vm);
 }
 
+static void printopts(const po::options_description& o)
+{
+    typedef boost::shared_ptr<po::option_description> od;
+    
+    vector<od> x = o.options();
+    vector<od>::iterator i;
+
+    for (i = x.begin(); i != x.end(); ++i)
+    {
+        cout << "--" << (*i)->long_name();
+        cout << endl;
+    }
+}
+
+void BaseCmd::listOptions()
+{
+    printopts(opts);
+    printopts(opts2);
+    printopts(other);
+}
+
 void BaseCmd::configure()
 {
     po::notify(vm);
+
+    if (!vm.count("debug"))
+    {
+        TRI_LOG_OFF();
+    }
+
+    if (vm.count("help"))
+    {
+        printUsage();
+        exit(1);
+    }
+
+    if (vm.count("list-options"))
+    {
+        listOptions();
+        exit(1);
+    }
 }
 
+int BaseCmd::run()
+{
+    if (vm.count("help"))
+    {
+        printUsage();
+        exit(1);
+    }
+}
 
 // ----------------------------------------------------------------------------

Modified: cs/cigma/trunk/src/cli_base_cmd.h
===================================================================
--- cs/cigma/trunk/src/cli_base_cmd.h	2008-12-17 10:33:49 UTC (rev 13773)
+++ cs/cigma/trunk/src/cli_base_cmd.h	2008-12-17 10:33:52 UTC (rev 13774)
@@ -33,12 +33,22 @@
     virtual void parseOptions(int argc, char *argv[]);
 
     /**
+     * Provide a list of options for this command.
+     */
+    void listOptions();
+
+    /**
      * Provide a default implementation of Command::configure.
      * We do nothing more than simply finalize the values in the
      * variables_map object by calling po::notify(vm).
      */
     virtual void configure();
 
+    /**
+     * Provide a default implementation of Command::run.
+     */
+    virtual int run();
+
 public:
     /**
      * Print out a usage message based on the available information.
@@ -52,6 +62,7 @@
     boost::program_options::options_description opts;
     boost::program_options::options_description opts2;
     boost::program_options::options_description other;
+    boost::program_options::options_description hidden;
     boost::program_options::positional_options_description args;
     boost::program_options::variables_map vm;
 };

Modified: cs/cigma/trunk/src/cli_compare_cmd.cpp
===================================================================
--- cs/cigma/trunk/src/cli_compare_cmd.cpp	2008-12-17 10:33:49 UTC (rev 13773)
+++ cs/cigma/trunk/src/cli_compare_cmd.cpp	2008-12-17 10:33:52 UTC (rev 13774)
@@ -67,16 +67,6 @@
 {
     BaseCmd::configure();
 
-    if (vm.count("help"))
-    {
-        return;
-    }
-
-    if (!vm.count("debug"))
-    {
-        TRI_LOG_OFF();
-    }
-
     TRI_LOG_STR("CompareCmd::configure()");
 
     if (vm.count("verbose"))
@@ -152,11 +142,7 @@
 
 int CompareCmd::run()
 {
-    if (vm.count("help"))
-    {
-        printUsage();
-        return 1;
-    }
+    BaseCmd::run();
 
     TRI_LOG_STR("CompareCmd::run()");
     TRI_LOG(outputfile);

Modified: cs/cigma/trunk/src/cli_eval_cmd.cpp
===================================================================
--- cs/cigma/trunk/src/cli_eval_cmd.cpp	2008-12-17 10:33:49 UTC (rev 13773)
+++ cs/cigma/trunk/src/cli_eval_cmd.cpp	2008-12-17 10:33:52 UTC (rev 13774)
@@ -33,7 +33,7 @@
 
     opts_section = "Eval Options: ";
     opts.add_options()
-        ("function,f", po::value<string>(&function), "Function <FUNC> to evaluate")
+        ("field,f", po::value<string>(&function), "Function <FUNC> to evaluate")
         ("points,p", po::value<string>(&points), "Input quadrature points <QPTS-IN>")
         ("output,o", po::value<string>(&values), "Output values of function <QVALS-OUT>")
         ;
@@ -53,16 +53,6 @@
 {
     BaseCmd::configure();
 
-    if (vm.count("help"))
-    {
-        return;
-    }
-
-    if (!vm.count("debug"))
-    {
-        TRI_LOG_OFF();
-    }
-
     TRI_LOG_STR("EvalCmd::configure()");
 
     if (vm.count("verbose"))
@@ -70,15 +60,15 @@
         op.verbose = true;
     }
 
-    if (!vm.count("function"))
+    if (!vm.count("field"))
     {
-        string msg("No function was specified (use --function option)");
+        string msg("No function was specified (use --field option)");
         throw cigma::Exception("EvalCmd::configure", msg, 1);
     }
 
     if (!vm.count("points"))
     {
-        string msg("No points were specified (use --points option)");
+        string msg("No evaluation points were specified (use --points option)");
         throw cigma::Exception("EvalCmd::configure", msg, 1);
     }
 
@@ -105,11 +95,7 @@
 
 int EvalCmd::run()
 {
-    if (vm.count("help"))
-    {
-        printUsage();
-        return 1;
-    }
+    BaseCmd::run();
 
     TRI_LOG_STR("EvalCmd::run()");
     TRI_LOG(function);

Modified: cs/cigma/trunk/src/cli_extract_cmd.cpp
===================================================================
--- cs/cigma/trunk/src/cli_extract_cmd.cpp	2008-12-17 10:33:49 UTC (rev 13773)
+++ cs/cigma/trunk/src/cli_extract_cmd.cpp	2008-12-17 10:33:52 UTC (rev 13774)
@@ -60,16 +60,6 @@
 {
     BaseCmd::configure();
 
-    if (vm.count("help"))
-    {
-        return;
-    }
-
-    if (!vm.count("debug"))
-    {
-        TRI_LOG_OFF();
-    }
-
     TRI_LOG_STR("ExtractCmd::configure()");
 
     if (vm.count("verbose"))
@@ -106,11 +96,7 @@
 
 int ExtractCmd::run()
 {
-    if (vm.count("help"))
-    {
-        this->printUsage();
-        return 1;
-    }
+    BaseCmd::run();
 
     TRI_LOG_STR("ExtractCmd::run()");
     TRI_LOG(mesh);

Modified: cs/cigma/trunk/src/cli_list_cmd.cpp
===================================================================
--- cs/cigma/trunk/src/cli_list_cmd.cpp	2008-12-17 10:33:49 UTC (rev 13773)
+++ cs/cigma/trunk/src/cli_list_cmd.cpp	2008-12-17 10:33:52 UTC (rev 13774)
@@ -38,11 +38,11 @@
 
 void ListCmd::defineOptions()
 {
+    BaseCmd::defineOptions();
+
     opts_section = "List Options: ";
 
     opts.add_options()
-        ("help,h", "produce help message")
-        ("debug,D", "print debugging information")
         ("input-file", po::value<string>(&op.filename), "input file")
         ;
 
@@ -53,11 +53,6 @@
 {
     BaseCmd::configure();
 
-    if (!vm.count("debug"))
-    {
-        TRI_LOG_OFF();
-    }
-
     //
     // Validate input data as much as possible, i.e.,
     //  - Make sure the file exists.
@@ -82,11 +77,7 @@
 
 int ListCmd::run()
 {
-    if (vm.count("help"))
-    {
-        this->printUsage();
-        return 1;
-    }
+    BaseCmd::run();
 
     if (op.filename == "")
     {

Modified: cs/cigma/trunk/tests/system/200-eval.sh
===================================================================
--- cs/cigma/trunk/tests/system/200-eval.sh	2008-12-17 10:33:49 UTC (rev 13773)
+++ cs/cigma/trunk/tests/system/200-eval.sh	2008-12-17 10:33:52 UTC (rev 13774)
@@ -5,6 +5,6 @@
 ./cigma eval \
     --output=$D/system/out/200.h5:/values \
     --points=$D/data/brick1/brick1.h5:/coordinates \
-    --function=test.cube \
+    --field=test.cube \
     $*
 



More information about the CIG-COMMITS mailing list