From d6394fcc06421edfd58ecabda7cc48eee1ad35e1 Mon Sep 17 00:00:00 2001 From: dstroch Date: Sat, 24 Feb 2024 20:43:10 +0100 Subject: [PATCH 1/2] Do not rescue matchError for positional argument --- lib/cli.ex | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 9033521..4703ce2 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -5,15 +5,17 @@ defmodule Onigumo.CLI do def main(argv) do try do - {[], [component], []} = OptionParser.parse(argv, strict: []) - component + {[], argv, []} = OptionParser.parse(argv, strict: []) + argv rescue MatchError -> usage_message() else - component -> + [component] -> {:ok, module} = Map.fetch(@components, String.to_atom(component)) root_path = File.cwd!() module.main(root_path) + _ -> + usage_message() end end From 445b8675134b722d86a5a3b6fab102fa79ad0e2d Mon Sep 17 00:00:00 2001 From: Glutexo Date: Sat, 24 Feb 2024 21:16:39 +0100 Subject: [PATCH 2/2] Use case instead of MatchErrror Instead of pattern matching causing a MatchError, use case directly matching pattern for the happy path and for the invalid argument path. --- lib/cli.ex | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index 4703ce2..53e2f6f 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -4,16 +4,12 @@ defmodule Onigumo.CLI do } def main(argv) do - try do - {[], argv, []} = OptionParser.parse(argv, strict: []) - argv - rescue - MatchError -> usage_message() - else - [component] -> + case OptionParser.parse(argv, strict: []) do + {[], [component], []} -> {:ok, module} = Map.fetch(@components, String.to_atom(component)) root_path = File.cwd!() module.main(root_path) + _ -> usage_message() end