]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools ConvertMasmToNasm: Fix exception when no arguments are given
authorJordan Justen <jordan.l.justen@intel.com>
Thu, 3 Mar 2016 08:44:38 +0000 (00:44 -0800)
committerJordan Justen <jordan.l.justen@intel.com>
Thu, 10 Mar 2016 18:01:39 +0000 (10:01 -0800)
Convert to use the argparse library rather than optparse.

As part of the conversion, the script will now give an error message
if no arguments are given. Previously the script would give an
exception when no arguments were given.

Fixes: https://github.com/tianocore/edk2/issues/65
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
BaseTools/Scripts/ConvertMasmToNasm.py

index 2f0dd4f9a65c765591359024c16326514e6991c3..82889721069da859c929e1e1cbb4c2ebf2cbe507 100755 (executable)
 #\r
 # Import Modules\r
 #\r
+import argparse\r
 import os.path\r
 import re\r
 import StringIO\r
 import subprocess\r
 import sys\r
-from optparse import OptionParser\r
 \r
 \r
 class UnsupportedConversion(Exception):\r
@@ -45,20 +45,20 @@ class CommonUtils:
 \r
     def __init__(self, clone=None):\r
         if clone is None:\r
-            (self.Opt, self.Args) = self.ProcessCommandLine()\r
+            self.args = self.ProcessCommandLine()\r
         else:\r
-            (self.Opt, self.Args) = (clone.Opt, clone.Args)\r
+            self.args = clone.args\r
 \r
         self.unsupportedSyntaxSeen = False\r
-        self.src = self.Args[0]\r
+        self.src = self.args.source\r
         assert(os.path.exists(self.src))\r
         self.dirmode = os.path.isdir(self.src)\r
         srcExt = os.path.splitext(self.src)[1]\r
         assert (self.dirmode or srcExt != '.nasm')\r
         self.infmode = not self.dirmode and srcExt == '.inf'\r
-        self.diff = self.Opt.diff\r
-        self.git = self.Opt.git\r
-        self.force = self.Opt.force\r
+        self.diff = self.args.diff\r
+        self.git = self.args.git\r
+        self.force = self.args.force\r
 \r
         if clone is None:\r
             self.rootdir = os.getcwd()\r
@@ -69,27 +69,22 @@ class CommonUtils:
             self.gitemail = clone.gitemail\r
 \r
     def ProcessCommandLine(self):\r
-        Parser = OptionParser(description=self.__copyright__,\r
-                              version=self.__version__,\r
-                              prog=sys.argv[0],\r
-                              usage=self.__usage__\r
-                              )\r
-        Parser.add_option("-q", "--quiet", action="store_true", type=None,\r
-                          help="Disable all messages except FATAL ERRORS.")\r
-        Parser.add_option("--git", action="store_true", type=None,\r
-                          help="Use git to create commits for each file converted")\r
-        Parser.add_option("--diff", action="store_true", type=None,\r
-                          help="Show diff of conversion")\r
-        Parser.add_option("-f", "--force", action="store_true", type=None,\r
-                          help="Force conversion even if unsupported")\r
-\r
-        (Opt, Args) = Parser.parse_args()\r
-\r
-        if not Opt.quiet:\r
-            print self.__copyright__\r
-            Parser.print_version()\r
-\r
-        return (Opt, Args)\r
+        parser = argparse.ArgumentParser(description=self.__copyright__)\r
+        parser.add_argument('--version', action='version',\r
+                            version='%(prog)s ' + self.VersionNumber)\r
+        parser.add_argument("-q", "--quiet", action="store_true",\r
+                            help="Disable all messages except FATAL ERRORS.")\r
+        parser.add_argument("--git", action="store_true",\r
+                            help="Use git to create commits for each file converted")\r
+        parser.add_argument("--diff", action="store_true",\r
+                            help="Show diff of conversion")\r
+        parser.add_argument("-f", "--force", action="store_true",\r
+                            help="Force conversion even if unsupported")\r
+        parser.add_argument('source', help='MASM input file')\r
+        parser.add_argument('dest', nargs='?',\r
+                            help='NASM output file (default=input.nasm; - for stdout)')\r
+\r
+        return parser.parse_args()\r
 \r
     def RootRelative(self, path):\r
         result = path\r
@@ -185,7 +180,7 @@ class CommonUtils:
         if not self.git or not self.gitdir:\r
             return\r
 \r
