<?php
namespace nur\mapper\line;

use nur\A;
use nur\mapper\base\Tparams_command;

/**
 * Class LineWriter_command: classe de support pour construire une instance de
 * {@link LineWriter} pour une application recevant des commandes utilisateurs
 */
class LineWriter_command {
  use Tparams_command;

  const NAME = "outfile";
  const DEF = [self::NAME, "outf",
    "help" => "configurer le fichier en sortie pour écriture ligne par ligne",
    "action" => [self::class, "create_command"],
    "cmd_args" => [
      "usage" => self::NAME." [file]",
      ["-t", "--output-encoding", "args" => "value",
        "action" => [self::class, "set_output_encoding"],
      ],
      ["-o", "--param", "args" => "value",
        "action" => [self::class, "add_params"],
        "help" => "spécifier une option générique",
      ],
      ["args" => [["file"]],
        "action" => [self::class, "set_file"],
      ],
    ],
  ];

  const O_OPTION = ["-o", "--output", "args" => "file",
    "action" => [self::class, "set_file"],
  ];
  const T_OPTION = ["-t", "--output-encoding", "args" => "value",
    "action" => [self::class, "set_output_encoding"],
  ];

  /** @var LineWriter */
  protected static $command;

  static function create_command() {
    self::$command = new LineWriter();
  }

  static function set_file($file) {
    $file = A::last(A::with($file));
    self::get()->setOutput($file);
  }

  static function set_output_encoding(string $output_encoding) {
    self::get()->setEncodingFilter($output_encoding);
  }
}
