Repository: gorakhargosh/watchdog
Pull request #887 (2022-05-13 12:41:35)

[watchmedo] Fix broken parsing of boolean arguments
Related to #855.


Patch:
diff --git a/changelog.rst b/changelog.rst
index 1b3a1ef..a6027fe 100644
--- a/changelog.rst
+++ b/changelog.rst
@@ -10,7 +10,8 @@ Changelog
 
 - Fix adding failed emitters on observer schedule. (`#872 <https://github.com/gorakhargosh/watchdog/issues/872>`_)
 - [watchmedo] Fix broken parsing of ``--kill-after`` argument for the ``auto-restart`` command. (`#870 <https://github.com/gorakhargosh/watchdog/issues/870>`_)
-- Thanks to our beloved contributors: @taleinat, @kianmeng, @palfrey, @IlayRosenberg
+- [watchmedo] Fix broken parsing of boolean arguments. (`#855 <https://github.com/gorakhargosh/watchdog/issues/855>`_)
+- Thanks to our beloved contributors: @taleinat, @kianmeng, @palfrey, @IlayRosenberg, @BoboTiG
 
 2.1.7
 ~~~~~
diff --git a/src/watchdog/watchmedo.py b/src/watchdog/watchmedo.py
index a3d1fd8..b76b8c0 100755
--- a/src/watchdog/watchmedo.py
+++ b/src/watchdog/watchmedo.py
@@ -213,22 +213,23 @@ def schedule_tricks(observer, tricks, pathname, recursive):
                    type=float,
                    help='Use this as the polling interval/blocking timeout (in seconds).'),
           argument('--recursive',
+                   action='store_true',
                    default=True,
                    help='Recursively monitor paths.'),
           argument('--debug-force-polling',
-                   default=False,
+                   action='store_true',
                    help='[debug] Forces polling.'),
           argument('--debug-force-kqueue',
-                   default=False,
+                   action='store_true',
                    help='[debug] Forces BSD kqueue(2).'),
           argument('--debug-force-winapi',
-                   default=False,
+                   action='store_true',
                    help='[debug] Forces Windows API.'),
           argument('--debug-force-fsevents',
-                   default=False,
+                   action='store_true',
                    help='[debug] Forces macOS FSEvents.'),
           argument('--debug-force-inotify',
-                   default=False,
+                   action='store_true',
                    help='[debug] Forces Linux inotify(7).')],
          cmd_aliases=['tricks'])
 def tricks_from(args):
@@ -302,7 +303,7 @@ def tricks_from(args):
           argument('-a',
                    '--append-only',
                    dest='append_only',
-                   default=False,
+                   action='store_true',
                    help='''
                    If --append-to-file is not specified, produces output for
                    appending instead of a complete tricks YAML file.''')],
@@ -357,13 +358,11 @@ def tricks_generate_yaml(args):
           argument('-D',
                    '--ignore-directories',
                    dest='ignore_directories',
-                   default=False,
                    action='store_true',
                    help='Ignores events for directories.'),
           argument('-R',
                    '--recursive',
                    dest='recursive',
-                   default=False,
                    action='store_true',
                    help='Monitors the directories recursively.'),
           argument('--interval',
@@ -373,22 +372,22 @@ def tricks_generate_yaml(args):
                    type=float,
                    help='Use this as the polling interval/blocking timeout.'),
           argument('--trace',
-                   default=False,
+                   action='store_true',
                    help='Dumps complete dispatching trace.'),
           argument('--debug-force-polling',
-                   default=False,
+                   action='store_true',
                    help='[debug] Forces polling.'),
           argument('--debug-force-kqueue',
-                   default=False,
+                   action='store_true',
                    help='[debug] Forces BSD kqueue(2).'),
           argument('--debug-force-winapi',
-                   default=False,
+                   action='store_true',
                    help='[debug] Forces Windows API.'),
           argument('--debug-force-fsevents',
-                   default=False,
+                   action='store_true',
                    help='[debug] Forces macOS FSEvents.'),
           argument('--debug-force-inotify',
-                   default=False,
+                   action='store_true',
                    help='[debug] Forces Linux inotify(7).')])
 def log(args):
     """
@@ -472,7 +471,6 @@ def log(args):
           argument('-R',
                    '--recursive',
                    dest='recursive',
-                   default=False,
                    action='store_true',
                    help='Monitors the directories recursively.'),
           argument('--interval',
@@ -484,16 +482,14 @@ def log(args):
           argument('-w', '--wait',
                    dest='wait_for_process',
                    action='store_true',
-                   default=False,
                    help='Wait for process to finish to avoid multiple simultaneous instances.'),
           argument('-W', '--drop',
                    dest='drop_during_process',
                    action='store_true',
-                   default=False,
                    help='Ignore events that occur while command is still being'
                    ' executed to avoid multiple simultaneous instances.'),
           argument('--debug-force-polling',
-                   default=False,
+                   action='store_true',
                    help='[debug] Forces polling.')])
 def shell_command(args):
     """
@@ -535,7 +531,7 @@ def shell_command(args):
           argument('-d',
                    '--directory',
                    dest='directories',
-                   metavar='directory',
+                   metavar='DIRECTORY',
                    action='append',
                    help='Directory to watch. Use another -d or --directory option '
                    'for each directory.'),
@@ -560,7 +556,6 @@ def shell_command(args):
           argument('-R',
                    '--recursive',
                    dest='recursive',
-                   default=False,
                    action='store_true',
                    help='Monitors the directories recursively.'),
           argument('--interval',
@@ -574,7 +569,7 @@ def shell_command(args):
                    default='SIGINT',
                    help='Stop the subprocess with this signal (default SIGINT).'),
           argument('--debug-force-polling',
-                   default=False,
+                   action='store_true',
                    help='[debug] Forces polling.'),
           argument('--kill-after',
                    dest='kill_after',


Test patch:
diff --git a/tests/test_0_watchmedo.py b/tests/test_0_watchmedo.py
index 5da4314..36187fc 100644
--- a/tests/test_0_watchmedo.py
+++ b/tests/test_0_watchmedo.py
@@ -75,10 +75,12 @@ def test_kill_auto_restart(tmpdir, capfd):
 
 
 def test_auto_restart_arg_parsing_basic():
-    args = watchmedo.cli.parse_args(["auto-restart", "-d", ".", "cmd"])
+    args = watchmedo.cli.parse_args(["auto-restart", "-d", ".", "--recursive", "--debug-force-polling", "cmd"])
     assert args.func is watchmedo.auto_restart
     assert args.command == "cmd"
     assert args.directories == ["."]
+    assert args.recursive
+    assert args.debug_force_polling
 
 
 def test_auto_restart_arg_parsing_kill_after():