cloudmonkey: add -p option to allow overriding profile

- -p or --profile overrides current selected profile for cloudmonkey
- this won't write the new profile/selection to config thereby allowing many
  cloudmonkey instances run both in interpretor and bash modes with different
  profile sharing common ui and core config options

Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
diff --git a/CHANGES.md b/CHANGES.md
index 225428a..0a68b16 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -12,6 +12,7 @@
 - Pass verifysslcert option while user logs in using username/password
 - Importing readline no longer outputs escape characters
 - CloudMonkey will not output extra empty lines in stdout output
+- New command line arg: -p for profile
 
 Version 5.3.0
 =============
@@ -23,13 +24,13 @@
 - Server profile related bugfixes, blank profile names are not allowed
 - Filtering support for default display output
 - Filtering by single key only outputs results without key names
-- New command line arg: -d for display (json, table or default)
 - Non-interactive commands from command line are outputted without colors
 - Parameter completion uses list api heuristics and related APIs as fallback
 - Parameter completion options are cached to speed up rendering of options
 - CloudMonkey returns non-zero exit code when run on shell and a error is return
   from managment server, the error message is written to stderr
 - Adds new config parameter 'verifysslcert' to enable/disable SSL cert checking
+- New command line arg: -d for display (json, table or default)
 
 Make sure you backup your config before you upgrade cloudmonkey from previous releases.
 With this release `cloudmonkey` will automatically fix your config file, add missing
diff --git a/cloudmonkey/cloudmonkey.py b/cloudmonkey/cloudmonkey.py
index b02393c..7981cb2 100644
--- a/cloudmonkey/cloudmonkey.py
+++ b/cloudmonkey/cloudmonkey.py
@@ -88,11 +88,13 @@
     port = "8080"
     path = "/client/api"
 
-    def __init__(self, pname, cfile):
+    def __init__(self, pname, cfile, profile=None):
         self.program_name = pname
         self.config_file = cfile
         self.config_options = read_config(self.get_attr, self.set_attr,
-                                          self.config_file)
+                                          self.config_file, profile)
+        if profile and profile.strip() != '':
+            self.profile = profile
         self.loadcache()
         self.init_credential_store()
         logging.basicConfig(filename=self.log_file,
@@ -754,13 +756,17 @@
                         help="output display type, json, table or default",
                         choices=tuple(displayTypes))
 
+    parser.add_argument("-p", "--profile",
+                        dest="serverProfile", default=None,
+                        help="override current server profile to use")
+
     parser.add_argument("commands", nargs=argparse.REMAINDER,
                         help="API commands")
 
     argcomplete.autocomplete(parser)
     args = parser.parse_args()
 
-    shell = CloudMonkeyShell(sys.argv[0], args.configFile)
+    shell = CloudMonkeyShell(sys.argv[0], args.configFile, args.serverProfile)
 
     if args.displayType and args.displayType in displayTypes:
         shell.set_attr("display", args.displayType)
diff --git a/cloudmonkey/config.py b/cloudmonkey/config.py
index c6bcc3a..7879e8c 100644
--- a/cloudmonkey/config.py
+++ b/cloudmonkey/config.py
@@ -91,7 +91,7 @@
     if profile is None or profile == '':
         profile = default_profile_name
     if profile in mandatory_sections:
-        print "Server profile name cannot be", profile
+        print "Server profile name cannot be '%s'" % profile
         sys.exit(1)
 
     has_profile_changed = False
@@ -131,7 +131,7 @@
     return config
 
 
-def read_config(get_attr, set_attr, config_file):
+def read_config(get_attr, set_attr, config_file, require_profile=None):
     global config_fields, config_dir, mandatory_sections
     global default_profile, default_profile_name
     if not os.path.exists(config_dir):
@@ -160,6 +160,12 @@
     else:
         global default_profile_name
         profile = default_profile_name
+
+    if require_profile and require_profile.strip() != '':
+        profile = require_profile
+    elif require_profile is not None:
+        print "Invalid profile, falling back to current profile"
+
     if profile is None or profile == '' or profile in mandatory_sections:
         print "Server profile cannot be", profile
         sys.exit(1)
@@ -168,7 +174,8 @@
                                      config.sections()))
 
     if not config.has_section(profile):
-        print "Selected profile (%s) does not exist, using defaults" % profile
+        print ("Selected profile (%s) does not exist," +
+               " using default values") % profile
         try:
             config.add_section(profile)
         except ValueError, e: