code: def parse_args(args):
      """Parse the CellProfiler command-line arguments"""
      usage = """usage: %prog [options] [<output-file>])
           where <output-file> is the optional filename for the output file of
                 measurements when running headless.
           The flags -p, -r and -c are required for a headless run."""
  
      if "--do-not-fetch" in args:
          args = list(args)
  
          args.remove("--do-not-fetch")
  
      parser = optparse.OptionParser(usage=usage)
  
      parser.add_option(
          "-p",
          "--pipeline",
          "--project",
          dest="pipeline_filename",
          help="Load this pipeline file or project on startup. If specifying a pipeline file rather than a project, the -i flag is also needed unless the pipeline is saved with the file list.",
          default=None,
      )
  
      default_show_gui = True
  
      if sys.platform.startswith("linux") and not os.getenv("DISPLAY"):
          default_show_gui = False
  
      parser.add_option(
          "-c",
          "--run-headless",
          action="store_false",
          dest="show_gui",
          default=default_show_gui,
          help="Run headless (without the GUI)",
      )
  
      parser.add_option(
          "-r",
          "--run",
          action="store_true",
          dest="run_pipeline",
          default=False,
          help="Run the given pipeline on startup",
      )
  
      parser.add_option(
          "-o",
          "--output-directory",
          dest="output_directory",
          default=None,
          help="Make this directory the default output folder",
      )
  
      parser.add_option(
          "-i",
          "--image-directory",
          dest="image_directory",
          default=None,
          help="Make this directory the default input folder",
      )
  
      parser.add_option(
          "-f",
          "--first-image-set",
          dest="first_image_set",
          default=None,
          help="The one-based index of the first image set to process",
      )
  
      parser.add_option(
          "-l",
          "--last-image-set",
          dest="last_image_set",
          default=None,
          help="The one-based index of the last image set to process",
      )
  
      parser.add_option(
          "-g",
          "--group",
          dest="groups",
          default=None,
          help='Restrict processing to one grouping in a grouped pipeline. For instance, "-g ROW=H,COL=01", will process only the group of image sets that match the keys.',
      )
  
      parser.add_option(
          "--plugins-directory",
          dest="plugins_directory",
          help="CellProfiler will look for plugin modules in this directory (headless-only).",
      )
  
      parser.add_option(
          "--conserve-memory",
          dest="conserve_memory",
          default=None,
          help="CellProfiler will attempt to release unused memory after each image set.",
      )
  
      parser.add_option(
          "--force-bioformats",
          dest="force_bioformats",
          default=None,
          help="CellProfiler will always use BioFormats for reading images.",
      )
  
      parser.add_option(
          "--version",
          dest="print_version",
          default=False,
          action="store_true",
          help="Print the version and exit",
      )
  
      parser.add_option(
          "-t",
          "--temporary-directory",
          dest="temp_dir",
          default=None,
          help=(
              "Temporary directory. "
              "CellProfiler uses this for downloaded image files "
              "and for the measurements file, if not specified. "
              "The default is " + tempfile.gettempdir()
          ),
      )
  
      parser.add_option(
          "-d",
          "--done-file",
          dest="done_file",
          default=None,
          help='The path to the "Done" file, written by CellProfiler shortly before exiting',
      )
  
      parser.add_option(
          "--measurements",
          dest="print_measurements",
          default=False,
          action="store_true",
          help="Open the pipeline file specified by the -p switch and print the measurements made by that pipeline",
      )
  
      parser.add_option(
          "--print-groups",
          dest="print_groups_file",
          default=None,
          help="Open the measurements file following the --print-groups switch and print the groups in its image sets. The measurements file should be generated using CreateBatchFiles. The output is a JSON-encoded data structure containing the group keys and values and the image sets in each group.",
      )
  
      parser.add_option(
          "--get-batch-commands",
          dest="batch_commands_file",
          default=None,
          help='Open the measurements file following the --get-batch-commands switch and print one line to the console per group. The measurements file should be generated using CreateBatchFiles and the image sets should be grouped into the units to be run. Each line is a command to invoke CellProfiler. You can use this option to generate a shell script that will invoke CellProfiler on a cluster by substituting "CellProfiler" '
          "with your invocation command in the script's text, for instance: CellProfiler --get-batch-commands Batch_data.h5 | sed s/CellProfiler/farm_jobs.sh. Note that CellProfiler will always run in headless mode when --get-batch-commands is present and will exit after generating the batch commands without processing any pipeline. Note that this exact version is deprecated and will be removed in CellProfiler 5; you may use the new version now with --get-batch-commands-new",
      )
  
      parser.add_option(
          "--get-batch-commands-new",
          dest="new_batch_commands_file",
          default=None,
          help='Open the batch file following the --get-batch-commands-new switch and print one line to the console per group. Each line is a command to invoke CellProfiler. You can use this option to generate a shell script that will invoke CellProfiler on a cluster by substituting "CellProfiler". This new version (which will be the only version in CellProfiler 5) will return groups if CellProfiler has more than one group and --images-per-batch is NOT passed (or is passed as 1), otherwise it will always return -f and -l commands. '
          "with your invocation command in the script's text, for instance: CellProfiler --get-batch-commands-new Batch_data.h5 | sed s/CellProfiler/farm_jobs.sh. Note that CellProfiler will always run in headless mode when --get-batch-commands is present and will exit after generating the batch commands without processing any pipeline.",
      )
  
      parser.add_option(
          "--images-per-batch",
          dest="images_per_batch",
          default="1",
          help="For pipelines that do not use image grouping this option specifies the number of images that should be processed in each batch if --get-batch-commands is used. Defaults to 1.",
      )
  
      parser.add_option(
          "--data-file",
          dest="data_file",
          default=None,
          help="Specify the location of a .csv file for LoadData. If this switch is present, this file is used instead of the one specified in the LoadData module.",
      )
  
      parser.add_option(
          "--file-list",
          dest="image_set_file",
          default=None,
          help="Specify a file list of one file or URL per line to be used to initially populate the Images module's file list.",
      )
  
      parser.add_option(
          "--do-not-write-schema",
          dest="allow_schema_write",
          default=True,
          action="store_false",
          help="Do not execute the schema definition and other per-experiment SQL commands during initialization when running a pipeline in batch mode.",
      )
  
      parser.add_option(
          "--write-schema-and-exit",
          dest="write_schema_and_exit",
          default=False,
          action="store_true",
          help="Create the experiment database schema and exit",
      )
  
      # TODO: disabled until CellProfiler/CellProfiler#4684 is resolved
      # parser.add_option(
      #     "--omero-credentials",
      #     dest="omero_credentials",
      #     default=None,
      #     help=(
      #         "Enter login credentials for OMERO. The credentials"
      #         " are entered as comma-separated key/value pairs with"
      #         ' keys, "%(OMERO_CK_HOST)s" - the DNS host name for the OMERO server'
      #         ', "%(OMERO_CK_PORT)s" - the server\'s port # (typically 4064)'
      #         ', "%(OMERO_CK_USER)s" - the name of the connecting user'
      #         ', "%(OMERO_CK_PASSWORD)s" - the connecting user\'s password'
      #         ', "%(OMERO_CK_SESSION_ID)s" - the session ID for an OMERO client session.'
      #         ', "%(OMERO_CK_CONFIG_FILE)s" - the path to the OMERO credentials config file.'
      #         " A typical set of credentials might be:"
      #         " --omero-credentials host=demo.openmicroscopy.org,port=4064,session-id=atrvomvjcjfe7t01e8eu59amixmqqkfp"
      #     )
      #     % globals(),
      # )
  
      parser.add_option(
          "-L",
          "--log-level",
          dest="log_level",
          default=str(logging.INFO),
          help=(
              "Set the verbosity for logging messages: "
              + ("%d or %s for debugging, " % (logging.DEBUG, "DEBUG"))
              + (
                  "%d or %s for informational, "
                  % (logging.INFO, "INFO")
              )
              + (
                  "%d or %s for warning, "
                  % (logging.WARNING, "WARNING")
              )
              + ("%d or %s for error, " % (logging.ERROR, "ERROR"))
              + (
                  "%d or %s for critical, "
                  % (logging.CRITICAL, "CRITICAL")
              )
              + ("%d or %s for fatal." % (logging.FATAL, "FATAL"))
              + " Otherwise, the argument is interpreted as the file name of a log configuration file (see http://docs.python.org/library/logging.config.html for file format)"
          ),
      )
  
      parser.add_option(
          "--always-continue",
          dest="always_continue",
          default=None,
          action="store_true",
          help="Keep running after an image set throws an error",
      )
  
      parser.add_option(
          "--widget-inspector",
          dest="widget_inspector",
          default=False,
          action="store_true",
          help='Enable the widget inspector menu item under "Test"',
      )
  
      options, result_args = parser.parse_args(args[1:])
      if len(args) == 2:
          if args[1].lower().endswith((".cpproj", ".cppipe")):
              # Opening a file with CellProfiler will supply the file as an argument
              options.pipeline_filename = args[1]
              result_args = []
      return options, result_args
  
