-
Notifications
You must be signed in to change notification settings - Fork 0
feat: support :integer option type in CLI spec #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d741a39
2b08f44
5629776
6adf83e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| defmodule ToolKit.CLI.SpecTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| alias ToolKit.CLI.Parser | ||
| alias ToolKit.CLI.Spec | ||
| alias ToolKit.Test.RegistryManagerSpecFixture, as: Fixture | ||
|
|
||
|
|
@@ -45,6 +46,37 @@ defmodule ToolKit.CLI.SpecTest do | |
| end | ||
| end | ||
|
|
||
| test "integer options derive strict switches and render a VALUE placeholder" do | ||
| spec = %Spec{ | ||
| tool_name: "demo", | ||
| tool_summary: "demo tool", | ||
| option_catalog: %{ | ||
| help: %{type: :boolean, alias: :h, values: nil, doc: "help"}, | ||
| jobs: %{type: :integer, alias: nil, values: nil, doc: "並列数"} | ||
| }, | ||
| global_option_names: [:help], | ||
| commands: [ | ||
| %{ | ||
| name: "run", | ||
| aliases: [], | ||
| usage: ["run"], | ||
| summary: "run", | ||
| options: [:jobs], | ||
| examples: ["run --jobs 4"] | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| assert {:jobs, :integer} in Spec.strict_switches(spec) | ||
| assert Spec.render_command_help(spec, "run") =~ "--jobs VALUE" | ||
| assert Spec.validate_opts(spec, "run", jobs: 4) == :ok | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 修正済み(2b08f44)。整数でない値の拒否は OptionParser(strict)の責務で、Parser がパース段階のエラーに変換します。 |
||
|
|
||
| # 整数でない値の拒否は OptionParser(strict)の責務で、 | ||
| # Parser がパース段階のエラーに変換する | ||
| assert {:error, message} = Parser.parse(spec, ["run", "--jobs", "four"]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ℹ️ [LOW]
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 据え置きます。負値・0 などの値域検証は spec エンジンの責務ではなくツール側のビジネスロジックの責務です(OptionParser は任意の整数を受理し、例えば max_concurrency の下限はツールが判断する)。エラーメッセージ文言のアサーション追加は、文言変更のたびに壊れる brittle なテストになるため、エラーであること + 対象オプション名の包含の検証に留めています。 |
||
| assert message =~ "--jobs" | ||
| end | ||
|
|
||
| test "command option overrides replace values and doc", %{spec: spec} do | ||
| list_command = Spec.find_command(spec, "list") | ||
| sort = spec |> Spec.options_for(list_command) |> Enum.find(&(&1.name == :sort)) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ [LOW]
:integer型のオプションに対してvaluesフィールドがnil以外(例:["1", "2", "4"]のような候補リスト)を持つケースを将来的に許容するかどうかが不明確です。現在のrender_values/1が:string向けに設計されている場合、:integerでvaluesが非nilのときの描画が意図通りになるか確認が必要です。型定義上values: [String.t()] | nilのままであり、:integerとの組み合わせの意味論が曖昧です。将来の拡張を見越してコメントや型定義で制約を明示することを検討してください。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
据え置きます。
valuesは enum 検証(パース済み値が文字列リストに含まれるか)のためのフィールドで、意味論上 :string 専用です。:integer オプションで values を設定すると validate_opts が必ず不一致になるため事実上使えず、使う予定もありません(現行の利用者 rm/tm/em に該当なし)。必要になった時点で型定義の制約強化を検討します。