code: def commandfactory(cmdline, mode='global'):
      
      # split commandname and parameters
      if not cmdline:
          return None
      logging.debug('mode:%s got commandline "%s"', mode, cmdline)
      # allow to shellescape without a space after '!'
      if cmdline.startswith('!'):
          cmdline = 'shellescape \'%s\'' % cmdline[1:]
      cmdline = re.sub(r'"(.*)"', r'"\\"\1\\""', cmdline)
      try:
          args = split_commandstring(cmdline)
      except ValueError as e:
          raise CommandParseError(str(e))
      args = [string_decode(x, 'utf-8') for x in args]
      logging.debug('ARGS: %s', args)
      cmdname = args[0]
      args = args[1:]
  
      # unfold aliases
      # TODO: read from settingsmanager
  
      # get class, argparser and forced parameter
      (cmdclass, parser, forcedparms) = lookup_command(cmdname, mode)
      if cmdclass is None:
          msg = 'unknown command: %s' % cmdname
          logging.debug(msg)
          raise CommandParseError(msg)
  
      parms = vars(parser.parse_args(args))
      parms.update(forcedparms)
  
      logging.debug('cmd parms %s', parms)
  
      # create Command
      cmd = cmdclass(**parms)
  
      # set pre and post command hooks
      get_hook = settings.get_hook
      cmd.prehook = get_hook('pre_%s_%s' % (mode, cmdname)) or \
          get_hook('pre_global_%s' % cmdname)
      cmd.posthook = get_hook('post_%s_%s' % (mode, cmdname)) or \
          get_hook('post_global_%s' % cmdname)
  
      return cmd
docs: parses `cmdline` and constructs a :class:`Command`.
  
      :param cmdline: command line to interpret
      :type cmdline: str
      :param mode: mode identifier
      :type mode: str