imports: import io
  import json
  import logging
  import logging.config
  import optparse
  import os
  import os.path
  import re
  import sys
  import tempfile
  import urllib.parse
  import h5py
  import matplotlib
  import numpy
  import pkg_resources
  from cellprofiler_core.constants.measurement import EXPERIMENT
  from cellprofiler_core.constants.measurement import GROUP_INDEX
  from cellprofiler_core.constants.measurement import GROUP_NUMBER
  from cellprofiler_core.constants.measurement import IMAGE
  from cellprofiler_core.constants.pipeline import M_PIPELINE, EXIT_STATUS
  from cellprofiler_core.measurement import Measurements
  from cellprofiler_core.object import ObjectSet
  from cellprofiler_core.pipeline import LoadException
  from cellprofiler_core.pipeline import Pipeline
  from cellprofiler_core.preferences import get_image_set_file
  from cellprofiler_core.preferences import get_temporary_directory
  from cellprofiler_core.preferences import set_conserve_memory
  from cellprofiler_core.preferences import set_force_bioformats
  from cellprofiler_core.preferences import set_allow_schema_write
  from cellprofiler_core.preferences import set_always_continue
  from cellprofiler_core.preferences import set_awt_headless
  from cellprofiler_core.preferences import set_data_file
  from cellprofiler_core.preferences import set_default_image_directory
  from cellprofiler_core.preferences import set_default_output_directory
  from cellprofiler_core.preferences import set_headless
  from cellprofiler_core.preferences import set_image_set_file
  from cellprofiler_core.preferences import set_plugin_directory
  from cellprofiler_core.preferences import set_temporary_directory
  from cellprofiler_core.preferences import set_widget_inspector
  from cellprofiler_core.utilities.core.workspace import is_workspace_file
  from cellprofiler_core.utilities.hdf5_dict import HDF5FileList
  from cellprofiler_core.utilities.java import start_java, stop_java
  from cellprofiler_core.utilities.measurement import load_measurements
  from cellprofiler_core.utilities.zmq import join_to_the_boundary
  from cellprofiler_core.worker import aw_parse_args
  from cellprofiler_core.worker import main
  from cellprofiler_core.workspace import Workspace
  from cellprofiler_core.reader import activate_readers
label_desc: Use image node_id 4 for calling a global function with example usage: parse_args(args) and returns: options,  result_args