Skip to content
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

feat: Add ability to define custom action callable. #27

Merged
merged 1 commit into from
Oct 20, 2023

Conversation

DanCardin
Copy link
Owner

Attempt at addressing issue described in #7, where one cannot replicate --help/--version alike arguments. basically, you'd supply a callable as an action and raise Exit.

But i think the idea as implemented has some other nice side-effects. when using the native backend, it can use the invoke system to dependency-inject parser state very easily. the main question is whether or not it's a good idea to expose that parser state to downstream consumers.

It's certainly useful inside the parser itself to send arguments to the native handlers, so 🤷

@coveralls
Copy link

coveralls commented Oct 20, 2023

Pull Request Test Coverage Report for Build 6590645774

  • 94 of 94 (100.0%) changed or added relevant lines in 4 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 6589255068: 0.0%
Covered Lines: 1536
Relevant Lines: 1536

💛 - Coveralls

Copy link
Sponsor Contributor

@pawamoy pawamoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using the cappa backend: cappa.Command, cappa.Arg, cappa.backend.ParseContext,
cappa.backend.Value are acceptable input values. They'll be automatically injected
into the action callable.

An example here would help a lot to understand.

IIUC with the cappa backend one can do:

def foo(
    origin_command: cappa.Command,
    origin_arg: cappa.Arg,
    context: cappa.backend.ParseContext,
    backend_value: cappa.backend.Value,
):
    # use these args as you see fit
    raise cappa.Exit("message")


@dataclass
class Args:
    foo: Annotated[str, cappa.Arg(action=foo, short=True)]

...with arbitrary argument names or positions since their values will be injected based on their type.

Is that correct?

It would be great if these 4 mentioned classes linked to their respective API docs (I'm not sure what cappa.backend.Value represents).

src/cappa/arg.py Show resolved Hide resolved
@DanCardin
Copy link
Owner Author

...with arbitrary argument names or positions since their values will be injected based on their type.

Is that correct?

It would be great if these 4 mentioned classes linked to their respective API docs (I'm not sure what cappa.backend.Value represents).

Right, it's like an invoke-equivalent to the arguments you get when subclassing an argparse Action on the __call__ method.

I left the objects somewhat intentionally undocumented because this feature makes those objects part of the public interface of the library, and i'm not confident (especially with ParseContext) that they wont have breaking changes.

I suppose I can just leave those objects out of the docs...in fact yes that's what I'll do.

@pawamoy
Copy link
Sponsor Contributor

pawamoy commented Oct 20, 2023

Yes, that sounds good 🙂

@DanCardin DanCardin marked this pull request as ready for review October 20, 2023 17:05
@DanCardin
Copy link
Owner Author

In fact, it turns out I can get argparse to supply Arg, Value, and Command. It now just leaves off documenting the parser context

@DanCardin DanCardin merged commit 8770131 into main Oct 20, 2023
6 checks passed
@DanCardin DanCardin deleted the dc/custom-action branch October 20, 2023 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants