[cura-tools] master: bump release version to v0.6 (9e28001)
by phatina@fedoraproject.org
Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
>---------------------------------------------------------------
commit 9e28001dfd7b49eb3168d9a60ebdcc520f59cf6e
Author: Peter Hatina <phatina(a)redhat.com>
Date: Wed Jul 31 15:25:03 2013 +0200
bump release version to v0.6
>---------------------------------------------------------------
cli/lmi/shell/__init__.py | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/cli/lmi/shell/__init__.py b/cli/lmi/shell/__init__.py
index acd65a7..8a20af5 100644
--- a/cli/lmi/shell/__init__.py
+++ b/cli/lmi/shell/__init__.py
@@ -17,7 +17,7 @@
LMIShell
"""
-__version__ = "0.5"
+__version__ = "0.6"
from LMIConsole import LMIConsole
from LMIExceptions import *
10 years, 9 months
[cura-tools] master: add log options description into man page generator (bc26a75)
by phatina@fedoraproject.org
Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
>---------------------------------------------------------------
commit bc26a75106dd7b0d2085f307007688df76682349
Author: Peter Hatina <phatina(a)redhat.com>
Date: Wed Jul 31 15:24:23 2013 +0200
add log options description into man page generator
>---------------------------------------------------------------
util/man-page-generator/generate-man-page | 27 +++++++++++++++++++++++----
1 files changed, 23 insertions(+), 4 deletions(-)
diff --git a/util/man-page-generator/generate-man-page b/util/man-page-generator/generate-man-page
index 1c039da..043af64 100755
--- a/util/man-page-generator/generate-man-page
+++ b/util/man-page-generator/generate-man-page
@@ -58,7 +58,7 @@ man_page_header = \
lmishell \\- (non)interactive WBEM client and interpreter.
.SH SYNOPSIS
.B lmishell
-[\\fB\\-i\\fR] [\\fB\\-\\-interact\\fR] script [script-options]
+[options] script [script-options]
.SH DESCRIPTION
lmishell provides a (non)interactive or interactive way how to access CIM
objects provided by OpenPegasus or sblim-sfcb CIMOM.
@@ -70,10 +70,29 @@ classes are added to fulfill wrapper pattern and expose only those methods,
which are necessary for the purpose of a shell. Following scheme depicts a
structure of the OpenLMI Shell.
.SH OPTIONS
-\\fB\\-h\\fR \\fB\\-\\-help\\fR Print summary of usage, command line options and exit.
+The options may be given in any order before the first positional argument,
+which stands for the script name.
.TP
-\\fB-h\\fR \\fB\\-\\-interact\\fR Enter interactive mode, when the script passed
-as the first positional argument is executed."""
+\\fB\\-h\\fR \\fB\\-\\-help\\fR
+Print summary of usage, command line options and exit.
+.TP
+\\fB\\-i\\fR \\fB\\-\\-interact\\fR
+Enter interactive mode, when the script passed as the first positional argument
+is executed.
+.TP
+\\fB\\-v\\fR \\fB\\-\\-verbose\\fR
+Print log messages to stderr.
+.TP
+\\fB\\-m\\fR \\fB\\-\\-more\\-verbose\\fR
+Print all log messages to stderr.
+.TP
+\\fB\\-q\\fR \\fB\\-\\-quiet\\fR
+Do not print any log messages to stderr.
+.P
+By default, LMIShell prints out log messages with Error severity. Options
+\\fB\\-v\\fR, \\fB\\-m\\fR and \\fB\\-q\\fR are mutually exclusive and
+can not be used together.
+"""
# The man page static footer containing Authors section.
man_page_footer = \
""".SH "AUTHORS"
10 years, 9 months
[cura-tools] master: add log options description into man page (b902a9e)
by phatina@fedoraproject.org
Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
>---------------------------------------------------------------
commit b902a9e213820f29d5e59e02267f3aafe203b9dd
Author: Peter Hatina <phatina(a)redhat.com>
Date: Wed Jul 31 15:23:52 2013 +0200
add log options description into man page
>---------------------------------------------------------------
man/lmishell.1 | 27 +++++++++++++++++++++++----
1 files changed, 23 insertions(+), 4 deletions(-)
diff --git a/man/lmishell.1 b/man/lmishell.1
index c0b4eb5..3f83e7a 100644
--- a/man/lmishell.1
+++ b/man/lmishell.1
@@ -3,7 +3,7 @@
lmishell \- (non)interactive WBEM client and interpreter.
.SH SYNOPSIS
.B lmishell
-[\fB\-i\fR] [\fB\-\-interact\fR] script [script-options]
+[options] script [script-options]
.SH DESCRIPTION
lmishell provides a (non)interactive or interactive way how to access CIM
objects provided by OpenPegasus or sblim-sfcb CIMOM.
@@ -15,10 +15,29 @@ classes are added to fulfill wrapper pattern and expose only those methods,
which are necessary for the purpose of a shell. Following scheme depicts a
structure of the OpenLMI Shell.
.SH OPTIONS
-\fB\-h\fR \fB\-\-help\fR Print summary of usage, command line options and exit.
+The options may be given in any order before the first positional argument,
+which stands for the script name.
.TP
-\fB-h\fR \fB\-\-interact\fR Enter interactive mode, when the script passed
-as the first positional argument is executed.
+\fB\-h\fR \fB\-\-help\fR
+Print summary of usage, command line options and exit.
+.TP
+\fB\-i\fR \fB\-\-interact\fR
+Enter interactive mode, when the script passed as the first positional argument
+is executed.
+.TP
+\fB\-v\fR \fB\-\-verbose\fR
+Print log messages to stderr.
+.TP
+\fB\-m\fR \fB\-\-more\-verbose\fR
+Print all log messages to stderr.
+.TP
+\fB\-q\fR \fB\-\-quiet\fR
+Do not print any log messages to stderr.
+.P
+By default, LMIShell prints out log messages with Error severity. Options
+\fB\-v\fR, \fB\-m\fR and \fB\-q\fR are mutually exclusive and
+can not be used together.
+
.SH HOW TO USE THE SHELL
The following text covers basic use-cases.
10 years, 9 months
[cura-tools] master: fix default namespace for indications in LMIMethod to root/interop (221fd12)
by phatina@fedoraproject.org
Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
>---------------------------------------------------------------
commit 221fd120862203d30b268d78fbe86206e0a38b86
Author: Peter Hatina <phatina(a)redhat.com>
Date: Wed Jul 31 13:52:58 2013 +0200
fix default namespace for indications in LMIMethod to root/interop
>---------------------------------------------------------------
cli/lmi/shell/LMIMethod.py | 11 +++++++----
1 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/cli/lmi/shell/LMIMethod.py b/cli/lmi/shell/LMIMethod.py
index 98eff31..8067d5a 100644
--- a/cli/lmi/shell/LMIMethod.py
+++ b/cli/lmi/shell/LMIMethod.py
@@ -62,6 +62,9 @@ class LMIMethod(LMIBaseObject):
"LMI_SoftwareInstallationJob",
"LMI_NetworkJob"
)
+ # Default namespace where the indication subscriptions, used for synchronous
+ # method calls, will be registered.
+ _INDICATION_NAMESPACE = "root/interop"
# When performing a synchronous method call and using the polling method to
# get a job object status, the sleep time between 2 polls doubles if it is
# less than _POLLING_ADAPT_MAX_WAITING_TIME.
@@ -164,8 +167,8 @@ class LMIMethod(LMIBaseObject):
# Search for necessary static filter
filter_name = "LMI:%s:Changed" % job_inst.classname
- (cim_filters, _, err) = self._client._get_instances("CIM_IndicationFilter", "root/PG_InterOp",
- {"Name" : filter_name})
+ (cim_filters, _, err) = self._client._get_instances("CIM_IndicationFilter",
+ LMIMethod._INDICATION_NAMESPACE, {"Name" : filter_name})
if not cim_filters:
listener.stop()
errorstr = "Can not find proper CIM_IndicationFilter for this method call"
@@ -198,7 +201,7 @@ class LMIMethod(LMIBaseObject):
LMIMethod._INDICATION_DESTINATION_PORT, indication_name),
}
(cim_handler, _, err) = self._client._create_instance("CIM_IndicationHandlerCIMXML",
- "root/PG_InterOp", cim_handler_props)
+ LMIMethod._INDICATION_NAMESPACE, cim_handler_props)
if not cim_handler:
listener.stop()
errorstr = "Can not create CIM_IndicationHandlerCIMXML object"
@@ -210,7 +213,7 @@ class LMIMethod(LMIBaseObject):
"Handler" : cim_handler.path
}
(cim_subscription, _, err) = self._client._create_instance("CIM_IndicationSubscription",
- "root/PG_InterOp", cim_subscription_props)
+ LMIMethod._INDICATION_NAMESPACE, cim_subscription_props)
if not cim_subscription:
self._client._delete_instance(cim_handler)
listener.stop()
10 years, 9 months
[cura-tools] master: rename wrong constant tuple in LMIMethod to _INDICATION_JOB_CLASSNAMES (797b500)
by phatina@fedoraproject.org
Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
>---------------------------------------------------------------
commit 797b500e93282718a23b87c7d77cb64907ccbbb7
Author: Peter Hatina <phatina(a)redhat.com>
Date: Wed Jul 31 13:55:09 2013 +0200
rename wrong constant tuple in LMIMethod to _INDICATION_JOB_CLASSNAMES
>---------------------------------------------------------------
cli/lmi/shell/LMIMethod.py | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/cli/lmi/shell/LMIMethod.py b/cli/lmi/shell/LMIMethod.py
index 8067d5a..6b60462 100644
--- a/cli/lmi/shell/LMIMethod.py
+++ b/cli/lmi/shell/LMIMethod.py
@@ -57,7 +57,7 @@ class LMIMethod(LMIBaseObject):
_INDICATION_DESTINATION_PORT = 10240
# Job classes, which can be used for synchro method calls
# TODO: create a configuration option for the static filters' classnames
- _INDICATION_FILTER_CLASSNAMES = (
+ _INDICATION_JOB_CLASSNAMES = (
"LMI_StorageJob",
"LMI_SoftwareInstallationJob",
"LMI_NetworkJob"
@@ -357,7 +357,7 @@ class LMIMethod(LMIBaseObject):
)
# Check if we can perform synchronous method call
job = call_rparams["job"] if "job" in call_rparams else None
- can_perform_sync_call = job.classname in LMIMethod._INDICATION_FILTER_CLASSNAMES if job else False
+ can_perform_sync_call = job.classname in LMIMethod._INDICATION_JOB_CLASSNAMES if job else False
if self._sync_method and job and can_perform_sync_call:
# Synchronous method calls
job_inst = call_rparams['job'].to_instance()
10 years, 9 months
[cura-tools] master: pylint fixes (03f159b)
by phatina@fedoraproject.org
Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
>---------------------------------------------------------------
commit 03f159bc949e5609afd5312ac7ca9431c8451d1d
Author: Peter Hatina <phatina(a)redhat.com>
Date: Wed Jul 31 13:24:38 2013 +0200
pylint fixes
>---------------------------------------------------------------
cli/lmi/shell/LMIConnection.py | 2 ++
cli/lmi/shell/LMIConsole.py | 2 +-
cli/lmi/shell/LMIExceptions.py | 6 ------
cli/lmi/shell/LMIFormatter.py | 2 +-
cli/lmi/shell/LMIMethod.py | 1 -
5 files changed, 4 insertions(+), 9 deletions(-)
diff --git a/cli/lmi/shell/LMIConnection.py b/cli/lmi/shell/LMIConnection.py
index f46fa32..31e4513 100644
--- a/cli/lmi/shell/LMIConnection.py
+++ b/cli/lmi/shell/LMIConnection.py
@@ -17,11 +17,13 @@ import os
import sys
import atexit
import pywbem
+import readline
import urlparse
from LMIBaseClient import LMIBaseClient
from LMIShellClient import LMIShellClient
from LMINamespace import LMINamespaceRoot
+from LMIInstance import LMIInstance
from LMIReturnValue import LMIReturnValue
from LMISubscription import LMISubscription
diff --git a/cli/lmi/shell/LMIConsole.py b/cli/lmi/shell/LMIConsole.py
index 9176bf2..94f91fb 100644
--- a/cli/lmi/shell/LMIConsole.py
+++ b/cli/lmi/shell/LMIConsole.py
@@ -200,7 +200,7 @@ class LMIConsole(code.InteractiveConsole):
return
readline.read_history_file(self._history_file)
if self._history_length > 0 and readline.get_current_history_length() > self._history_length:
- readline.set_history_length(length)
+ readline.set_history_length(self._history_length)
readline.write_history_file(self._history_file)
readline.read_history_file(self._history_file)
diff --git a/cli/lmi/shell/LMIExceptions.py b/cli/lmi/shell/LMIExceptions.py
index 30b22a3..2bd4bda 100644
--- a/cli/lmi/shell/LMIExceptions.py
+++ b/cli/lmi/shell/LMIExceptions.py
@@ -73,9 +73,3 @@ class LMIFilterError(Exception):
"""
Raised, when there is a filter error present
"""
-
-class LMIHandlerNamePatternError(Exception):
- """
- Raised when the pattern string does not contain minimum
- replacable characters.
- """
diff --git a/cli/lmi/shell/LMIFormatter.py b/cli/lmi/shell/LMIFormatter.py
index e11f44f..5e139cc 100644
--- a/cli/lmi/shell/LMIFormatter.py
+++ b/cli/lmi/shell/LMIFormatter.py
@@ -113,7 +113,7 @@ class LMIFormatter(object):
"""
if "PAGER" in os.environ:
pager = os.environ["PAGER"].split()
- if os.path.exists(pager[0]) and os.access(pager[0], X_OK):
+ if os.path.exists(pager[0]) and os.access(pager[0], os.X_OK):
return pager
for p in ["less", "more"]:
path = LMIFormatter.__which(p)
diff --git a/cli/lmi/shell/LMIMethod.py b/cli/lmi/shell/LMIMethod.py
index 28c3db6..98eff31 100644
--- a/cli/lmi/shell/LMIMethod.py
+++ b/cli/lmi/shell/LMIMethod.py
@@ -13,7 +13,6 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
-import os
import sys
import time
import pywbem
10 years, 9 months
[cura-tools] master: fix forgotten doc string in LMIConsole (85f2cdd)
by phatina@fedoraproject.org
Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
>---------------------------------------------------------------
commit 85f2cdd8106cbd26aeb928c3bcb973520b41fa2b
Author: Peter Hatina <phatina(a)redhat.com>
Date: Wed Jul 31 13:31:57 2013 +0200
fix forgotten doc string in LMIConsole
>---------------------------------------------------------------
cli/lmi/shell/LMIConsole.py | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)
diff --git a/cli/lmi/shell/LMIConsole.py b/cli/lmi/shell/LMIConsole.py
index 94f91fb..d59d362 100644
--- a/cli/lmi/shell/LMIConsole.py
+++ b/cli/lmi/shell/LMIConsole.py
@@ -94,6 +94,9 @@ class LMIConsole(code.InteractiveConsole):
lmi_set_use_exceptions(self._use_exceptions)
def setup_completer(self):
+ """
+ Initializes tab-completer.
+ """
self._completer = LMICompleter(self.locals)
readline.set_completer(self._completer.complete)
readline.parse_and_bind("tab: complete")
10 years, 9 months
[cura-tools] master: fix provide locals when transfering from script to interactive mode (b491da1)
by phatina@fedoraproject.org
Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
>---------------------------------------------------------------
commit b491da193e121a9fa2a159a8f64c4dfd17f6321e
Author: Peter Hatina <phatina(a)redhat.com>
Date: Wed Jul 31 13:32:59 2013 +0200
fix provide locals when transfering from script to interactive mode
>---------------------------------------------------------------
cli/lmishell | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/cli/lmishell b/cli/lmishell
index 9bc10ff..f49a208 100755
--- a/cli/lmishell
+++ b/cli/lmishell
@@ -49,7 +49,7 @@ if __name__ == "__main__":
script_argv = options.script_argv
exit_code = console.interpret(script_name, script_argv)
if options.interact:
- console.interact()
+ console.interact(console.locals)
else:
sys.exit(exit_code)
sys.exit(0)
10 years, 9 months
[cura-tools] master: unify ctor-s calls (1d112fd)
by phatina@fedoraproject.org
Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
>---------------------------------------------------------------
commit 1d112fd13ebaf1193f7da36c2ee1ecf8b207a612
Author: Peter Hatina <phatina(a)redhat.com>
Date: Wed Jul 31 13:04:53 2013 +0200
unify ctor-s calls
>---------------------------------------------------------------
cli/lmi/shell/LMIClass.py | 2 +-
cli/lmi/shell/LMIConstantValues.py | 4 ++--
cli/lmi/shell/LMIFormatter.py | 4 ++--
cli/lmi/shell/LMIInstance.py | 2 +-
cli/lmi/shell/LMIMethod.py | 2 +-
cli/lmi/shell/LMINamespace.py | 2 +-
cli/lmi/shell/LMIShellClient.py | 2 +-
7 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/cli/lmi/shell/LMIClass.py b/cli/lmi/shell/LMIClass.py
index fc07dea..a82d2fb 100644
--- a/cli/lmi/shell/LMIClass.py
+++ b/cli/lmi/shell/LMIClass.py
@@ -46,7 +46,7 @@ class LMIClass(LMIBaseObject):
namespace -- LMINamespace object used for CIMInstances retrieval
classname -- string containing CIM class name
"""
- super(self.__class__, self).__init__()
+ super(LMIClass, self).__init__()
self.__dict__["_client"] = client
self.__dict__["_namespace"] = namespace
self.__dict__["_cim_class"] = None
diff --git a/cli/lmi/shell/LMIConstantValues.py b/cli/lmi/shell/LMIConstantValues.py
index d783641..79449e4 100644
--- a/cli/lmi/shell/LMIConstantValues.py
+++ b/cli/lmi/shell/LMIConstantValues.py
@@ -115,7 +115,7 @@ class LMIConstantValuesParamProp(LMIConstantValues):
necessary member "type" which is needed for proper casting.
"""
self._cast_type = cim_property.type
- super(self.__class__, self).__init__(cim_property)
+ super(LMIConstantValuesParamProp, self).__init__(cim_property)
class LMIConstantValuesMethodReturnType(LMIConstantValues):
"""
@@ -129,4 +129,4 @@ class LMIConstantValuesMethodReturnType(LMIConstantValues):
cim_method -- CIMMethod object
"""
self._cast_type = cim_method.return_type
- super(self.__class__, self).__init__(cim_method)
+ super(LMIConstantValuesMethodReturnType, self).__init__(cim_method)
diff --git a/cli/lmi/shell/LMIFormatter.py b/cli/lmi/shell/LMIFormatter.py
index c5fef30..e11f44f 100644
--- a/cli/lmi/shell/LMIFormatter.py
+++ b/cli/lmi/shell/LMIFormatter.py
@@ -159,7 +159,7 @@ class LMIXmlFormatter(LMIFormatter):
Arguments:
xml -- XML element of pywbem.cim_xml.CLASS, retrieved by CIMClass.tocimxml()
"""
- super(self.__class__, self).__init__()
+ super(LMIXmlFormatter, self).__init__()
self._xml = xml
def __format_class_property_content(self, node, indent=0, sub_indent=0, width=80, f=sys.stdout):
@@ -419,7 +419,7 @@ class LMIMofFormatter(LMIFormatter):
Arguments:
mof -- string containing a MOF representation of a object
"""
- super(self.__class__, self).__init__()
+ super(LMIMofFormatter, self).__init__()
self._mof = mof
def format(self, indent=0, sub_indent=0, width=80, f=sys.stdout):
diff --git a/cli/lmi/shell/LMIInstance.py b/cli/lmi/shell/LMIInstance.py
index a58a0d8..80218dd 100644
--- a/cli/lmi/shell/LMIInstance.py
+++ b/cli/lmi/shell/LMIInstance.py
@@ -44,7 +44,7 @@ class LMIInstance(LMIBaseObject):
lmi_class -- LMIClass object, creation class of the instance
cim_instance -- wrapped CIMInstance object
"""
- super(self.__class__, self).__init__()
+ super(LMIInstance, self).__init__()
# We use __dict__ to avoid recursion potentially caused by
# combo __setattr__ and __getattr__
self.__dict__["_deleted"] = False
diff --git a/cli/lmi/shell/LMIMethod.py b/cli/lmi/shell/LMIMethod.py
index 7ef1879..28c3db6 100644
--- a/cli/lmi/shell/LMIMethod.py
+++ b/cli/lmi/shell/LMIMethod.py
@@ -79,7 +79,7 @@ class LMIMethod(LMIBaseObject):
sync_method -- bool flag indicating, if we are trying to perform
a synchronous method call
"""
- super(self.__class__, self).__init__()
+ super(LMIMethod, self).__init__()
self._client = client
self._lmi_instance = lmi_instance
self._method = lmi_instance._lmi_class._cim_class.methods[method]
diff --git a/cli/lmi/shell/LMINamespace.py b/cli/lmi/shell/LMINamespace.py
index e0ad42c..72a5f4d 100644
--- a/cli/lmi/shell/LMINamespace.py
+++ b/cli/lmi/shell/LMINamespace.py
@@ -137,7 +137,7 @@ class LMINamespaceRoot(LMINamespace):
Arguments:
client -- LMIShellClient object used for CIMOM communication
"""
- super(self.__class__, self).__init__(client, "root")
+ super(LMINamespaceRoot, self).__init__(client, "root")
@property
def cimv2(self):
diff --git a/cli/lmi/shell/LMIShellClient.py b/cli/lmi/shell/LMIShellClient.py
index 21b0875..6ef012f 100644
--- a/cli/lmi/shell/LMIShellClient.py
+++ b/cli/lmi/shell/LMIShellClient.py
@@ -44,7 +44,7 @@ class LMIShellClient(LMIBaseClient):
CIMOM, it uses unix socket for the connection; default value is
CONN_TYPE_WBEM
"""
- super(self.__class__, self).__init__(hostname, username, password, conn_type)
+ super(LMIShellClient, self).__init__(hostname, username, password, conn_type)
self._interactive = interactive
self._cache = LMIShellCache(use_cache)
10 years, 9 months
[cura-tools] master: introduce argparse for cli option parsing (2a732ce)
by phatina@fedoraproject.org
Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
>---------------------------------------------------------------
commit 2a732ce26b022840e52dac58a806e33433ae6a90
Author: Peter Hatina <phatina(a)redhat.com>
Date: Tue Jul 30 21:58:23 2013 +0200
introduce argparse for cli option parsing
>---------------------------------------------------------------
cli/lmi/shell/LMIShellOptions.py | 133 ++++++++++++++++++++------------------
cli/lmishell | 12 +---
2 files changed, 73 insertions(+), 72 deletions(-)
diff --git a/cli/lmi/shell/LMIShellOptions.py b/cli/lmi/shell/LMIShellOptions.py
index e9a6d5f..3e182a7 100644
--- a/cli/lmi/shell/LMIShellOptions.py
+++ b/cli/lmi/shell/LMIShellOptions.py
@@ -15,8 +15,33 @@
import os
import sys
+import argparse
+
from . import __version__
+class LMIShellOptionsHelpWithVersionFormatter(argparse.HelpFormatter):
+ """
+ Helper class used for help message formatting.
+ """
+ def _format_usage(self, usage, actions, groups, prefix):
+ """
+ Returns a string with preformatted LMIShell usage. This method prepends
+ LMIShell version to the usage message.
+ """
+ return argparse.HelpFormatter._format_usage(self, usage, actions, groups,
+ "OpenLMI Shell v%s\n\n" % __version__)
+
+class LMIShellOptionParser(argparse.ArgumentParser):
+ """
+ Helper class for CLI option parsing.
+ """
+ def error(self, msg):
+ """
+ Prints help message, error message and exits with erro code 2.
+ """
+ self.print_help()
+ self.exit(2, "\n%s: error: %s\n" % (self.prog, msg))
+
class LMIShellOptions(object):
"""
Class representing a LMIShell command line options.
@@ -36,51 +61,50 @@ class LMIShellOptions(object):
Arguments:
argv -- list of strings of all the provided arguments
"""
- self._argv = argv
- self._script_name = ""
- self._script_argv = []
- self._good = True
- self._inspect = False
- self._help = False
- self._log = LMIShellOptions._LOG_DEFAULT
- for a in self._argv[1:]:
- if not self._script_name and a in ("-h", "--help"):
- self._help = True
- return
- elif not self._script_name and a in ("-i", "--inspect"):
- self._inspect = True
- elif not self._script_name and a in ("-v", "--verbose"):
- if self._log == LMIShellOptions._LOG_VERBOSE:
- self._log = LMIShellOptions._LOG_MORE_VERBOSE
- else:
- self._log = LMIShellOptions._LOG_VERBOSE
- elif not self._script_name and a in ("-vv", "--more-verbose"):
- self._log = LMIShellOptions._LOG_MORE_VERBOSE
- elif not self._script_name and a in ("-q", "--quiet"):
- self._log = LMIShellOptions._LOG_QUIET
- elif not self._script_name and not a.startswith("-"):
- self._script_name = a
- elif self._script_name:
- self._script_argv.append(a)
- else:
- self._good = False
- break
+ # Create a option parser
+ parser = LMIShellOptionParser(
+ usage="%(prog)s [options] script [script-options]",
+ formatter_class=LMIShellOptionsHelpWithVersionFormatter)
+ parser.add_argument("-i", "--interact", dest="interact",
+ action="store_true", default=False,
+ help="inspect interactively after running a script")
+ parser.add_argument("-v", "--verbose", dest="verbose",
+ action="store_true", default=False,
+ help="print log messages to stderr")
+ parser.add_argument("-m", "--more-verbose", dest="more_verbose",
+ action="store_true", default=False,
+ help="print all log messages to stderr")
+ parser.add_argument("-q", "--quiet", dest="quiet",
+ action="store_true", default=False,
+ help="do not print any log messages to stderr")
- @property
- def good(self):
- """
- Property returning a bool flag, if all the provided options on the command line
- are good; if the LMIShell can continue after parsing the options.
- """
- return self._good
+ # Split CLI arguments into LMIShell ones and script ones. LMIShell arguments
+ # are those, which are before the first positional argument. Other CLI arguments
+ # are passed to the interpreted script.
+ try:
+ delimiter = 1 + map(lambda arg: not arg.startswith("-"), argv[1:]).index(True)
+ except ValueError, e:
+ delimiter = len(argv)
+ self._script_argv = argv[delimiter:]
+ self._script_name = self._script_argv[0] if self._script_argv else ""
- @property
- def help(self):
- """
- Property returning a bool flag, if the help message option was seen and the help
- message should be printed out.
- """
- return self._help
+ # Parse CLI options
+ shell_argv = argv[1:delimiter]
+ options = parser.parse_args(shell_argv)
+
+ # Store CLI options
+ self._interact = options.interact
+ self._log = LMIShellOptions._LOG_DEFAULT
+ if options.verbose:
+ self._log = LMIShellOptions._LOG_VERBOSE
+ if options.more_verbose:
+ self._log = LMIShellOptions._LOG_MORE_VERBOSE
+ if options.quiet:
+ self._log = LMIShellOptions._LOG_QUIET
+
+ # Check for errors
+ if (options.verbose, options.more_verbose, options.quiet).count(True) > 1:
+ parser.error("Options -v, -m and -q are mutually exclusive")
@property
def interactive(self):
@@ -88,16 +112,16 @@ class LMIShellOptions(object):
Property returning a bool flag, which indicates, if the LMIShell should be intially
run in the interactive mode.
"""
- return not self._script_name
+ return not self._script_argv
@property
- def inspect(self):
+ def interact(self):
"""
Property returning a bool flag, which indicates, if the LMIShell should enter an
interactive mode, after executing a provided script. The behavior is similar to
python interpreter.
"""
- return self._inspect
+ return self._interact
@property
def script_name(self):
@@ -112,7 +136,7 @@ class LMIShellOptions(object):
"""
Property returning a list of command line arguments of the interpreted script.
"""
- return [self._script_name] + self._script_argv
+ return self._script_argv
@property
def log(self):
@@ -125,18 +149,3 @@ class LMIShellOptions(object):
_LOG_QUIET
"""
return self._log
-
- def print_usage(self):
- """
- Prints out a help message.
- """
- sys.stdout.write("OpenLMI Shell v%s\n" % __version__)
- sys.stdout.write("\nUsage: %s [options] [script] [script-options]\n" % os.path.basename(self._argv[0]))
- sys.stdout.write("\nOptions:\n")
- sys.stdout.write(" -h, --help print this message\n")
- sys.stdout.write(" -i, --interact inspect interactively after running a script\n\n")
- sys.stdout.write(" -v, --verbose print log messages to stderr; if provided twice,\n")
- sys.stdout.write(" all the log messages will be printed to stderr\n")
- sys.stdout.write(" -vv, --more-verbose print all log messages to stderr\n")
- sys.stderr.write(" -q, --quiet do not print any log messages to stderr\n")
- sys.stderr.write("\n By default, error messages are print to stderr stream.\n")
diff --git a/cli/lmishell b/cli/lmishell
index 7f7c263..9bc10ff 100755
--- a/cli/lmishell
+++ b/cli/lmishell
@@ -22,15 +22,7 @@ from lmi.shell import LMIShellOptions
if __name__ == "__main__":
options = LMIShellOptions(sys.argv)
- if not options.good:
- sys.stderr.write("Wrong tool usage!\n\n")
- options.print_usage()
- sys.exit(1)
-
- if options.help:
- options.print_usage()
- sys.exit(0)
- elif options.log == LMIShellOptions._LOG_MORE_VERBOSE:
+ if options.log == LMIShellOptions._LOG_MORE_VERBOSE:
# Print out all the log messages to stderr stream
logger = logging.getLogger("")
logger.setLevel(logging.DEBUG)
@@ -56,7 +48,7 @@ if __name__ == "__main__":
script_name = options.script_name
script_argv = options.script_argv
exit_code = console.interpret(script_name, script_argv)
- if options.inspect:
+ if options.interact:
console.interact()
else:
sys.exit(exit_code)
10 years, 9 months