]> granicus.if.org Git - python/commitdiff
Issue #18973: Command-line interface of the calendar module now uses argparse
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 1 Nov 2015 15:14:27 +0000 (17:14 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 1 Nov 2015 15:14:27 +0000 (17:14 +0200)
instead of optparse.

Lib/calendar.py
Lib/test/test_calendar.py
Misc/NEWS

index 02050ea4579ee79e8882b24801d153fce257ee45..4ff154cb6ef0607de40bc896673414f8529aa981 100644 (file)
@@ -605,51 +605,63 @@ def timegm(tuple):
 
 
 def main(args):
-    import optparse
-    parser = optparse.OptionParser(usage="usage: %prog [options] [year [month]]")
-    parser.add_option(
+    import argparse
+    parser = argparse.ArgumentParser()
+    textgroup = parser.add_argument_group('text only arguments')
+    htmlgroup = parser.add_argument_group('html only arguments')
+    textgroup.add_argument(
         "-w", "--width",
-        dest="width", type="int", default=2,
-        help="width of date column (default 2, text only)"
+        type=int, default=2,
+        help="width of date column (default 2)"
     )
-    parser.add_option(
+    textgroup.add_argument(
         "-l", "--lines",
-        dest="lines", type="int", default=1,
-        help="number of lines for each week (default 1, text only)"
+        type=int, default=1,
+        help="number of lines for each week (default 1)"
     )
-    parser.add_option(
+    textgroup.add_argument(
         "-s", "--spacing",
-        dest="spacing", type="int", default=6,
-        help="spacing between months (default 6, text only)"
+        type=int, default=6,
+        help="spacing between months (default 6)"
     )
-    parser.add_option(
+    textgroup.add_argument(
         "-m", "--months",
-        dest="months", type="int", default=3,
-        help="months per row (default 3, text only)"
+        type=int, default=3,
+        help="months per row (default 3)"
     )
-    parser.add_option(
+    htmlgroup.add_argument(
         "-c", "--css",
-        dest="css", default="calendar.css",
-        help="CSS to use for page (html only)"
+        default="calendar.css",
+        help="CSS to use for page"
     )
-    parser.add_option(
+    parser.add_argument(
         "-L", "--locale",
-        dest="locale", default=None,
+        default=None,
         help="locale to be used from month and weekday names"
     )
-    parser.add_option(
+    parser.add_argument(
         "-e", "--encoding",
-        dest="encoding", default=None,
-        help="Encoding to use for output."
+        default=None,
+        help="encoding to use for output"
     )
-    parser.add_option(
+    parser.add_argument(
         "-t", "--type",
-        dest="type", default="text",
+        default="text",
         choices=("text", "html"),
         help="output type (text or html)"
     )
+    parser.add_argument(
+        "year",
+        nargs='?', type=int,
+        help="year number (1-9999)"
+    )
+    parser.add_argument(
+        "month",
+        nargs='?', type=int,
+        help="month number (1-12, text only)"
+    )
 
-    (options, args) = parser.parse_args(args)
+    options = parser.parse_args(args[1:])
 
     if options.locale and not options.encoding:
         parser.error("if --locale is specified --encoding is required")
@@ -667,10 +679,10 @@ def main(args):
             encoding = sys.getdefaultencoding()
         optdict = dict(encoding=encoding, css=options.css)
         write = sys.stdout.buffer.write
-        if len(args) == 1:
+        if options.year is None:
             write(cal.formatyearpage(datetime.date.today().year, **optdict))
-        elif len(args) == 2:
-            write(cal.formatyearpage(int(args[1]), **optdict))
+        elif options.month is None:
+            write(cal.formatyearpage(options.year, **optdict))
         else:
             parser.error("incorrect number of arguments")
             sys.exit(1)
@@ -680,18 +692,15 @@ def main(args):
         else:
             cal = TextCalendar()
         optdict = dict(w=options.width, l=options.lines)
-        if len(args) != 3:
+        if options.month is None:
             optdict["c"] = options.spacing
             optdict["m"] = options.months
-        if len(args) == 1:
+        if options.year is None:
             result = cal.formatyear(datetime.date.today().year, **optdict)
-        elif len(args) == 2:
-            result = cal.formatyear(int(args[1]), **optdict)
-        elif len(args) == 3:
-            result = cal.formatmonth(int(args[1]), int(args[2]), **optdict)
+        elif options.month is None:
+            result = cal.formatyear(options.year, **optdict)
         else:
-            parser.error("incorrect number of arguments")
-            sys.exit(1)
+            result = cal.formatmonth(options.year, options.month, **optdict)
         write = sys.stdout.write
         if options.encoding:
             result = result.encode(options.encoding)
index 80ed6325881a1247c0c718fc6b3eb54dfbd9e480..d9d3128ea8807dd4133f859eccfc0c0e8602d5ed 100644 (file)
@@ -702,19 +702,19 @@ class CommandLineTestCase(unittest.TestCase):
 
     def assertFailure(self, *args):
         rc, stdout, stderr = assert_python_failure('-m', 'calendar', *args)
-        self.assertIn(b'Usage:', stderr)
+        self.assertIn(b'usage:', stderr)
         self.assertEqual(rc, 2)
 
     def test_help(self):
         stdout = self.run_ok('-h')
-        self.assertIn(b'Usage:', stdout)
+        self.assertIn(b'usage:', stdout)
         self.assertIn(b'calendar.py', stdout)
         self.assertIn(b'--help', stdout)
 
     def test_illegal_arguments(self):
         self.assertFailure('-z')
-        #self.assertFailure('spam')
-        #self.assertFailure('2004', 'spam')
+        self.assertFailure('spam')
+        self.assertFailure('2004', 'spam')
         self.assertFailure('-t', 'html', '2004', '1')
 
     def test_output_current_year(self):
index b67a3a5bcd32da521185eb57394f6996e5e9ed43..5722a3a92206056f360eaf4ab8cba60f9467e7b2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -66,6 +66,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #18973: Command-line interface of the calendar module now uses argparse
+  instead of optparse.
+
 - Issue #25510: fileinput.FileInput.readline() now returns b'' instead of ''
   at the end if the FileInput was opened with binary mode.
   Patch by Ryosuke Ito.