Support custom type coertion

Open
#2 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
ruby
Domain
cli

Research direction

Read the current type-coercion branch and Ruby's OptionParser#accept API first, then trace how parser definitions are registered and applied. Done means callers can register a custom User coercion alongside built-in types, parse the example input into a User, and retain existing required and pattern behavior.

Written by the indexing model from the issue text.

Description

good first issue

Instead of an if/elsif/else expression, we can store accepted types in a hash and a proc that uses it to convert. That way, users can register custom type coercion strategies.

I'm thinking something like:

parser = ArgumentParser.build do
  accept User do |string|
    User.find(string)
  end

  required :action, pattern: [:delete, :update]
  required :user, type: User
end

args = parser.parse!(["user-1"])
# => { user: #<User id="user-1"> }

We can copy OptionParser's API for that.

Dominant language
Ruby
Stars
3
Forks
0
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from thoughtbot/argument_parser

All issues in thoughtbot/argument_parser

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.