<commit_before><?php

final class PhabricatorConfigManagementSetWorkflow
  extends PhabricatorConfigManagementWorkflow {

  protected function didConstruct() {
    $this
      ->setName('set')
      ->setExamples('**set** __key__ __value__')
      ->setSynopsis(pht('Set a local configuration value.'))
      ->setArguments(
        array(
          array(
            'name'  => 'database',
            'help'  => pht('Update configuration in the database instead of '.
            'in local configuration.'),
          ),
          array(
            'name'      => 'args',
            'wildcard'  => true,
          ),
        ));
  }

  public function execute(PhutilArgumentParser $args) {
    $console = PhutilConsole::getConsole();
    $argv = $args->getArg('args');
    if (count($argv) == 0) {
      throw new PhutilArgumentUsageException(pht(
        'Specify a configuration key and a value to set it to.'));
    }

    $key = $argv[0];

    if (count($argv) == 1) {
      throw new PhutilArgumentUsageException(pht(
        "Specify a value to set the key '%s' to.",
        $key));
    }

    $value = $argv[1];

    if (count($argv) > 2) {
      throw new PhutilArgumentUsageException(pht(
        'Too many arguments: expected one key and one value.'));
    }

    $options = PhabricatorApplicationConfigOptions::loadAllOptions();
    if (empty($options[$key])) {
      throw new PhutilArgumentUsageException(pht(
        "No such configuration key '%s'! Use `config list` to list all ".
        "keys.",
        $key));
    }

    $option = $options[$key];

    $type = $option->getType();
    switch ($type) {
      case 'string':
      case 'class':
      case 'enum':
        $value = (string)$value;
        break;
      case 'int':
        if (!ctype_digit($value)) {
          throw new PhutilArgumentUsageException(pht(
            "Config key '%s' is of type '%s'. Specify an integer.",
            $key,
            $type));
        }
        $value = (int)$value;
        break;
      case 'bool':
        if ($value == 'true') {
          $value = true;
        } else if ($value == 'false') {
          $value = false;
        } else {
          throw new PhutilArgumentUsageException(pht(
            "Config key '%s' is of type '%s'. ".
            "Specify 'true' or 'false'.",
            $key,
            $type));
        }
        break;
      default:
        $value = json_decode($value, true);
        if (!is_array($value)) {
          throw new PhutilArgumentUsageException(pht(
            "Config key '%s' is of type '%s'. Specify it in JSON.",
            $key,
            $type));
        }
        break;
    }
    $use_database = $args->getArg('database');
    if ($option->getLocked() && $use_database) {<commit_msg>Improve error messages when specifying bad set or list to bin/config<commit_after><?php

final class PhabricatorConfigManagementSetWorkflow
  extends PhabricatorConfigManagementWorkflow {

  protected function didConstruct() {
    $this
      ->setName('set')
      ->setExamples('**set** __key__ __value__')
      ->setSynopsis(pht('Set a local configuration value.'))
      ->setArguments(
        array(
          array(
            'name'  => 'database',
            'help'  => pht('Update configuration in the database instead of '.
            'in local configuration.'),
          ),
          array(
            'name'      => 'args',
            'wildcard'  => true,
          ),
        ));
  }

  public function execute(PhutilArgumentParser $args) {
    $console = PhutilConsole::getConsole();
    $argv = $args->getArg('args');
    if (count($argv) == 0) {
      throw new PhutilArgumentUsageException(pht(
        'Specify a configuration key and a value to set it to.'));
    }

    $key = $argv[0];

    if (count($argv) == 1) {
      throw new PhutilArgumentUsageException(pht(
        "Specify a value to set the key '%s' to.",
        $key));
    }

    $value = $argv[1];

    if (count($argv) > 2) {
      throw new PhutilArgumentUsageException(pht(
        'Too many arguments: expected one key and one value.'));
    }

    $options = PhabricatorApplicationConfigOptions::loadAllOptions();
    if (empty($options[$key])) {
      throw new PhutilArgumentUsageException(pht(
        "No such configuration key '%s'! Use `config list` to list all ".
        "keys.",
        $key));
    }

    $option = $options[$key];

    $type = $option->getType();
    switch ($type) {
      case 'string':
      case 'class':
      case 'enum':
        $value = (string)$value;
        break;
      case 'int':
        if (!ctype_digit($value)) {
          throw new PhutilArgumentUsageException(pht(
            "Config key '%s' is of type '%s'. Specify an integer.",
            $key,
            $type));
        }
        $value = (int)$value;
        break;
      case 'bool':
        if ($value == 'true') {
          $value = true;
        } else if ($value == 'false') {
          $value = false;
        } else {
          throw new PhutilArgumentUsageException(pht(
            "Config key '%s' is of type '%s'. ".
            "Specify 'true' or 'false'.",
            $key,
            $type));
        }
        break;
      default:
        $value = json_decode($value, true);
        if (!is_array($value)) {
          switch ($type) {
            case 'set':
              $message = pht(
                "Config key '%s' is of type '%s'. Specify it in JSON. ".
                "For example:\n\n".
                '    ./bin/config set \'{"value1": true, "value2": true}\''.
                "\n",
                $key,
                $type);
              break;
            default:
              if (preg_match('/^list</', $type)) {
                $message = pht(
                  "Config key '%s' is of type '%s'. Specify it in JSON. ".
                  "For example:\n\n".
                  '    ./bin/config set \'["a", "b", "c"]\''.
                  "\n",
                  $key,
                  $type);
              } else {
                $message = pht(
                  'Config key "%s" is of type "%s". Specify it in JSON.',
                  $key,
                  $type);
              }
              break;
          }
          throw new PhutilArgumentUsageException($message);
        }
        break;
    }
    $use_database = $args->getArg('database');
    if ($option->getLocked() && $use_database) {
{'fraction_non_alphanumeric': 0.14223399283377472, 'fraction_numerical': 0.0018694500701043775, 'mean_word_length': 1.687316868982838, 'pattern_counts': {'":': 2, '<': 6, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2895696', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1824, 'n_words': 502}