-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Conversation
Pull Request Test Coverage Report for Build 6590645774
💛 - Coveralls |
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.
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).
Right, it's like an invoke-equivalent to the arguments you get when subclassing an argparse 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 I suppose I can just leave those objects out of the docs...in fact yes that's what I'll do. |
Yes, that sounds good 🙂 |
268b152
to
e3aeded
Compare
e3aeded
to
85c035b
Compare
In fact, it turns out I can get argparse to supply Arg, Value, and Command. It now just leaves off documenting the parser context |
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 🤷