jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/495479 )

Change subject: [IMPR] Enable global args with pwb.py script
......................................................................

[IMPR] Enable global args with pwb.py script

This can be used for tests to set the default site like
 python pwb.py -lang:de bot_tests -v

- Update doc string
- a new method handle_args parses given args and returns the script's
  filename and its options and local pwb arguments which can be passed
  to pywikibot.handle_args
- check whether local pwb options are valid. Otherwise print an error
  and the doc string.

Note: Idf0af67a must be merged first

Bug: T216825
Change-Id: I511604833548f197e8a38dd72c211ebb851db8fb
---
M pwb.py
1 file changed, 34 insertions(+), 10 deletions(-)

Approvals:
  D3r1ck01: Looks good to me, but someone else must approve
  Huji: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pwb.py b/pwb.py
index 36d2b7b..0a882c7 100755
--- a/pwb.py
+++ b/pwb.py
@@ -4,10 +4,15 @@

 Run scripts using:

-    python pwb.py <name_of_script> <options>
+    python pwb.py <pwb options> <name_of_script> <options>

 and it will use the package directory to store all user files, will fix up
 search paths so the package does not need to be installed, etc.
+
+Currently <pwb options> are global options. This can be used for tests
+to set the default site like (see T216825):
+
+    python pwb.py -lang:de bot_tests -v
 """
 # (C) Pywikibot team, 2012-2019
 #
@@ -114,6 +119,25 @@
     return path


+def handle_args(pwb_py, *args):
+    """Handle args and get filename.
+
+    @return: filename, script args, local args for pwb.py
+    @rtype: tuple
+    """
+    fname = None
+    index = 0
+    for arg in args:
+        if arg.startswith('-'):
+            index += 1
+        else:
+            fname = arg
+            if not fname.endswith('.py'):
+                fname += '.py'
+            break
+    return fname, list(args[index + int(bool(fname)):]), args[:index]
+
+
 # Establish a normalised path for the directory containing pwb.py.
 # Either it is '.' if the user's current working directory is the same,
 # or it is the absolute path for the directory of pwb.py
@@ -131,15 +155,7 @@
                       "Try running 'pip install requests'.".format(e))
 del requests

-if len(sys.argv) > 1 and sys.argv[1][0] != '-':
-    filename = sys.argv[1]
-    if not filename.endswith('.py'):
-        filename += '.py'
-else:
-    filename = None
-
-# Skip the filename if one was given
-args = sys.argv[(2 if filename else 1):]
+filename, args, local_args = handle_args(*sys.argv)

 # Search for user-config.py before creating one.
 # If successful, user-config.py already exists in one of the candidate
@@ -221,6 +237,14 @@
     if not filename:
         return False

+    if local_args:  # don't use sys.argv
+        pwb_args = pwb.handle_args(local_args)
+        if pwb_args:
+            print('ERROR: unknown pwb.py argument{}: {}\n'
+                  .format('' if len(pwb_args) == 1 else 's',
+                          ', '.join(pwb_args)))
+            return False
+
     file_package = None
     argvu = pwb.argvu[1:]


--
To view, visit https://gerrit.wikimedia.org/r/495479
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I511604833548f197e8a38dd72c211ebb851db8fb
Gerrit-Change-Number: 495479
Gerrit-PatchSet: 5
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: D3r1ck01 <[email protected]>
Gerrit-Reviewer: Huji <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot (75)
Gerrit-CC: Dvorapa <[email protected]>
Gerrit-CC: Hashar <[email protected]>
Gerrit-CC: Zhuyifei1999 <[email protected]>
_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to