<?php
namespace nur\mapper\line;

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

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

  const NAME = "infile";
  const DEF = [self::NAME, "inf",
    "help" => "configurer le fichier en entrée pour lecture ligne par ligne",
    "action" => [self::class, "create_command"],
    "cmd_args" => [
      "usage" => self::NAME." [file]",
      ["-e", "--input-encoding", "args" => "value",
        "action" => [self::class, "set_input_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 F_OPTION = ["-f", "--input", "args" => "file",
    "action" => [self::class, "set_file"],
  ];
  const E_OPTION = ["-e", "--input-encoding", "args" => "value",
    "action" => [self::class, "set_input_encoding"],
  ];

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

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

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

  static function set_input_encoding(string $input_encoding) {
    self::get()->setEncodingFilter($input_encoding);
  }
}
