Skip to content

Postcommand Hooks and argparse #905

Closed
@grolinet

Description

@grolinet

Hi

I am using argparse for autocompletion and with sub-commands.
I always annotate my command methods with @with_argparser(parser) which i think is the right way to use the argparser with cmd2.
I did experiment with postcommand hooks using the register_postcmd_hook method.
The documentation states, that the registered postcommand is only executed when the command method was called (in case of a raised exception the postcommand it is not executed).

Now, if argparse can't parse the submitted args (for example when there are too few), the command method is not executed, but the postcommand hooks will be executed anyway.

I understand, that in this context, the @with_argparser(parser) belongs to the method and thus one could argument, the method was called and didn't raise an exception, but on the other side, in most cases (i think), it makes not a lot of sense to execute postcommands if argparse can't parse the arguments of the command method.
Is this behaviour intended? And if yes, is there a possibility to change it? I know i could ditch the annotations, use the argparser inside the command method and raise an exception if argparse fails, but this seems to be a lot of overhead (and i would lose the autocomplete stuff).

Could i possibly "monkey patch" the with_argparser method so a exception will be raised? (Also not a very elegant solution i think)

Thanks for any help.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions