You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/12/28 18:41:52 UTC

[GitHub] [airflow] potiuk commented on a change in pull request #13234: Rewrite handwritten argument parser in prepare_provider_packages.py

potiuk commented on a change in pull request #13234:
URL: https://github.com/apache/airflow/pull/13234#discussion_r549448941



##########
File path: dev/provider_packages/prepare_provider_packages.py
##########
@@ -1532,70 +1506,92 @@ def copy_readme_and_changelog(provider_package_id: str, backport_packages: bool)
     suffix = ""
 
     provider_names = get_provider_packages()
-    possible_first_params = provider_names.copy()
-    possible_first_params.append(LIST_PROVIDERS_PACKAGES)
-    possible_first_params.append(LIST_BACKPORTABLE_PACKAGES)
-    possible_first_params.append(UPDATE_PACKAGE_RELEASE_NOTES)
-    possible_first_params.append(GENERATE_SETUP_FILES)
-    if len(sys.argv) == 1:
-        print(
-            """
-ERROR! Missing first param"
-""",
-            file=sys.stderr,
-        )
-        usage()
-        sys.exit(1)
-    if sys.argv[1] == "--version-suffix":
-        if len(sys.argv) < 3:
-            print(
+    help_text = textwrap.dedent(
+        """
+                Available packages:
+
                 """
-ERROR! --version-suffix needs parameter!
-""",
-                file=sys.stderr,
-            )
-            usage()
-            sys.exit(1)
-        suffix = sys.argv[2]
-        sys.argv = [sys.argv[0]] + sys.argv[3:]
-    elif "--help" in sys.argv or "-h" in sys.argv or len(sys.argv) < 2:
-        usage()
-        sys.exit(0)
+    )
+    out = ""
+    for package in provider_names:
+        out += f"{package} "
+    out_array = textwrap.wrap(out, 80)
 
-    if sys.argv[1] not in possible_first_params:
-        print(
-            f"""
-ERROR! Wrong first param: {sys.argv[1]}
-""",
-            file=sys.stderr,
-        )
-        usage()
-        print()
+    for text in out_array:
+        help_text += f"{text}\n"
+
+    parser = argparse.ArgumentParser(description=help_text, formatter_class=argparse.RawTextHelpFormatter)
+    parser.add_argument(
+        PACKAGES,
+        help=textwrap.dedent(
+            """provide packages for setup.py.
+Choose from the above available packages."""
+        ),
+    )
+    parser.add_argument(
+        VERSION_SUFFIX,
+        metavar="SUFFIX",
+        help=textwrap.dedent(
+            """adds version suffix to version of the packages.
+Only useful when generating RC candidates for PyPI."""
+        ),
+    )
+
+    subparsers = parser.add_subparsers(dest="cmd")
+
+    first_param_subparser1 = subparsers.add_parser(LIST_PROVIDERS_PACKAGES, help="list all provider packages")
+    first_param_subparser1.set_defaults(cmd=LIST_PROVIDERS_PACKAGES)

Review comment:
       Yep. I agree with @mik-laj this is a much better approach. It makes it much more readable if the code to execute is in separate functions for each options. It's pretty natural evolution when the command line options become more complex. When you start, it's easier to add hand-written stuff, and keep code "together" but once you start adding functionality there is a need at some point in time (now !) to split it into smaller, independent methods. The next step is to split it into separate modules, but we are far from that!




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org