prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  `ERROR: Could not consume arg: freesurfer` when finishing `run.py freesurfer`
  Here is the output from running `run.py freesurfer`:
  ```
  2021-04-15 17:02:00 INFO Now running: init_derivatives_dir
  17:02:01 Using custom configuration: /home/merlin/hMT+/config.py
  17:02:01 [Step-01] Running: Initializing output directories.
  17:02:01 [Step-01] Initializing output directories.
  17:02:01 [Step-01] Completed: Initializing output directories.
  2021-04-15 17:02:01 INFO Successfully finished running: init_derivatives_dir
  2021-04-15 17:02:01 INFO Now running: recon_all
  17:02:01 Running FreeSurfer
  17:02:01 Running recon-all on subject 01. This will take a LONG time – it's a good idea to let it run over night.
  17:02:01 Subject 01 is already present. Please delete the directory if you want to recompute.
  ERROR: Could not consume arg: freesurfer
  Usage: recon_all.py freesurfer
  
  ```
  Here I only ask to process subject 01, which was already done.
  Even though there was no error within `recon_all.py` (which I checked using a print at the end of `main`), the script exits with an error.
  The problem seems to be coming from using both Fire and runpy: https://github.com/mne-tools/mne-bids-pipeline/blob/d49d05351b2da2a4269dff5761a457e0e85964bd/run.py#L98
  it seems Fire expects all arguments to be used for both `run.py` and `recon_all.py`, when they are only used for `run.py`.
  
  Note that this is not a regression from #289, as it happens even without that modification.
  
  I'm not sure how fix this, maybe it is simple though as it does not seem to be a problem for other scripts.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `scripts/freesurfer/recon_all.py`
  Content:
  ```
  1 #!/usr/bin/env python
  2 
  3 import os
  4 import shutil
  5 import sys
  6 from pathlib import Path
  7 import logging
  8 from typing import Union
  9 
  10 import fire
  11 
  12 from mne.utils import run_subprocess
  13 from mne.parallel import parallel_func
  14 
  15 import config
  16 
  17 PathLike = Union[str, Path]
  18 logger = logging.getLogger('mne-bids-pipeline')
  19 fs_bids_app = Path(__file__).parent / 'contrib' / 'run.py'
  20 
  21 
  22 def _get_subjects_dir(root_dir) -> Path:
  23     subjects_dir = \
  24         Path(root_dir) / "derivatives" / "freesurfer" / "subjects"
  25     return subjects_dir
  26 
  27 
  28 def run_recon(root_dir, subject, fs_bids_app) -> None:
  29     logger.info(f"Running recon-all on subject {subject}. This will take "
  30                 f"a LONG time – it's a good idea to let it run over night.")
  31 
  32     subjects_dir = _get_subjects_dir(root_dir)
  33     subj_dir = subjects_dir / f"sub-{subject}"
  34 
  35     if subj_dir.exists():
  36         logger.info(f"Subject {subject} is already present. Please delete the "
  37                     f"directory if you want to recompute.")
  38         return
  39 
  40     env = os.environ
  41     if 'FREESURFER_HOME' not in env:
  42         raise RuntimeError("FreeSurfer is not available.")
  43 
  44     license_file = Path(f"{env['FREESURFER_HOME']}/license.txt")
  45     if not license_file.exists():
  46         license_file = Path(f"{env['FREESURFER_HOME']}/.license")
  47     if not license_file.exists():
  48         raise RuntimeError("FreeSurfer license file not found.")
  49 
  50     cmd = [
  51         f"{sys.executable}",
  52         f"{fs_bids_app}",
  53         f"{root_dir}",
  54         f"{subjects_dir}", "participant",
  55         "--n_cpus=2", "--stages=all", "--skip_bids_validator",
  56         f"--license_file={license_file}",
  57         f"--participant_label={subject}"
  58     ]
  59     logger.debug("Running: " + " ".join(cmd))
  60     run_subprocess(cmd, env=env, verbose=logger.level)
  61 
  62 
  63 def main(*, n_jobs: int = 1) -> None:
  64     """Run freesurfer recon-all command on BIDS dataset.
  65 
  66     The command allows to run the freesurfer recon-all
  67     command on all subjects of your BIDS dataset. It can
  68     run in parallel with the --n_jobs parameter.
  69 
  70     It is built on top of the FreeSurfer BIDS app:
  71 
  72     https://github.com/BIDS-Apps/freesurfer
  73 
  74     You must have freesurfer available on your system.
  75 
  76     Examples
  77     --------
  78     run_freesurfer.py /path/to/bids/dataset/study-template-config.py /path/to/freesurfer_bids_app/
  79 
  80     or to run in parallel (3 subjects at a time):
  81 
  82     run_freesurfer.py /path/to/bids/dataset/study-template-config.py /path/to/freesurfer_bids_app/ --n_jobs=3
  83 
  84     """  # noqa
  85 
  86     logger.info('Running FreeSurfer')
  87 
  88     subjects = config.get_subjects()
  89 
  90     root_dir = config.bids_root
  91     subjects_dir = _get_subjects_dir(root_dir)
  92     subjects_dir.mkdir(parents=True, exist_ok=True)
  93 
  94     parallel, run_func, _ = parallel_func(run_recon, n_jobs=n_jobs)
  95     parallel(run_func(root_dir, subject, fs_bids_app)
  96              for subject in subjects)
  97 
  98     # Handle fsaverage
  99     fsaverage_dir = subjects_dir / 'fsaverage'
  100     if fsaverage_dir.exists():
  101         if fsaverage_dir.is_symlink():
  102             fsaverage_dir.unlink()
  103         else:
  104             shutil.rmtree(fsaverage_dir)
  105 
  106     env = os.environ
  107     shutil.copytree(f"{env['FREESURFER_HOME']}/subjects/fsaverage",
  108                     subjects_dir / 'fsaverage')
  109 
  110 
  111 if __name__ == '__main__':
  112     fire.Fire(main)
  113
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/scripts/freesurfer/recon_all.py b/scripts/freesurfer/recon_all.py
  --- a/scripts/freesurfer/recon_all.py
  +++ b/scripts/freesurfer/recon_all.py
  @@ -7,8 +7,6 @@
   import logging
   from typing import Union
   
  -import fire
  -
   from mne.utils import run_subprocess
   from mne.parallel import parallel_func
   
  @@ -60,10 +58,10 @@
       run_subprocess(cmd, env=env, verbose=logger.level)
   
   
  -def main(*, n_jobs: int = 1) -> None:
  +def main() -> None:
       """Run freesurfer recon-all command on BIDS dataset.
   
  -    The command allows to run the freesurfer recon-all
  +    The script allows to run the freesurfer recon-all
       command on all subjects of your BIDS dataset. It can
       run in parallel with the --n_jobs parameter.
   
  @@ -71,26 +69,26 @@
   
       https://github.com/BIDS-Apps/freesurfer
   
  -    You must have freesurfer available on your system.
  +    and the MNE BIDS Pipeline
   
  -    Examples
  -    --------
  -    run_freesurfer.py /path/to/bids/dataset/study-template-config.py /path/to/freesurfer_bids_app/
  +    https://mne.tools/mne-bids-pipeline
   
  -    or to run in parallel (3 subjects at a time):
  +    You must have freesurfer available on your system.
   
  -    run_freesurfer.py /path/to/bids/dataset/study-template-config.py /path/to/freesurfer_bids_app/ --n_jobs=3
  +    Run via the MNE BIDS Pipeline's `run.py`:
  +
  +    python run.py --steps=freesurfer --config=your_pipeline_config.py
   
       """  # noqa
   
       logger.info('Running FreeSurfer')
   
       subjects = config.get_subjects()
  -
       root_dir = config.bids_root
       subjects_dir = _get_subjects_dir(root_dir)
       subjects_dir.mkdir(parents=True, exist_ok=True)
   
  +    n_jobs = config.N_JOBS
       parallel, run_func, _ = parallel_func(run_recon, n_jobs=n_jobs)
       parallel(run_func(root_dir, subject, fs_bids_app)
                for subject in subjects)
  @@ -109,4 +107,4 @@
   
   
   if __name__ == '__main__':
  -    fire.Fire(main)
  +    main()
  
verification_info: {"golden_diff": "diff --git a/scripts/freesurfer/recon_all.py b/scripts/freesurfer/recon_all.py\n--- a/scripts/freesurfer/recon_all.py\n+++ b/scripts/freesurfer/recon_all.py\n@@ -7,8 +7,6 @@\n import logging\n from typing import Union\n \n-import fire\n-\n from mne.utils import run_subprocess\n from mne.parallel import parallel_func\n \n@@ -60,10 +58,10 @@\n     run_subprocess(cmd, env=env, verbose=logger.level)\n \n \n-def main(*, n_jobs: int = 1) -> None:\n+def main() -> None:\n     \"\"\"Run freesurfer recon-all command on BIDS dataset.\n \n-    The command allows to run the freesurfer recon-all\n+    The script allows to run the freesurfer recon-all\n     command on all subjects of your BIDS dataset. It can\n     run in parallel with the --n_jobs parameter.\n \n@@ -71,26 +69,26 @@\n \n     https://github.com/BIDS-Apps/freesurfer\n \n-    You must have freesurfer available on your system.\n+    and the MNE BIDS Pipeline\n \n-    Examples\n-    --------\n-    run_freesurfer.py /path/to/bids/dataset/study-template-config.py /path/to/freesurfer_bids_app/\n+    https://mne.tools/mne-bids-pipeline\n \n-    or to run in parallel (3 subjects at a time):\n+    You must have freesurfer available on your system.\n \n-    run_freesurfer.py /path/to/bids/dataset/study-template-config.py /path/to/freesurfer_bids_app/ --n_jobs=3\n+    Run via the MNE BIDS Pipeline's `run.py`:\n+\n+    python run.py --steps=freesurfer --config=your_pipeline_config.py\n \n     \"\"\"  # noqa\n \n     logger.info('Running FreeSurfer')\n \n     subjects = config.get_subjects()\n-\n     root_dir = config.bids_root\n     subjects_dir = _get_subjects_dir(root_dir)\n     subjects_dir.mkdir(parents=True, exist_ok=True)\n \n+    n_jobs = config.N_JOBS\n     parallel, run_func, _ = parallel_func(run_recon, n_jobs=n_jobs)\n     parallel(run_func(root_dir, subject, fs_bids_app)\n              for subject in subjects)\n@@ -109,4 +107,4 @@\n \n \n if __name__ == '__main__':\n-    fire.Fire(main)\n+    main()\n", "issue": "`ERROR: Could not consume arg: freesurfer` when finishing `run.py freesurfer`\nHere is the output from running `run.py freesurfer`:\r\n```\r\n2021-04-15 17:02:00 INFO Now running: init_derivatives_dir\r\n17:02:01 Using custom configuration: /home/merlin/hMT+/config.py\r\n17:02:01 [Step-01] Running: Initializing output directories.\r\n17:02:01 [Step-01] Initializing output directories.\r\n17:02:01 [Step-01] Completed: Initializing output directories.\r\n2021-04-15 17:02:01 INFO Successfully finished running: init_derivatives_dir\r\n2021-04-15 17:02:01 INFO Now running: recon_all\r\n17:02:01 Running FreeSurfer\r\n17:02:01 Running recon-all on subject 01. This will take a LONG time \u2013 it's a good idea to let it run over night.\r\n17:02:01 Subject 01 is already present. Please delete the directory if you want to recompute.\r\nERROR: Could not consume arg: freesurfer\r\nUsage: recon_all.py freesurfer\r\n\r\n```\r\nHere I only ask to process subject 01, which was already done.\r\nEven though there was no error within `recon_all.py` (which I checked using a print at the end of `main`), the script exits with an error.\r\nThe problem seems to be coming from using both Fire and runpy: https://github.com/mne-tools/mne-bids-pipeline/blob/d49d05351b2da2a4269dff5761a457e0e85964bd/run.py#L98\r\nit seems Fire expects all arguments to be used for both `run.py` and `recon_all.py`, when they are only used for `run.py`.\r\n\r\nNote that this is not a regression from #289, as it happens even without that modification.\r\n\r\nI'm not sure how fix this, maybe it is simple though as it does not seem to be a problem for other scripts.\n", "before_files": [{"content": "#!/usr/bin/env python\n\nimport os\nimport shutil\nimport sys\nfrom pathlib import Path\nimport logging\nfrom typing import Union\n\nimport fire\n\nfrom mne.utils import run_subprocess\nfrom mne.parallel import parallel_func\n\nimport config\n\nPathLike = Union[str, Path]\nlogger = logging.getLogger('mne-bids-pipeline')\nfs_bids_app = Path(__file__).parent / 'contrib' / 'run.py'\n\n\ndef _get_subjects_dir(root_dir) -> Path:\n    subjects_dir = \\\n        Path(root_dir) / \"derivatives\" / \"freesurfer\" / \"subjects\"\n    return subjects_dir\n\n\ndef run_recon(root_dir, subject, fs_bids_app) -> None:\n    logger.info(f\"Running recon-all on subject {subject}. This will take \"\n                f\"a LONG time \u2013 it's a good idea to let it run over night.\")\n\n    subjects_dir = _get_subjects_dir(root_dir)\n    subj_dir = subjects_dir / f\"sub-{subject}\"\n\n    if subj_dir.exists():\n        logger.info(f\"Subject {subject} is already present. Please delete the \"\n                    f\"directory if you want to recompute.\")\n        return\n\n    env = os.environ\n    if 'FREESURFER_HOME' not in env:\n        raise RuntimeError(\"FreeSurfer is not available.\")\n\n    license_file = Path(f\"{env['FREESURFER_HOME']}/license.txt\")\n    if not license_file.exists():\n        license_file = Path(f\"{env['FREESURFER_HOME']}/.license\")\n    if not license_file.exists():\n        raise RuntimeError(\"FreeSurfer license file not found.\")\n\n    cmd = [\n        f\"{sys.executable}\",\n        f\"{fs_bids_app}\",\n        f\"{root_dir}\",\n        f\"{subjects_dir}\", \"participant\",\n        \"--n_cpus=2\", \"--stages=all\", \"--skip_bids_validator\",\n        f\"--license_file={license_file}\",\n        f\"--participant_label={subject}\"\n    ]\n    logger.debug(\"Running: \" + \" \".join(cmd))\n    run_subprocess(cmd, env=env, verbose=logger.level)\n\n\ndef main(*, n_jobs: int = 1) -> None:\n    \"\"\"Run freesurfer recon-all command on BIDS dataset.\n\n    The command allows to run the freesurfer recon-all\n    command on all subjects of your BIDS dataset. It can\n    run in parallel with the --n_jobs parameter.\n\n    It is built on top of the FreeSurfer BIDS app:\n\n    https://github.com/BIDS-Apps/freesurfer\n\n    You must have freesurfer available on your system.\n\n    Examples\n    --------\n    run_freesurfer.py /path/to/bids/dataset/study-template-config.py /path/to/freesurfer_bids_app/\n\n    or to run in parallel (3 subjects at a time):\n\n    run_freesurfer.py /path/to/bids/dataset/study-template-config.py /path/to/freesurfer_bids_app/ --n_jobs=3\n\n    \"\"\"  # noqa\n\n    logger.info('Running FreeSurfer')\n\n    subjects = config.get_subjects()\n\n    root_dir = config.bids_root\n    subjects_dir = _get_subjects_dir(root_dir)\n    subjects_dir.mkdir(parents=True, exist_ok=True)\n\n    parallel, run_func, _ = parallel_func(run_recon, n_jobs=n_jobs)\n    parallel(run_func(root_dir, subject, fs_bids_app)\n             for subject in subjects)\n\n    # Handle fsaverage\n    fsaverage_dir = subjects_dir / 'fsaverage'\n    if fsaverage_dir.exists():\n        if fsaverage_dir.is_symlink():\n            fsaverage_dir.unlink()\n        else:\n            shutil.rmtree(fsaverage_dir)\n\n    env = os.environ\n    shutil.copytree(f\"{env['FREESURFER_HOME']}/subjects/fsaverage\",\n                    subjects_dir / 'fsaverage')\n\n\nif __name__ == '__main__':\n    fire.Fire(main)\n", "path": "scripts/freesurfer/recon_all.py"}], "after_files": [{"content": "#!/usr/bin/env python\n\nimport os\nimport shutil\nimport sys\nfrom pathlib import Path\nimport logging\nfrom typing import Union\n\nfrom mne.utils import run_subprocess\nfrom mne.parallel import parallel_func\n\nimport config\n\nPathLike = Union[str, Path]\nlogger = logging.getLogger('mne-bids-pipeline')\nfs_bids_app = Path(__file__).parent / 'contrib' / 'run.py'\n\n\ndef _get_subjects_dir(root_dir) -> Path:\n    subjects_dir = \\\n        Path(root_dir) / \"derivatives\" / \"freesurfer\" / \"subjects\"\n    return subjects_dir\n\n\ndef run_recon(root_dir, subject, fs_bids_app) -> None:\n    logger.info(f\"Running recon-all on subject {subject}. This will take \"\n                f\"a LONG time \u2013 it's a good idea to let it run over night.\")\n\n    subjects_dir = _get_subjects_dir(root_dir)\n    subj_dir = subjects_dir / f\"sub-{subject}\"\n\n    if subj_dir.exists():\n        logger.info(f\"Subject {subject} is already present. Please delete the \"\n                    f\"directory if you want to recompute.\")\n        return\n\n    env = os.environ\n    if 'FREESURFER_HOME' not in env:\n        raise RuntimeError(\"FreeSurfer is not available.\")\n\n    license_file = Path(f\"{env['FREESURFER_HOME']}/license.txt\")\n    if not license_file.exists():\n        license_file = Path(f\"{env['FREESURFER_HOME']}/.license\")\n    if not license_file.exists():\n        raise RuntimeError(\"FreeSurfer license file not found.\")\n\n    cmd = [\n        f\"{sys.executable}\",\n        f\"{fs_bids_app}\",\n        f\"{root_dir}\",\n        f\"{subjects_dir}\", \"participant\",\n        \"--n_cpus=2\", \"--stages=all\", \"--skip_bids_validator\",\n        f\"--license_file={license_file}\",\n        f\"--participant_label={subject}\"\n    ]\n    logger.debug(\"Running: \" + \" \".join(cmd))\n    run_subprocess(cmd, env=env, verbose=logger.level)\n\n\ndef main() -> None:\n    \"\"\"Run freesurfer recon-all command on BIDS dataset.\n\n    The script allows to run the freesurfer recon-all\n    command on all subjects of your BIDS dataset. It can\n    run in parallel with the --n_jobs parameter.\n\n    It is built on top of the FreeSurfer BIDS app:\n\n    https://github.com/BIDS-Apps/freesurfer\n\n    and the MNE BIDS Pipeline\n\n    https://mne.tools/mne-bids-pipeline\n\n    You must have freesurfer available on your system.\n\n    Run via the MNE BIDS Pipeline's `run.py`:\n\n    python run.py --steps=freesurfer --config=your_pipeline_config.py\n\n    \"\"\"  # noqa\n\n    logger.info('Running FreeSurfer')\n\n    subjects = config.get_subjects()\n    root_dir = config.bids_root\n    subjects_dir = _get_subjects_dir(root_dir)\n    subjects_dir.mkdir(parents=True, exist_ok=True)\n\n    n_jobs = config.N_JOBS\n    parallel, run_func, _ = parallel_func(run_recon, n_jobs=n_jobs)\n    parallel(run_func(root_dir, subject, fs_bids_app)\n             for subject in subjects)\n\n    # Handle fsaverage\n    fsaverage_dir = subjects_dir / 'fsaverage'\n    if fsaverage_dir.exists():\n        if fsaverage_dir.is_symlink():\n            fsaverage_dir.unlink()\n        else:\n            shutil.rmtree(fsaverage_dir)\n\n    env = os.environ\n    shutil.copytree(f\"{env['FREESURFER_HOME']}/subjects/fsaverage\",\n                    subjects_dir / 'fsaverage')\n\n\nif __name__ == '__main__':\n    main()\n", "path": "scripts/freesurfer/recon_all.py"}]}