-        if not self.Opt.quiet:\r
+        if not self.args.quiet:\r
             print 'Committing: Conversion of', dst\r
 \r
         prefix = ' '.join(filter(lambda a: a, [pkg, module]))\r
@@ -235,7 +230,7 @@ class ConvertAsmFile(CommonUtils):
             self.output = sys.stdout\r
         else:\r
             self.output = StringIO.StringIO()\r
-        if not self.Opt.quiet:\r
+        if not self.args.quiet:\r
             dirpath, src = os.path.split(self.inputFilename)\r
             dirpath = self.RootRelative(dirpath)\r
             dst = os.path.basename(self.outputFilename)\r
@@ -811,11 +806,11 @@ class ConvertInfFile(CommonUtils):
                 self.UpdateInfAsmFile(dst)\r
                 didSomething = True\r
             except UnsupportedConversion:\r
-                if not self.Opt.quiet:\r
+                if not self.args.quiet:\r
                     print 'MASM=>NASM conversion unsupported for', dst\r
                 notConverted.append(dst)\r
             except NoSourceFile:\r
-                if not self.Opt.quiet:\r
+                if not self.args.quiet:\r
                     print 'Source file missing for', reldst\r
                 notConverted.append(dst)\r
             except UnsupportedArch:\r
@@ -823,11 +818,11 @@ class ConvertInfFile(CommonUtils):
             else:\r
                 if didSomething:\r
                     self.ConversionFinished(dst)\r
-        if len(notConverted) > 0 and not self.Opt.quiet:\r
+        if len(notConverted) > 0 and not self.args.quiet:\r
             for dst in notConverted:\r
                 reldst = self.RootRelative(dst)\r
                 print 'Unabled to convert', reldst\r
-        if unsupportedArchCount > 0 and not self.Opt.quiet:\r
+        if unsupportedArchCount > 0 and not self.args.quiet:\r
             print 'Skipped', unsupportedArchCount, 'files based on architecture'\r
 \r
     def UpdateInfAsmFile(self, dst, IgnoreMissingAsm=False):\r
@@ -921,11 +916,11 @@ class ConvertInfFiles(CommonUtils):
                     inf.UpdateInfAsmFile(reldst, IgnoreMissingAsm=didSomething)\r
                     didSomething = True\r
             except UnsupportedConversion:\r
-                if not self.Opt.quiet:\r
+                if not self.args.quiet:\r
                     print 'MASM=>NASM conversion unsupported for', reldst\r
                 notConverted.append(dst)\r
             except NoSourceFile:\r
-                if not self.Opt.quiet:\r
+                if not self.args.quiet:\r
                     print 'Source file missing for', reldst\r
                 notConverted.append(dst)\r
             except UnsupportedArch:\r
@@ -933,11 +928,11 @@ class ConvertInfFiles(CommonUtils):
             else:\r
                 if didSomething:\r
                     inf.ConversionFinished(reldst)\r
-        if len(notConverted) > 0 and not self.Opt.quiet:\r
+        if len(notConverted) > 0 and not self.args.quiet:\r
             for dst in notConverted:\r
                 reldst = self.RootRelative(dst)\r
                 print 'Unabled to convert', reldst\r
-        if unsupportedArchCount > 0 and not self.Opt.quiet:\r
+        if unsupportedArchCount > 0 and not self.args.quiet:\r
             print 'Skipped', unsupportedArchCount, 'files based on architecture'\r
 \r
 \r
@@ -970,19 +965,13 @@ class ConvertAsmApp(CommonUtils):
     def __init__(self):\r
         CommonUtils.__init__(self)\r
 \r
-        numArgs = len(self.Args)\r
-        assert(numArgs >= 1)\r
+        src = self.args.source\r
+        dst = self.args.dest\r
         if self.infmode:\r
-            ConvertInfFiles(self.Args, self)\r
+            ConvertInfFiles(src, self)\r
         elif self.dirmode:\r
-            ConvertDirectories(self.Args, self)\r
+            ConvertDirectories((src,), self)\r
         elif not self.dirmode:\r
-            assert(numArgs <= 2)\r
-            src = self.Args[0]\r
-            if numArgs > 1:\r
-                dst = self.Args[1]\r
-            else:\r
-                dst = None\r
             ConvertAsmFile(src, dst, self)\r
 \r
 ConvertAsmApp()\r