From da733c06640b8d894bf8743b91377715e8b116b4 Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Tue, 11 Jul 2023 21:38:25 -0400 Subject: [PATCH] fix: better error message when passing invalid flags Closes #103 --- lib/next_ls/lsp_supervisor.ex | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/next_ls/lsp_supervisor.ex b/lib/next_ls/lsp_supervisor.ex index 59bd7e66..80a80339 100644 --- a/lib/next_ls/lsp_supervisor.ex +++ b/lib/next_ls/lsp_supervisor.ex @@ -5,6 +5,25 @@ defmodule NextLS.LSPSupervisor do @env Mix.env() + defmodule OptionsError do + @moduledoc false + defexception [:message] + + @impl true + def exception(options) do + msg = """ + Unknown Options: #{Enum.map_join(options, " ", fn {k, v} -> "#{k} #{v}" end)} + + Valid options: + + --stdio Starts the server using stdio + --port port-number Starts the server using TCP on the given port + """ + + %OptionsError{message: msg} + end + end + def start_link(init_arg) do Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__) end @@ -14,8 +33,8 @@ defmodule NextLS.LSPSupervisor do if @env == :test do :ignore else - {opts, _} = - OptionParser.parse!(System.argv(), + {opts, _, invalid} = + OptionParser.parse(System.argv(), strict: [stdio: :boolean, port: :integer] ) @@ -29,7 +48,7 @@ defmodule NextLS.LSPSupervisor do [communication: {GenLSP.Communication.TCP, [port: opts[:port]]}] true -> - raise "Unknown option" + raise OptionsError, invalid end children = [