summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorChris St. Pierre <chris.a.st.pierre@gmail.com>2012-08-20 07:23:18 -0400
committerChris St. Pierre <chris.a.st.pierre@gmail.com>2012-08-20 09:02:32 -0400
commited2c0c5cec1bf45d2be66f0f218241d23bd77353 (patch)
treee9482a4e1f2157566c9fc75168e123067aadd390 /src
parentd299f25f723961a6ef9626c6922082c06332d168 (diff)
downloadbcfg2-ed2c0c5cec1bf45d2be66f0f218241d23bd77353.tar.gz
bcfg2-ed2c0c5cec1bf45d2be66f0f218241d23bd77353.tar.bz2
bcfg2-ed2c0c5cec1bf45d2be66f0f218241d23bd77353.zip
added --version argument to everything that uses Bcfg2.Options (#1044)
Diffstat (limited to 'src')
-rw-r--r--src/lib/Bcfg2/Options.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/src/lib/Bcfg2/Options.py b/src/lib/Bcfg2/Options.py
index ef6807255..028e878a0 100644
--- a/src/lib/Bcfg2/Options.py
+++ b/src/lib/Bcfg2/Options.py
@@ -10,6 +10,7 @@ import sys
import Bcfg2.Client.Tools
# Compatibility imports
from Bcfg2.Bcfg2Py3k import ConfigParser
+from Bcfg2.version import __version__
class OptionFailure(Exception):
@@ -123,7 +124,10 @@ class Option(object):
self.value = True
return
if self.cmd and self.cmd in rawopts:
- data = rawopts[rawopts.index(self.cmd) + 1]
+ if self.odesc:
+ data = rawopts[rawopts.index(self.cmd) + 1]
+ else:
+ data = True
self.value = self.get_cooked_value(data)
return
# No command line option found
@@ -194,8 +198,17 @@ class OptionSet(dict):
print(self.buildHelpMessage())
raise SystemExit(code)
+ def versionExit(self, code=0):
+ print("%s %s on Python %s" %
+ (os.path.basename(sys.argv[0]),
+ __version__,
+ ".".join(str(v) for v in sys.version_info[0:3])))
+ raise SystemExit(code)
+
def parse(self, argv, do_getopt=True):
'''Parse options from command line.'''
+ if VERSION not in self.values():
+ self['__version__'] = VERSION
if do_getopt:
try:
opts, args = getopt.getopt(argv, self.buildGetopt(),
@@ -205,6 +218,8 @@ class OptionSet(dict):
self.helpExit(err)
if '-h' in argv:
self.helpExit('', 0)
+ if '--version' in argv:
+ self.versionExit()
self['args'] = args
for key in list(self.keys()):
if key == 'args':
@@ -217,6 +232,8 @@ class OptionSet(dict):
if hasattr(option, 'value'):
val = option.value
self[key] = val
+ if "__version__" in self:
+ del self['__version__']
def list_split(c_string):
@@ -271,6 +288,10 @@ HELP = \
Option('Print this usage message',
default=False,
cmd='-h')
+VERSION = \
+ Option('Print the version and exit',
+ default=False,
+ cmd='--version', long_arg=True)
DAEMON = \
Option("Daemonize process, storing pid",
default=None,
@@ -879,6 +900,7 @@ CRYPT_REMOVE = \
CLI_COMMON_OPTIONS = dict(configfile=CFILE,
debug=DEBUG,
help=HELP,
+ version=VERSION,
verbose=VERBOSE,
encoding=ENCODING,
logging=LOGGING_FILE_PATH,