Packages

Librería base para ejecución de comandos del sistema y gestión de tareas asíncronas con resultados estructurados.

Retired package: Release invalid - Versión publicada por error

Current section

Files

Jump to
argos lib cli parser.ex
Raw

lib/cli/parser.ex

defmodule Argos.CLI.Parser do
@moduledoc """
Módulo para parsear argumentos de línea de comandos.
"""
@doc """
Parsea los argumentos de línea de comandos.
Detects if help is requested for a specific command by checking if `--help` or `-h`
appears after a command in the remaining arguments.
"""
@spec parse_args([String.t()]) :: {[String.t()], [String.t()], keyword(), String.t() | nil}
def parse_args(argv) do
{opts, remaining, _errors} =
OptionParser.parse(argv,
strict: [
command: :keep,
help: :boolean,
examples: :boolean,
version: :boolean,
no_config: :boolean,
load_config: :boolean,
no_login: :boolean,
asdf_local: :boolean
],
aliases: [
c: :command,
h: :help,
e: :examples,
v: :version
]
)
commands = collect_commands(opts)
command_for_help = detect_command_for_help(remaining, opts)
{commands, remaining, opts, command_for_help}
end
defp detect_command_for_help(remaining, opts) do
help_requested = Keyword.get(opts, :help, false)
if help_requested and length(remaining) > 0 do
command = List.first(remaining)
if command in ["--help", "-h", "help"] do
nil
else
command
end
else
nil
end
end
@doc """
Recoge todos los comandos de las opciones (puede haber múltiples --command).
"""
@spec collect_commands(keyword()) :: [String.t()]
def collect_commands(opts) do
opts
|> Keyword.get_values(:command)
|> Enum.filter(&(&1 != nil and &1 != ""))
end
end