imgtool: update a few errors to use click's

Click has better UI for exceptions, so instead of throwing a backtrace,
allow it to print a nicer error message.

Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/scripts/imgtool/image.py b/scripts/imgtool/image.py
index 94ed820..d6899ef 100644
--- a/scripts/imgtool/image.py
+++ b/scripts/imgtool/image.py
@@ -156,13 +156,16 @@
     def load(self, path):
         """Load an image from a given file"""
         ext = os.path.splitext(path)[1][1:].lower()
-        if ext == INTEL_HEX_EXT:
-            ih = IntelHex(path)
-            self.payload = ih.tobinarray()
-            self.base_addr = ih.minaddr()
-        else:
-            with open(path, 'rb') as f:
-                self.payload = f.read()
+        try:
+            if ext == INTEL_HEX_EXT:
+                ih = IntelHex(path)
+                self.payload = ih.tobinarray()
+                self.base_addr = ih.minaddr()
+            else:
+                with open(path, 'rb') as f:
+                    self.payload = f.read()
+        except FileNotFoundError:
+            raise click.UsageError("Input file not found")
 
         # Add the image header if needed.
         if self.pad_header and self.header_size > 0:
@@ -180,8 +183,8 @@
         if ext == INTEL_HEX_EXT:
             # input was in binary format, but HEX needs to know the base addr
             if self.base_addr is None and hex_addr is None:
-                raise Exception("No address exists in input file neither was "
-                                "it provided by user")
+                raise click.UsageError("No address exists in input file "
+                                       "neither was it provided by user")
             h = IntelHex()
             if hex_addr is not None:
                 self.base_addr = hex_addr
@@ -381,7 +384,8 @@
             return MAX_ALIGN * 2 + magic_size
         else:
             if write_size not in set([1, 2, 4, 8]):
-                raise Exception("Invalid alignment: {}".format(write_size))
+                raise click.BadParameter("Invalid alignment: {}".format(
+                    write_size))
             m = DEFAULT_MAX_SECTORS if max_sectors is None else max_sectors
             trailer = m * 3 * write_size  # status area
             if enckey is not None:
diff --git a/scripts/imgtool/main.py b/scripts/imgtool/main.py
index ba58bfd..5ef7943 100755
--- a/scripts/imgtool/main.py
+++ b/scripts/imgtool/main.py
@@ -22,6 +22,7 @@
 import sys
 from imgtool import image, imgtool_version
 from imgtool.version import decode_version
+from .keys import RSAUsageError, ECDSAUsageError, Ed25519UsageError
 
 
 def gen_rsa2048(keyfile, passwd):
@@ -116,7 +117,10 @@
     key = load_key(key)
     if key is None:
         print("Invalid passphrase")
-    key.emit_private(minimal)
+    try:
+        key.emit_private(minimal)
+    except (RSAUsageError, ECDSAUsageError, Ed25519UsageError) as e:
+        raise click.UsageError(e)
 
 
 @click.argument('imgfile')
@@ -254,7 +258,8 @@
                 or (isinstance(key, keys.RSA) and
                     not isinstance(enckey, keys.RSAPublic))):
             # FIXME
-            raise Exception("Signing and encryption must use the same type of key")
+            raise click.UsageError("Signing and encryption must use the same "
+                                   "type of key")
     img.create(key, enckey, dependencies)
     img.save(outfile, hex_addr)