-
with inheritance I can declare a common set of arguments for various subcommands like so:
Now I'd like to still have
would that be possible ? Maybe the more generic question would be the same as what is achievable in this SO question ablout click: |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 14 replies
-
I dont see how this could work in a way that relies on runtime decided conditional inheritance. The parser needs to know ahead of time which options are actually available in order to parse them and to generate Functionally, It really depends on the details of the alternate base that you're envisioning. If With this you could get UX like If the intent is that they have different flag names, then that's more complicated and confusing. As i said originally, the flags for both options ultimately need to exist at parse time, before you've encountered any options at all (at least from a While it doesn't exist now, I could imagine there being a feature that makes (more ergnonomic) certain combinations of argument values tied to some other input value, although it's not clear to me right now what that syntax might look like. You could probably emulate it today through some sort of custom action from cappa.parser import ParseContext
def two_action(context: ParseContext):
if 'switch' not in context.result:
raise ValueError("--switch must be provided before whatever this arg is")
value = context.result['switch']
if value == '1':
raise ValueError("--switch 1 is incompatible with this flag")
...
class Foo:
foo: Annotated[... | None, cappa.Arg(action=two_action)] = None
bar: Annotated[... | None, cappa.Arg(action=one_action)] = None or something to that effect. But notably the fields for the alternate flags would both need to exist. |
Beta Was this translation helpful? Give feedback.
-
you know, in fact, your original question ultimately just sounds like more subcommands tbh...like the way you get different downstream subsets of arguments is subcommands it probably wouldnt work today, but i could imagine a Which wouldnt be a |
Beta Was this translation helpful? Give feedback.
-
ok I spoke too fast or I'm missing something obvious, taking your example and adding some invoke functions I get this error when I call I added a invoke function to GoogleArgs for instance (in fact the leaf command) and I get the args passed correctly, but I'm unable to say it was from migrate or ingest
|
Beta Was this translation helpful? Give feedback.
-
seems like this is working:
would be easier to be able to do :
but I failed at making it work, seems like union is not a thing, |
Beta Was this translation helpful? Give feedback.
Something else that just occurred to me again, is that
Subcommands[A, B]
==Annotated[A | B, Subcommand()]
andSubcommand()
is short forSubcommand(options={'a': Command(...), 'b': Command(...)})
So you can get more custom/fancier by actually supplying your own
options
to overrride the default behavior.This is a fully contained example you could run to see if it gives you the desired behavior. And you can then lean more into or further out of the DRY ness as needed.