This section is building a tree of dictionaries based on the commands, right? The setdefault() method of dict is useful for ensuring that a key is present without testing from the loop, and looping over a slice of the command list without processing the last item means you don't have to keep track of last and lastsubcmd:

  for subcmd in command[:-1]:
    dicto = dicto.setdefault(subcmd, {})
  dicto[command[-1]] = optstr