-
Notifications
You must be signed in to change notification settings - Fork 42
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
What is the right way to call set_defaults
#109
Comments
The alternative is to use So if I do this for example:
And run, then I get |
I also tried with
But get the same error. It seems there is some code that attempts to ignore class variables starting with typed-argument-parser/tap/tap.py Lines 509 to 522 in eb55a9b
But this does not seem to be working as intended, because these still show up in Is this another bug? If so I will raise an issue for it. |
As a workaround I have done this: class MainParser(Tap):
subparser_flag: str
def configure(self):
self.add_argument(
# Hack to work around problems with Tap, without this Tap complains
# that `subparser_flag` is not set even if a subcommand is supplied,
# for more info see
# <https://github.com/swansonk14/typed-argument-parser/issues/109>.
"--subparser_flag",
action="store",
# dest="subparser_flag",
help=argparse.SUPPRESS,
required=False,
)
self.add_subparsers(required=True, dest="subparser_flag", help="sub-command") |
Hi @aucampia, Unfortunately, we couldn't quite follow this issue. We realize that there's some concern with our current treatment of subparsers. Would you be able to clarify with some example code and then a description of the desired behavior versus the existing behavior? Thanks, |
@aucampia from tap import Tap
class Foo(Tap):
def configure(self):
self.set_defaults(func=lambda: print("foo subcommand"))
class Bar(Tap):
def configure(self):
self.set_defaults(func=lambda: print("bar subcommand"))
class MyTap(Tap):
def configure(self):
self.add_subparsers()
self.add_subparser("foo", Foo)
self.add_subparser("bar", Bar)
args = MyTap().parse_args(["foo"])
args.func() # prints "foo subcommand"
args = MyTap().parse_args(["bar"])
args.func() # prints "bar subcommand" Edit: The issue with this solution is that the lines However, I am assuming the typed solution may be out of scope for Tap since this is essentially inheriting the functionality from argparse. |
The documentation for
argparse
[ref] suggests usingset_defaults
for handling subcommands, but I'm not sure what is the right way to callset_defaults
with TAP, it would be great if you can add some examples or documentation for this.The text was updated successfully, but these errors were encountered: