Browse Source

Merge "Move to argparse"

tags/2.8.0^0
Zuul 10 months ago
parent
commit
cec3a7576b
1 changed files with 49 additions and 45 deletions
  1. 49
    45
      bindep/main.py

+ 49
- 45
bindep/main.py View File

@@ -15,8 +15,8 @@
15 15
 # See the License for the specific language governing permissions and
16 16
 # limitations under the License.
17 17
 
18
+import argparse
18 19
 import logging
19
-import optparse
20 20
 import sys
21 21
 
22 22
 import bindep.depends
@@ -27,32 +27,37 @@ logging.basicConfig(
27 27
 
28 28
 
29 29
 def main(depends=None):
30
-    usage = "Usage: %prog [options] [profile]..."
31
-    parser = optparse.OptionParser(
32
-        usage=usage, version="%%prog %s" % bindep.version)
33
-    parser.add_option(
34
-        "-b", "--brief", action="store_true", dest="brief",
30
+    parser = argparse.ArgumentParser()
31
+    parser.add_argument(
32
+        "--brief", "-b", action="store_true", dest="brief",
35 33
         help="List only missing packages one per line.")
36
-    parser.add_option(
37
-        "-f", "--file", action="store", type="string", dest="filename",
38
-        default="",
34
+    parser.add_argument(
35
+        "--file", "-f", action="store", dest="filename", default="",
39 36
         help="Package list file (default: bindep.txt or "
40
-             "other-requirements.txt).")
41
-    parser.add_option(
37
+        "other-requirements.txt).")
38
+    parser.add_argument(
42 39
         "--profiles", action="store_true",
43 40
         help="List the platform and configuration profiles.")
44
-    parser.add_option(
45
-        "-l", "--list_all", dest="list_all", choices=["newline", "csv"],
46
-        type="choice", action="store",
41
+    parser.add_argument(
42
+        "--list_all", "-l", dest="list_all", action="store",
43
+        choices=["newline", "csv"],
47 44
         help="List all dependencies for this platform and profile."
48 45
              " Pass in either 'newline' or 'csv' to specify the format"
49 46
              " of the output.")
50
-    opts, args = parser.parse_args()
47
+    parser.add_argument(
48
+        'profile', nargs='*', default=["default"],
49
+        help="Extra profiles to match when checking for packages.")
50
+
51
+    parser.add_argument(
52
+        '--version', action='version', version="%%(prog)s %s" % bindep.version)
53
+    args = parser.parse_args()
54
+
51 55
     if depends is None:
52
-        depends = bindep.depends.get_depends(opts.filename)
56
+        depends = bindep.depends.get_depends(args.filename)
53 57
         if not depends:
54 58
             return 1
55
-    if opts.profiles:
59
+
60
+    if args.profiles:
56 61
         logging.info("Platform profiles:")
57 62
         for profile in depends.platform_profiles():
58 63
             logging.info("%s", profile)
@@ -60,35 +65,34 @@ def main(depends=None):
60 65
         logging.info("Configuration profiles:")
61 66
         for profile in depends.profiles():
62 67
             logging.info("%s", profile)
63
-    else:
64
-        if args:
65
-            profiles = args
66
-        else:
67
-            profiles = ["default"]
68
-        profiles = profiles + depends.platform_profiles()
69
-        rules = depends.active_rules(profiles)
70
-        if opts.list_all:
71
-            depends.list_all_packages(rules, opts.list_all)
72
-            return 0
73
-        errors = depends.check_rules(rules)
74
-        for error in errors:
75
-            if error[0] == 'missing':
76
-                if opts.brief:
77
-                    logging.info("%s", "\n".join(error[1]))
78
-                else:
79
-                    logging.info("Missing packages:")
80
-                    logging.info("    %s", " ".join(error[1]))
81
-            if error[0] == 'badversion':
82
-                if not opts.brief:
83
-                    logging.info("Bad versions of installed packages:")
84
-                    for pkg, constraint, version in error[1]:
85
-                        logging.info(
86
-                            "    %s version %s does not match %s",
87
-                            pkg, version, constraint)
88
-        if errors:
89
-            return 1
90
-    return 0
68
+        return 0
91 69
 
70
+    profiles = args.profile + depends.platform_profiles()
71
+    rules = depends.active_rules(profiles)
72
+
73
+    if args.list_all:
74
+        depends.list_all_packages(rules, args.list_all)
75
+        return 0
76
+
77
+    errors = depends.check_rules(rules)
78
+    for error in errors:
79
+        if error[0] == 'missing':
80
+            if args.brief:
81
+                logging.info("%s", "\n".join(error[1]))
82
+            else:
83
+                logging.info("Missing packages:")
84
+                logging.info("    %s", " ".join(error[1]))
85
+        if error[0] == 'badversion':
86
+            if not args.brief:
87
+                logging.info("Bad versions of installed packages:")
88
+                for pkg, constraint, version in error[1]:
89
+                    logging.info(
90
+                        "    %s version %s does not match %s",
91
+                        pkg, version, constraint)
92
+    if errors:
93
+        return 1
94
+
95
+    return 0
92 96
 
93 97
 if __name__ == '__main__':
94 98
     sys.exit(main())

Loading…
Cancel
Save