errbotio / errbot

Errbot is a chatbot, a daemon that connects to your favorite chat service and bring your tools and some fun into the conversation.
http://errbot.io
GNU General Public License v3.0
3.13k stars 615 forks source link

Command with a default arg fails to run when omitting the argument #1483

Open alpinweis opened 3 years ago

alpinweis commented 3 years ago

In order to let us help you better, please fill out the following fields as best you can:

I am...

I am running...

Issue description

I have a command with an argument. I'd like it to work with a default value if the argument is omitted.

@arg_botcmd("service_name", type=str, default="myservice", help="Service name")
def getstatus(self, msg, service_name):
  ...

I get an error when I try to run it w/o an argument.

User: !getstatus myservice
Errbot: ok

User: !getstatus
Errbot: I couldn't parse the arguments; the following arguments are required: service_name

The correct/expected result would be for the command to pick the default value myservice for the missing argument service_name. I also tried to use a default arg value in the method signature, but it did not help.

def getstatus(self, msg, service_name="myservice"):
duhow commented 3 years ago

As how the arg_botcmd is working now, first argument creates the ArgumentParser object, and does not allow setting the required flag you're looking for. https://docs.python.org/3/library/argparse.html#required

https://github.com/errbotio/errbot/blob/54542fd38250b4cb977283854c5cb2b446b1281b/errbot/__init__.py#L383-L386

But the next ones can use the argparse options. https://github.com/errbotio/errbot/blob/54542fd38250b4cb977283854c5cb2b446b1281b/errbot/__init__.py#L445