diff --git a/lib/cli/args.ex b/lib/cli/args.ex index edf31e8..0ceb56b 100644 --- a/lib/cli/args.ex +++ b/lib/cli/args.ex @@ -17,4 +17,15 @@ defmodule ExTTRPGDev.CLI.Args do ] ] end + + def character do + [ + character: [ + value_name: "CHARACTER", + help: "A saved character, e.g. misu_park", + required: true, + parser: &CustomParsers.character_parser(&1) + ] + ] + end end diff --git a/lib/cli/characters.ex b/lib/cli/characters.ex index fbb9311..74fad58 100644 --- a/lib/cli/characters.ex +++ b/lib/cli/characters.ex @@ -32,6 +32,11 @@ defmodule ExTTRPGDev.CLI.Characters do list: [ name: "list", about: "List saved characters" + ], + show: [ + name: "show", + about: "Show information for a character", + args: Args.character() ] ] ] @@ -68,4 +73,10 @@ defmodule ExTTRPGDev.CLI.Characters do Enum.each(characters, fn character -> IO.puts("- #{character}") end) end end + + def handle_characters_subcommands([:show | _subcommands], %Optimus.ParseResult{ + args: %{character: character} + }) do + IO.inspect(character) + end end diff --git a/lib/cli/custom_parsers.ex b/lib/cli/custom_parsers.ex index d7eceaa..44c6c03 100644 --- a/lib/cli/custom_parsers.ex +++ b/lib/cli/custom_parsers.ex @@ -3,6 +3,7 @@ defmodule ExTTRPGDev.CLI.CustomParsers do Custom parsers to be used with Optimus args :parse """ alias ExTTRPGDev.RuleSystems + alias ExTTRPGDev.Characters @doc """ Parses a string of dice specifications seperated by commas @@ -37,4 +38,15 @@ defmodule ExTTRPGDev.CLI.CustomParsers do "\"#{system}\" is not configured, run `ex_ttrpg_dev systems list` to list configured systems"} end end + + def character_parser(character_slug) when is_bitstring(character_slug) do + if Characters.character_exists?(character_slug) do + character_slug + |> Characters.load_character!() + |> Kernel.then(fn loaded_character -> {:ok, loaded_character} end) + else + {:error, + "Character matching \"#{character_slug}\" was not found, run `ex_ttrpg_dev characters list` to list existing characters"} + end + end end diff --git a/test/custom_parsers_test.exs b/test/custom_parsers_test.exs index 152fd05..44802d5 100644 --- a/test/custom_parsers_test.exs +++ b/test/custom_parsers_test.exs @@ -1,7 +1,10 @@ defmodule ExTTRPGDevTest.CLI.CustomParsers do use ExUnit.Case + alias ExTTRPGDev.Characters + alias ExTTRPGDev.Characters.Character alias ExTTRPGDev.CLI.CustomParsers + alias ExTTRPGDev.RuleSystems alias ExTTRPGDev.RuleSystems.RuleSystem doctest ExTTRPGDev.CLI.CustomParsers, @@ -9,6 +12,23 @@ defmodule ExTTRPGDevTest.CLI.CustomParsers do system_parser: 1 ] + def build_test_character do + RuleSystems.list_systems() + |> List.first() + |> RuleSystems.load_system!() + |> Character.gen_character!() + end + + def save_test_character do + character = build_test_character() + Characters.save_character!(character) + character + end + + def delete_test_character(%Character{} = character) do + File.rm(Characters.character_file_path!(character)) + end + def system_parser_test do # If no error was raised, then all is good {:ok, %RuleSystem{}} = CustomParsers.system_parser("dnd_5e_srd") @@ -16,4 +36,18 @@ defmodule ExTTRPGDevTest.CLI.CustomParsers do # Should raise an error assert_raise RuntimeError, CustomParsers.system_parser("unknown_system") end + + def character_parser_test do + character = save_test_character() + + {:ok, %Character{}} = CustomParsers.character_parser(character.metadata.slug) + + # cleanup + delete_test_character(character) + + # should raise an error + assert_raise RuntimeError, CustomParsers.character_parser(character.metadata.slug) + + assert False + end end